Reputation: 21742
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
Upvotes: 1
Views: 416
Reputation: 46098
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<>)
Upvotes: 1
Reputation: 20121
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");
Upvotes: 5