467,888 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

Generic list and MethodInfo

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.GetGenericArgum ents()).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
Jun 27 '08 #1
  • viewed: 2012
Share:
4 Replies
It could, but why go through all that trouble? If it has a property
then you should create an interface that exposes that property, implement it
on the types (the interface can be generic) and then set up the type T to
have a constraint where it implements that interface. That way, you don't
have to use reflection at all.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ta*****@yahoo.comwrote in message
news:4e**********************************@m36g2000 hse.googlegroups.com...
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.GetGenericArgum ents()).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

Jun 27 '08 #2
On Jun 3, 12:47*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
* * It could, but why go through all that trouble? *If it has a property
then you should create an interface that exposes that property, implement it
on the types (the interface can be generic) and then set up the type T to
have a constraint where it implements that interface. *That way, you don't
have to use reflection at all.

--
* * * * * - Nicholas Paldino [.NET/C# MVP]
* * * * * - m...@spam.guard.caspershouse.com

<tadm...@yahoo.comwrote in message

news:4e**********************************@m36g2000 hse.googlegroups.com...
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.GetGenericArgum ents()).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- Hide quoted text -

- Show quoted text -
Although my understanding of OOP is somewhat new, I do have a general
idea of the purpose of interfaces. Maybe if I include the calling
code it'd make more sense: (kinda long to follow)

public class TEmployees
{
private long empID;
private string ssn;
private byte[] photo;
private DataList<TTaskstheTasks;

private DateTime lastUpdate;

public long EmployeeID
{
get { return empID; }
set
{
if (this.empID != value)
{this.empID = value; }
}
}
public string SSN
{
get { return ssn; }
set
{
if (this.ssn != value)
{ this.ssn = value; }
}
}
public byte[] Photograph
{
get { return photo; }
set
{
if (this.photo != value)
{ this.photo = value; }
}
}
public DataList<TTasksTheTasks //another custom class called
TTasks
{
get {return theTasks;}
set { this.theTasks = value; }
}
}

Then, in the form, I have:

DataList<TEmployeesEmps = new DataList<TEmployees>();

There are other properties and methods that setup the database call
and table names, but basically, I want to be able to pass a class to
DataList, and have it auto-detect a property that is also a DataList,
and create it.

That may not be kosher object orientation, but I was thinking it could
be done through reflection, and I'm not sure how it could be done
through an interface, because then I'd have to say, "this interface
has this property that is DataList<T>". Also, TTasks may have its own
property that is also a type of DataList<T>.

Does this make any more sense?
Jun 27 '08 #3
On Jun 3, 2:25*pm, tadm...@yahoo.com wrote:
On Jun 3, 12:47*pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
* * It could, but why go through all that trouble? *If it has a property
then you should create an interface that exposes that property, implement it
on the types (the interface can be generic) and then set up the type T to
have a constraint where it implements that interface. *That way, you don't
have to use reflection at all.
--
* * * * * - Nicholas Paldino [.NET/C# MVP]
* * * * * - m...@spam.guard.caspershouse.com
<tadm...@yahoo.comwrote in message
news:4e**********************************@m36g2000 hse.googlegroups.com...
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.GetGenericArgum ents()).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- Hide quoted text -
- Show quoted text -

Although my understanding of OOP is somewhat new, I do have a general
idea of the purpose of interfaces. *Maybe if I include the calling
code it'd make more sense: (kinda long to follow)

* *public class TEmployees
* *{
* * private long empID;
* * private string ssn;
* * private byte[] photo;
* * private DataList<TTaskstheTasks;

* * private DateTime lastUpdate;

* * public long EmployeeID
* * {
* * *get *{ return empID; }
* * *set
* * *{
* * * *if (this.empID != value)
* * * * {this.empID = value; }
* * *}
* * }
* * public string SSN
* * {
* * *get { return ssn; }
* * *set
* * *{
* * * * if (this.ssn != value)
* * * * { this.ssn = value; }
* * *}
* * }
* * public byte[] Photograph
* * * {
* * * * *get { *return photo; }
* * * * *set
* * * * *{
* * * * * * if (this.photo != value)
* * * * * * { this.photo = value; }
* * * * *}
* * * }
* * *public DataList<TTasksTheTasks *//another custom class called
TTasks
* * {
* * * get {return theTasks;}
* * * set { this.theTasks = value; }
* * }
* }

