Reflection with Generic Collection Types

  • Thread starter Fernando Arámburu
  • Start date
F

Fernando Arámburu

hi everybody,

I´m working on a very strange dotnet Reflection component and i´m having
some troubles with reflection on generic types. To talk more concretely I
will gvie you some examples of what i´m looking at.

The method where I´m having some troubles needs to do get the generic
collection type from a generic collection variable. Examples (Please forget
about namespaces, í´m not writing them just to be more clear)

I have My Method
should return
List<int> List< >
type instance
Dictionary<string, object> Dictionary<> type
instance

By now I know how to get the parameter types (like int in the first
example and string and object in the second one) and how to get something
like List'1<int> or Dictionary'2<string, object> and I know I can parse the
type name as a string and then everything is fixed but I think there is some
whay to get what i´m looking at by using Reflection library. Can someone
give me a hand?

Thanks in advance.

Fernando
 
P

Peter Morris

í´m not writing them just to be more clear

I believe your attempt at being clear failed, I have no idea what the
following text means:
 
F

Fernando Arámburu

"I have" means the parameter I will pass to my method.
"My method should return" means what i´m expecting the method to return

So... first example is:
Type aGenericCollectionType = MyClass.MyMethod( typeof(List<int> ) );
Assert.AreEqual(aGenericCollectionType , typeof(List< >));

second example is:
Type aGenericCollectionType = MyClass.MyMethod( typeof(Dictionary<string,
object> ) );
Assert.AreEqual(aGenericCollectionType , typeof(Dictionary<>));

Hope I´m being clear now.

Thanks in advance.

Fernando
 
J

Jeff Johnson

I´m working on a very strange dotnet Reflection component and i´m
having some troubles with reflection on generic types. To talk more
concretely I will gvie you some examples of what i´m looking at.

The method where I´m having some troubles needs to do get the generic
collection type from a generic collection variable. Examples (Please
forget about namespaces, í´m not writing them just to be more clear)

I have My Method
should return
List<int> List< >
type instance
Dictionary<string, object> Dictionary<> type
instance

By now I know how to get the parameter types (like int in the first
example and string and object in the second one) and how to get something
like List'1<int> or Dictionary'2<string, object> and I know I can parse
the type name as a string and then everything is fixed but I think there
is some whay to get what i´m looking at by using Reflection library. Can
someone give me a hand?

So are you saying that you're trying to IGNORE the generic parameters and
just focus on whether you have a List<> or a Dictionary<> (or a
Collection<>, etc.)?
 
F

Fernando Arámburu

Something like that. By getting the List<> or Dictionary<> I can then create
generic classes by reflection with another method by doing
CreateGenericInstance(typeof(List<>), typeof(int)) or
CreateGenericInstance(typeof(Dictionary<>), typeof(string), typeof(object))
 

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