T
tadmill
Hi,
Is it possible for a generic list class to use Reflection on the
generic type being used to detect a property within that type that
refers to the same generic class, only with a different type as the
parameter?
From various posts and help, this is what I've got so far:
public class DataList<T> : BindingList<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(DataList<>))
{
// The property is a type of DataList<T>
MethodInfo mi = typeof(DataList<>).GetMethod("TestGeneric");
Type listChildType = pinn.PropertyType.GetGenericArguments()[0];
// next 2 lines are not correct
object obj;
mi.MakeGenericMethod(interfaceType.GetGenericArguments()).Invoke(null,new
object[] { obj });
}
}
}
public void TestGeneric<U>(U thisVar) //no idea if this is needed or
what to put in it
{}
What I'm trying to do is emulate a query structure; basically, a class
represents a table, and a class can have a property that is a
collection of another class, a basic one-to-many, represented by the
generic list class DataList<T>.
Any chance that this idea could work? Any helpful suggestions would
be much appreciated.
Terry
Is it possible for a generic list class to use Reflection on the
generic type being used to detect a property within that type that
refers to the same generic class, only with a different type as the
parameter?
From various posts and help, this is what I've got so far:
public class DataList<T> : BindingList<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(DataList<>))
{
// The property is a type of DataList<T>
MethodInfo mi = typeof(DataList<>).GetMethod("TestGeneric");
Type listChildType = pinn.PropertyType.GetGenericArguments()[0];
// next 2 lines are not correct
object obj;
mi.MakeGenericMethod(interfaceType.GetGenericArguments()).Invoke(null,new
object[] { obj });
}
}
}
public void TestGeneric<U>(U thisVar) //no idea if this is needed or
what to put in it
{}
What I'm trying to do is emulate a query structure; basically, a class
represents a table, and a class can have a property that is a
collection of another class, a basic one-to-many, represented by the
generic list class DataList<T>.
Any chance that this idea could work? Any helpful suggestions would
be much appreciated.
Terry