473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 2280
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.