Type.GetType return null [duplicate]
Asked Answered
Y

4

13

I am trying to use Type.GetType and pass "caLibClient.entity.Web2ImageEntity" full class name. The caLibClient.entity is namespace, located in separated assembly (caLibClient) and added to program reference assemblies list. The Type.GetType always return Null when I call it from program, what is wrong?

Yawl answered 2/9, 2011 at 12:59 Comment(0)
P
25

You need to add the assembly name as well, since your type isn't in the executing assembly (nor mscorlib.) So the call should be:

var myType = Type.GetType("caLibClient.entity.Web2ImageEntity, FullAssemblyName");

From the Type.GetType() docs:

typeName
Type: System.String
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.

From the docs for AssemblyQualifiedName, this is a sample name:

TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089

Update: If you're already referencing the assembly in your project, and know at compile-time what the type-name is, you're better off saying

Type myType = typeof(caLibClient.entity.Web2ImageEntity);

since now you don't need to search for the type at run-time; the compiler will do everything for you.

Prefigure answered 2/9, 2011 at 13:3 Comment(2)
How to find full assembly name? Not sure if I understand correctly what does it mean. Where to look?Yawl
@Yawl What is the name of the assembly that you referenced?Prefigure
F
2

Try Type.GetType("caLibClient.entity.Web2ImageEntity, caLibClient"), according to Assembly qualified name

Felicidad answered 2/9, 2011 at 13:5 Comment(0)
N
1

You need to pass an assembly qualified name, in your case something like this:

var yourType = Type.GetType("caLibClient.entity.Web2ImageEntity,caLibClient");
Ninurta answered 2/9, 2011 at 13:4 Comment(0)
W
1

If you know a type in the assembly that the target type lives in you can avoid hard coding the full assembly qualified name. For example:

Type.GetType(
    "MyAssembly.Foo.BarSubclass, " + typeof(MyAssembly.Foo.IBar).Assembly.FullName)
Whore answered 4/6, 2014 at 23:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.