Generic list and MethodInfo

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
T

tadmill

    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]
          - (e-mail address removed)




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- 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<TEmployees> Emps = 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?
 
T

tadmill

    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.
news:4eb9323b-137c-45cc-903e-83d7f31e2e43@m36g2000hse.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.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- 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<TEmployees> Emps = 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?
 
T

tadmill

    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.
news:4eb9323b-137c-45cc-903e-83d7f31e2e43@m36g2000hse.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.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- 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<TEmployees> Emps = 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top