Then, in the form, I have:

DataList<TEmployeesEmps = new DataList<TEmployees>();

There are other properties and methods that setup the database call
and table names, but basically, I want to be able to pass a class to
DataList, and have it auto-detect a property that is also a DataList,
and create it.

That may not be kosher object orientation, but I was thinking it could
be done through reflection, and I'm not sure how it could be done
through an interface, because then I'd have to say, "this interface
has this property that is DataList<T>". *Also, TTasks may have its own
property that is also a type of DataList<T>.

Does this make any more sense?- Hide quoted text -

- Show quoted text -
Nicholas, you mentioned that the idea could work - would you mind
describing, or giving a quick rundown of how it could be done?
Jun 27 '08 #4
On Jun 3, 2:25*pm, tadm...@yahoo.com wrote:
On Jun 3, 12:47*pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
* * It could, but why go through all that trouble? *If it has a property
then you should create an interface that exposes that property, implement it
on the types (the interface can be generic) and then set up the type T to
have a constraint where it implements that interface. *That way, you don't
have to use reflection at all.
--
* * * * * - Nicholas Paldino [.NET/C# MVP]
* * * * * - m...@spam.guard.caspershouse.com
<tadm...@yahoo.comwrote in message
news:4e**********************************@m36g2000 hse.googlegroups.com...
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.GetGenericArgum ents()).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- Hide quoted text -
- Show quoted text -

Although my understanding of OOP is somewhat new, I do have a general
idea of the purpose of interfaces. *Maybe if I include the calling
code it'd make more sense: (kinda long to follow)

* *public class TEmployees
* *{
* * private long empID;
* * private string ssn;
* * private byte[] photo;
* * private DataList<TTaskstheTasks;

* * private DateTime lastUpdate;

* * public long EmployeeID
* * {
* * *get *{ return empID; }
* * *set
* * *{
* * * *if (this.empID != value)
* * * * {this.empID = value; }
* * *}
* * }
* * public string SSN
* * {
* * *get { return ssn; }
* * *set
* * *{
* * * * if (this.ssn != value)
* * * * { this.ssn = value; }
* * *}
* * }
* * public byte[] Photograph
* * * {
* * * * *get { *return photo; }
* * * * *set
* * * * *{
* * * * * * if (this.photo != value)
* * * * * * { this.photo = value; }
* * * * *}
* * * }
* * *public DataList<TTasksTheTasks *//another custom class called
TTasks
* * {
* * * get {return theTasks;}
* * * set { this.theTasks = value; }
* * }
* }

Then, in the form, I have:

DataList<TEmployeesEmps = new DataList<TEmployees>();

There are other properties and methods that setup the database call
and table names, but basically, I want to be able to pass a class to
DataList, and have it auto-detect a property that is also a DataList,
and create it.

That may not be kosher object orientation, but I was thinking it could
be done through reflection, and I'm not sure how it could be done
through an interface, because then I'd have to say, "this interface
has this property that is DataList<T>". *Also, TTasks may have its own
property that is also a type of DataList<T>.

Does this make any more sense?- Hide quoted text -

- Show quoted text -
If anyone could point me in the right direction, it would be very much
appreciated. There's a piece missing, and I can't seem to find it.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Paul Welter | last post: by
2 posts views Thread by D2 | last post: by
3 posts views Thread by Tao | last post: by
7 posts views Thread by Henri.Chinasque | last post: by
reply views Thread by =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.