Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generic Type Parameters

Posted on 2009-05-15
5
Medium Priority
?
225 Views
Last Modified: 2012-05-07
As an example, if I inspect an object the following type:

public class LookupList<K, T> : IList<T>, IDictionary<K, T>

How would I look up the type corresponding to the generic argument for IList<>? In this case just retrieving GetGenericArguments will return an array of the two types K and T, and it's unclear which is the argument to IList. So what I'd really like to be able to do is test the object to see if it implements IList<> ( i.e. typeof(IList<>).IsInstanceOfType(myObject) ), and then if so retrieve just the IList<> type so I can then retrieve the generic type.

Is that clear? What's the right call?

Thanks in advance!

0
Comment
Question by:ebyrne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24396073
It's really not a good idea to implement both of these interfaces, as they will surely conflict with each other (even though it is possible...). Why implement both, anyway? Due to polymorphism, I'm not sure how to do this--or why it would even be useful.
0
 
LVL 2

Author Comment

by:ebyrne
ID: 24396104
And I suppose I could call GetInterfaces() in this case and iterate through them testing each for it's GenericTypeDefinition, but it seems to me there should be a more direct way (and/or a way that would work with all base classes/interfaces). Is there?
0
 
LVL 2

Author Comment

by:ebyrne
ID: 24396142
tculler,

Thanks for responding, but that's not really an answer or even an attempt. :) To respond though, it seems perfectly reasonable to me. In this case the class is a list that provides the lookup functionality of a dictionary and the normal iteration functionality of a list (and it was just an example I cooked up, not an actual class)...

Thanks for stopping in though!
0
 
LVL 2

Author Comment

by:ebyrne
ID: 24396251
And he does bring up a good point, perhaps it's not an ideal example, but really all I'm getting at is that the class implements several generic interfaces with varying generic parameters. I know what interface I'm interested in, I just want to know how to get the parameter for that interface/class...
0
 
LVL 2

Accepted Solution

by:
ebyrne earned 0 total points
ID: 24396310
Okay, so this is the best I've got so far, supporting only interfaces and a single argument. If you can either provide the more direct route I'm looking for or generalize this to classes and multiple arguments the points are yours.

Thanks Experts!
        static public Type GetGenericArgument(Type type, Type genericTypeDefinition)
        {
            if (type == null || genericTypeDefinition == null)
            {
                throw new ArgumentNullException();
            }
            if (!genericTypeDefinition.IsGenericTypeDefinition)
            {
                throw new ArgumentException("The generic type must be a generic type definition");
            }
            if (!genericTypeDefinition.IsInterface || genericTypeDefinition.GetGenericArguments().Length != 1)
            {
                throw new NotImplementedException("Must be a single argument interface type currently!");
            }
 
            var baseTypes = type.GetInterfaces();
 
            foreach (Type t in baseTypes)
            {
                if (t.IsGenericType && t.GetGenericTypeDefinition() == genericTypeDefinition)
                {
                    var args = t.GetGenericArguments();
 
                    if (args.Length > 0)
                    {
                        return args[0];
                    }
                }
            }
 
            return null;
        }
 
Usage:
        type = GetGenericArgument(dataSource.GetType(), typeof(IEnumerable<>));

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question