Type.GetType(“namespace.a.b.ClassName”) returns null

前端 未结 16 1688
旧巷少年郎
旧巷少年郎 2020-11-22 02:58

This code:

Type.GetType(\"namespace.a.b.ClassName\")

returns null.

and I have in the usings:

using nam         


        
16条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 03:30

    This solution above seems to be the best to me, but it didn't work for me, so I did it as follows:

    AssemblyName assemblyName = AssemblyName.GetAssemblyName(HttpContext.Current.Server.MapPath("~\\Bin\\AnotherAssembly.dll"));
    string typeAssemblyQualifiedName = string.Join(", ", "MyNamespace.MyType", assemblyName.FullName);
    
    Type myType = Type.GetType(typeAssemblyQualifiedName);
    

    The precondition is that you know the path of the assembly. In my case I know it because this is an assembly built from another internal project and its included in our project's bin folder.

    In case it matters I am using Visual Studio 2013, my target .NET is 4.0. This is an ASP.NET project, so I am getting absolute path via HttpContext. However, absolute path is not a requirement as it seems from MSDN on AssemblyQualifiedNames

提交回复
热议问题