472,952 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

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
4 2258
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul Welter | last post by:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a...
2
by: D2 | last post by:
Hi, I have a requirement where I need to call a generic method without knowing the class name i.e. I'm getting class name from xml file. Given below is a replica of the scenario we are having. ...
3
by: Tao | last post by:
hi.. group I have a question about generic fucntion. Let us say, I have a function F<T>(int a); can I do something like: Type type = obj.GetType(); F<type>(3);
1
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList,...
5
by: Anders Borum | last post by:
Hi! While implementing a property manager (that supports key / value pairs), I was wondering how to constrain T to a struct or string type. Basically, I guess what I'm looking for is the common...
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
7
by: Henri.Chinasque | last post by:
Hi all, I've tried searching for this one, but can't seem to come up with the proper terms. I want to know how to pass a type to a generic method. Something like this: public void...
3
by: Anders Borum | last post by:
Hi I need to invoke a generic method determined at runtime. The method has two arguments, a string and a generic type that is constrained to a struct: public void Add<T>(string key, T value)...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.