Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get property of generic class

Tags:

c#

I have a generic class, and an object value where obj.GetType().GetGenericTypeDefinition() == typeof(Foo<>).

class Foo<T> {     public List<T> Items { get; set; } } 

How do I get the value of Items from obj? Remember, obj is an Object, I can't cast obj as Foo because I don't know what T is.

I was hoping to use reflection for this, but each time I do GetProperty("Items") it returns null. However, if someone knows a good way to do this without reflection, by all means.

Let's say my code looks like this:

//just to demonstrate where this comes from Foo<int> fooObject = new Foo<int>(); fooObject.Items = someList; object obj = (object)fooObject;  //now trying to get the Item value back from obj //assume I have no idea what <T> is PropertyInfo propInfo = obj.GetType().GetProperty("Items"); //this returns null object itemValue = propInfo.GetValue(obj, null); //and this breaks because it's null 
like image 368
gunr2171 Avatar asked Jan 02 '13 21:01

gunr2171


People also ask

How do you find the property of a generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

Can generic class inherit?

An attribute cannot inherit from a generic class, nor can a generic class inherit from an attribute.

What is generic in C# with example?

Generic Class T is called type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the DataStore class. For example, Data is generic property because we have used a type parameter T as its type instead of the specific data type.

What is a generic class C#?

Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.


1 Answers

You should be able to use:

Type t = obj.GetType();  PropertyInfo prop = t.GetProperty("Items");  object list = prop.GetValue(obj); 

You will not be able to cast as a List<T> directly, of course, as you don't know the type T, but you should still be able to get the value of Items.


Edit:

The following is a complete example, to demonstrate this working:

// Define other methods and classes here class Foo<T> {     public List<T> Items { get; set; } }  class Program {     void Main()     {            //just to demonstrate where this comes from         Foo<int> fooObject = new Foo<int>();         fooObject.Items = new List<int> { 1, 2, 3};         object obj = (object)fooObject;          //now trying to get the Item value back from obj         //assume I have no idea what <T> is         PropertyInfo propInfo = obj.GetType().GetProperty("Items"); //this returns null         object itemValue = propInfo.GetValue(obj, null);          Console.WriteLine(itemValue);                     // Does not print out NULL - prints out System.Collections.Generic.List`1[System.Int32]           IList values = (IList)itemValue;         foreach(var val in values)             Console.WriteLine(val); // Writes out values appropriately     } } 
like image 56
Reed Copsey Avatar answered Sep 19 '22 15:09

Reed Copsey