views:

243

answers:

2

I need to get the type object for IProcessor via reflection and according to documentation I need to do the following:

Type.GetType("IProcessor`1[System.Int32]")

However, that returns null. I then tried:

Type.GetType(typeof(IProcessor<int>).FullName)

and still got a null.

I am, however, capable of getting the Type argument with:

Type.GetType(typeof(IProcessor<int>).AssemblyQualifiedName)

But unfortunately I do not have version, culture or publickey available at the call site. Any suggestions on how to get the type object?

EDIT: below mentioned typos corrected EDIT: using the typeof operator in the code is not an option. I need to get the type object runtime from a string

+1  A: 

The typeof operator expects a type name, and returns the Type object directly. So, if you can reference the type in code, to get the type you only have to use

typeof(IProcessor<int>)

or, for the open generic version you an then construct a closed type using reflection

typeof(IProcessor<>)
thecoop
I need to get the type object from a string at runtime. The typeof above works fine but they were only a means of showing that with no spelling errors in the name I still got a null value. I admmit that's not very clear in the question
Rune FS
+4  A: 

You have to be a bit more careful with your transcription of the code you are asking about since the samples in your question have syntax errors in them; "FullName" and "AssemblyQualifiedName" need to be outside the bracket, not directly on the type.

As such, I'll assume that you meant:

Type.GetType("IProcessor`1[System.Int32]");

(note the backtick between IProcessor and 1, which is very important)

At the very least for this syntax to work, you have to include the full namespace that the type is in, so if IProcessor is in namespace MyApp.Interfaces for example, then the code should be:

Type.GetType("MyApp.Interfaces.IProcessor`1[System.Int32]");

Note however that this only works if the type you are referring to is inside the same assembly that you make this call from. If it is not, then at the very least you are going to have to add assembly names to the call, as such (assuming it is in an assembly named MyAssembly):

Type.GetType("MyApp.Interfaces.IProcessor`1[System.Int32], MyAssembly");
jerryjvl
Your right about the syntax errors, my bad. Ill have to try including the assembly name. However I will find it odd that you'd have to include the name of the assembly if it's a generic type. The code works fine for none generic types, using only FullName
Rune FS
Note that when I tested in an application, the second variant without the assembly name worked fine; that was only because the namespace and class occurred in the same assembly that I was making the GetType call from though.If the type you are resolving is not in the same assembly as the GetType call, then you will require an assembly name, otherwise you will not
jerryjvl
What puzzles me is the difference between generics and non generics, and it doesn't have to be in the same namespace/assembly but the the assembly holding needs to be loaded for it to work
Rune FS
I'm not sure what you mean by the 'difference between generics and non generics' ... the fact that the assembly holding the type needs to be loaded is not surprising, ... although it'd be interesting to see if a strongly named assembly when fully referenced gets loaded automatically or not (probably not would be my guess, but at that point it is at least technically possible).
jerryjvl