473,386 Members | 1,828 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,386 software developers and data experts.

Generics & reflection

Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine>
InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by
reflection that 'InvoiceLines' is a 'System.Collection.Generic.IList'.

When performing something like: 'if
(typeof(IList<>).IsAssignableFrom(propertyInfo.Typ e))' where 'propertyInfo'
obviously refers to the 'InvoiceLines', the result is always 'false' because
indeed 'IList<object>' and 'IMyColl<IInvoiceLine>' are not type-related.

So, I would like something that restricts the checking to the 'list' part of
the type 'IMyColl<IInvoiceLine>', but cannot find it when exploring the
different properties of 'propertyInfo.Type'.
--
Thank you, Marc
Nov 17 '05 #1
2 2851
Reflection doesn't provide an ImplementsInterface-type method that I'm aware
of. It might in 2.0. For 1.1 I would use the following code:

Type ifcType =
propertyInfo.Type.GetInterface("System.Collection. Generic.IList") or of
course you can always use the "is" operator if you have the actual property
object.

You could also use FindInterfaces but I think that it is overkill.

Don't know if that helps any.

Michael Taylor, MCP, MCAD - 6/23/05

"Marc" wrote:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine>
InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by
reflection that 'InvoiceLines' is a 'System.Collection.Generic.IList'.

When performing something like: 'if
(typeof(IList<>).IsAssignableFrom(propertyInfo.Typ e))' where 'propertyInfo'
obviously refers to the 'InvoiceLines', the result is always 'false' because
indeed 'IList<object>' and 'IMyColl<IInvoiceLine>' are not type-related.

So, I would like something that restricts the checking to the 'list' part of
the type 'IMyColl<IInvoiceLine>', but cannot find it when exploring the
different properties of 'propertyInfo.Type'.
--
Thank you, Marc

Nov 17 '05 #2
Dear Michael, thank you.

1. question was asked for .NET 2.0 only (supports generics)
2. correction in my example: 'propertyInfo.Type' should have read
'propertyInfo.PropertyType'
3.1 result of trying your suggestion:
object o11 =
propertyInfo.PropertyType.GetInterface("System.Col lections.IList");
object o12 =
propertyInfo.PropertyType.GetInterface("System.Col lections.ICollection");
object o13 =
propertyInfo.PropertyType.GetInterface("System.Col lections.IEnumerable");
object o21 =
propertyInfo.PropertyType.GetInterface("System.Col lections.Generic.IList<>");
object o22 =
propertyInfo.PropertyType.GetInterface("System.Col lections.Generic.ICollection<>");
object o23 =
propertyInfo.PropertyType.GetInterface("System.Col lections.Generic.IEnumerable<>");
is: only 'o13' is not null.
3.2 this is indeed the correct outcome and confirms the absence of solution,
so far.
4.1 result of 'Type type in propertyInfo.PropertyType.GetInterfaces()':
System.Collections.Generic.IList`1[Xyz.Domn.Path.ProgDataT1+IInvoiceLines]

System.Collections.Generic.ICollection`1[Xyz.Domn.Path.ProgDataT1+IInvoiceLines]

System.Collections.Generic.IEnumerable`1[Xyz.Domn.Path.ProgDataT1+IInvoiceLines]
System.Collections.IEnumerable
4.2 this is indeed 'overkill', plus i find it a little akward to decode a
string representation "[...]`1[...]" to conclude that it contains something
like 'System.Collections.Generic.IList'; if i try to get the type of that
string, it would give me the type object but i am then probably back to
square one in testing for it being assignable from IList<>.
--
Thank you, Marc
"MTaylor" wrote:
Reflection doesn't provide an ImplementsInterface-type method that I'm aware
of. It might in 2.0. For 1.1 I would use the following code:

Type ifcType =
propertyInfo.Type.GetInterface("System.Collection. Generic.IList") or of
course you can always use the "is" operator if you have the actual property
object.

You could also use FindInterfaces but I think that it is overkill.

Don't know if that helps any.

Michael Taylor, MCP, MCAD - 6/23/05

"Marc" wrote:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine>
InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by
reflection that 'InvoiceLines' is a 'System.Collection.Generic.IList'.

When performing something like: 'if
(typeof(IList<>).IsAssignableFrom(propertyInfo.Typ e))' where 'propertyInfo'
obviously refers to the 'InvoiceLines', the result is always 'false' because
indeed 'IList<object>' and 'IMyColl<IInvoiceLine>' are not type-related.

So, I would like something that restricts the checking to the 'list' part of
the type 'IMyColl<IInvoiceLine>', but cannot find it when exploring the
different properties of 'propertyInfo.Type'.
--
Thank you, Marc

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. ...
7
by: Gene Vital | last post by:
Hi all, I need some help in understanding how to use Generics. I have a class based on a user control that can be put on any Container at runtime, I want to be able to call a method on the...
4
by: Dov Tendler | last post by:
Hi All, I would like to get MethodInfo of current instantiated generic method call. Consider the following function call: C.f<int>(5); Where f is defined as follows: class C {
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
8
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.