473,795 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetP roperties();

foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyT ype.IsGenericTy pe &&
pinn.PropertyTy pe.GetGenericTy peDefinition() == typeof(DataList <>))
{
// The property is a type of DataList<T>
MethodInfo mi = typeof(DataList <>).GetMethod(" TestGeneric");
Type listChildType = pinn.PropertyTy pe.GetGenericAr guments()[0];
// next 2 lines are not correct
object obj;

mi.MakeGenericM ethod(interface Type.GetGeneric Arguments()).In voke(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 2299
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.co m

<ta*****@yahoo. comwrote in message
news:4e******** *************** ***********@m36 g2000hse.google groups.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.GetP roperties();

foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyT ype.IsGenericTy pe &&
pinn.PropertyTy pe.GetGenericTy peDefinition() == typeof(DataList <>))
{
// The property is a type of DataList<T>
MethodInfo mi = typeof(DataList <>).GetMethod(" TestGeneric");
Type listChildType = pinn.PropertyTy pe.GetGenericAr guments()[0];
// next 2 lines are not correct
object obj;

mi.MakeGenericM ethod(interface Type.GetGeneric Arguments()).In voke(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.guar d.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.c om

<tadm...@yahoo. comwrote in message

news:4e******** *************** ***********@m36 g2000hse.google groups.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.GetP roperties();
foreach (PropertyInfo pinn in propInfos)
{
*if (pinn.PropertyT ype.IsGenericTy pe &&
pinn.PropertyTy pe.GetGenericTy peDefinition() == typeof(DataList <>))
*{
// The property is a type of DataList<T>
* MethodInfo mi = typeof(DataList <>).GetMethod(" TestGeneric");
* Type listChildType = pinn.PropertyTy pe.GetGenericAr guments()[0];
// next 2 lines are not correct
* object obj;
mi.MakeGenericM ethod(interface Type.GetGeneric Arguments()).In voke(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<TTasks theTasks;

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<TTasks TheTasks //another custom class called
TTasks
{
get {return theTasks;}
set { this.theTasks = value; }
}
}

Then, in the form, I have:

DataList<TEmplo yeesEmps = new DataList<TEmplo yees>();

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.c om wrote:
On Jun 3, 12:47*pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guar d.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.c om
<tadm...@yahoo. comwrote in message
news:4e******** *************** ***********@m36 g2000hse.google groups.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.GetP roperties();
foreach (PropertyInfo pinn in propInfos)
{
*if (pinn.PropertyT ype.IsGenericTy pe &&
pinn.PropertyTy pe.GetGenericTy peDefinition() == typeof(DataList <>))
*{
// The property is a type of DataList<T>
* MethodInfo mi = typeof(DataList <>).GetMethod(" TestGeneric");
* Type listChildType = pinn.PropertyTy pe.GetGenericAr guments()[0];
// next 2 lines are not correct
* object obj;
mi.MakeGenericM ethod(interface Type.GetGeneric Arguments()).In voke(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<TTasks theTasks;

* * 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<TTasks TheTasks *//another custom class called
TTasks
* * {
* * * get {return theTasks;}
* * * set { this.theTasks = value; }
* * }
* }

Then, in the form, I have:

DataList<TEmplo yeesEmps = new DataList<TEmplo yees>();

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.c om wrote:
On Jun 3, 12:47*pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guar d.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.c om
<tadm...@yahoo. comwrote in message
news:4e******** *************** ***********@m36 g2000hse.google groups.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.GetP roperties();
foreach (PropertyInfo pinn in propInfos)
{
*if (pinn.PropertyT ype.IsGenericTy pe &&
pinn.PropertyTy pe.GetGenericTy peDefinition() == typeof(DataList <>))
*{
// The property is a type of DataList<T>
* MethodInfo mi = typeof(DataList <>).GetMethod(" TestGeneric");
* Type listChildType = pinn.PropertyTy pe.GetGenericAr guments()[0];
// next 2 lines are not correct
* object obj;
mi.MakeGenericM ethod(interface Type.GetGeneric Arguments()).In voke(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<TTasks theTasks;

* * 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<TTasks TheTasks *//another custom class called
TTasks
* * {
* * * get {return theTasks;}
* * * set { this.theTasks = value; }
* * }
* }

Then, in the form, I have:

DataList<TEmplo yeesEmps = new DataList<TEmplo yees>();

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
17183
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 BindingFlag that will only get the Generic one? Here is an example ... public class UserTest { public ArrayList GetCollection() { return new ArrayList(); }
2
2327
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. The generic method shown is just to explain the scenario, actually its somewhere outside. private void Form1_Load(object sender, EventArgs e) {
3
11476
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
2785
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, List<VtheSecondList); I pass the name of the method, the arguments for the "GenericMethodWithGenericArguments" to another method, which is supposed to invoke this method using the Invoke method in the MethodInfo class. My process of invocation is as follows
5
4613
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 denominator for structs and strings and while looking through the SDK I only noticed the IEquatable<T> interface. So I implemented the class with methods such as the following, but I'm aware that this is not the right approach.
7
1751
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 propInfos; private Type objectType = typeof(T); public SomeClass()
7
3209
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 MyMethod(Type objType){ DoSomething<objType>();
3
2265
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) where T : struct The method is an instance member located on a class called CmsPropertyManager. I also have a number of other "Add" methods with different overloads.
0
2369
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 GetMethods() MethodInfo methods = t.GetMethods();
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.