chugh97
chugh97

Reputation: 9992

convert a string to its type in .net

I have a string "System.Linq.IQueryable`1[System.String]", I want to convert this to the underlying .net type Type.GetType("System.Linq.IQueryable`1[System.String]") returns null. Can any one out help me on this.

Upvotes: 0

Views: 149

Answers (3)

keyboardP
keyboardP

Reputation: 69372

You need to explicitly include the assembly, version, and public key token (if it has one).

If you're running .NET 4, try this

Type t = Type.GetType("System.Linq.IQueryable`1[System.String], System.Core, "
     + "Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089");

The reason you don't always have to fully qualify the string is because the extra information can be omitted if the type is within the current assembly or in the Mscorlib assembly. From MSDN:

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Upvotes: 2

James
James

Reputation: 9985

I think the problem is that you are using Type.Name instead of Type.AssemblyQualifiedName.

Upvotes: 0

Denys Denysenko
Denys Denysenko

Reputation: 7904

Can you take a real non-abstract type signature? For example Type.GetType("System.Linq.EnumerableQuery`1[System.String]") should work for you.

Upvotes: 0

Related Questions