Answered by:
How do I use .NET Reflection to inspect a generic type object?

Question
-
How do I use .NET Reflection to inspect a generic type object?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Wednesday, April 8, 2009 6:04 AM
Answers
-
We can follow these steps to retrieve the value of a collection class:
1) Verify that the object is a generic type object.
if (myList.GetType().IsGenericType)
2) Retrieve the Count property to determine the elements quantity of the list.
int n = (int)myList.GetType().GetProperty("Count").GetValue(myList, null);
3) Use a loop to call GetValue() on each element in the generic type object.
// Process each element in the list for (int i = 0; i < n; i++) { // Get the list element as type object object[] index = { i }; object myObject = myList.GetType().GetProperty("Item").GetValue(myList, index); }
For a detailed code sample, please check the related thread:
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/4d310108-ca7c-47de-b5bd-962dfc88ed92/For more FAQ about Visual C# General, please see Visual C# General FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked as answer by Xiaoyun Li – MSFT Wednesday, April 8, 2009 6:10 AM
- Edited by Xiaoyun Li – MSFT Wednesday, April 8, 2009 3:29 PM
Wednesday, April 8, 2009 6:05 AM
All replies
-
We can follow these steps to retrieve the value of a collection class:
1) Verify that the object is a generic type object.
if (myList.GetType().IsGenericType)
2) Retrieve the Count property to determine the elements quantity of the list.
int n = (int)myList.GetType().GetProperty("Count").GetValue(myList, null);
3) Use a loop to call GetValue() on each element in the generic type object.
// Process each element in the list for (int i = 0; i < n; i++) { // Get the list element as type object object[] index = { i }; object myObject = myList.GetType().GetProperty("Item").GetValue(myList, index); }
For a detailed code sample, please check the related thread:
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/4d310108-ca7c-47de-b5bd-962dfc88ed92/For more FAQ about Visual C# General, please see Visual C# General FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked as answer by Xiaoyun Li – MSFT Wednesday, April 8, 2009 6:10 AM
- Edited by Xiaoyun Li – MSFT Wednesday, April 8, 2009 3:29 PM
Wednesday, April 8, 2009 6:05 AM -
Introduction and Reference:
The common language runtime loader manages application domains, which are constituent defined boundaries around objects that have the same application scope. This management includes loading each assembly into the appropriate application domain and controlling the memory layout of the type hierarchy within each assembly.
Assemblies contain modules, modules contain types, and types contain members. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type’s methods or access its fields and properties.
For more details, please refer to this MSDN documentation:
http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspxHere is a MSDN magazine article on .NET Reflection by Mike Repass.
http://msdn.microsoft.com/en-us/magazine/cc163408.aspx
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Wednesday, April 8, 2009 6:12 AM