开发者

Type.GetType return null [duplicate]

This question already has answers here: Type.GetType("namespace.a.b.ClassName") returns null (17 answers) Closed 7 years ago.

I am trying to use Type.GetType and pass "caLibClient.en开发者_JAVA技巧tity.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?


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.


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


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

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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