471,326 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Verifying parent type on generic object

If I had the following variables defined:

System.Collections.Generic.BindingList<String> obj1;
System.Collections.Generic.BindingList<Int32> obj2;

And then I wanted to tell if an object was any type of generic
BindingList what would the syntax be? I can get it to work with
non-generic types, but can't seem to get the statement to return back
true for both obj1 and obj2. Something like the following doesn't work:

if (obj1 is System.Collections.Generic.BindingList<object>) ...

Thanks.

Feb 8 '06 #1
3 2256
>if (obj1 is System.Collections.Generic.BindingList<object>) ...

Try this

Type t = obj1.GetType();
if (t.IsGenericType && t.GetGenericTypeDefinition() ==
typeof(BindingList<>))
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 8 '06 #2
That worked great. Thank you. Now that I know the object is a type of
BindingList is there a way to write a generic routine to use an
enumerator to loop through whatever type of BindingList was found
regardless of the generic parameter type? Casting it something like:

System.Collections.Generic.BindingList<object> list =
(System.Collections.Generic.BindingList<object>)ob j;

doesn't work. Thanks again.

Kevin

Feb 8 '06 #3
>Now that I know the object is a type of
BindingList is there a way to write a generic routine to use an
enumerator to loop through whatever type of BindingList was found
regardless of the generic parameter type?


Sure, BingingList implements IEnumerable so you can use a foreach
loop.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Suzanne Vogel | last post: by
9 posts views Thread by Martin Herbert Dietze | last post: by
1 post views Thread by koch.henning | last post: by
4 posts views Thread by Charles Churchill | last post: by
7 posts views Thread by S. Lorétan | last post: by
reply views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by rosydwin | last post: by

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.