简体   繁体   English

assembly.CreateInstance返回null

[英]assembly.CreateInstance returns null

I'm working with assembly.CreateInstance, and it returns null, while it was fine using it with a different project with the same DLL file "assembly file", Can you please suggest reasons when and why it returns null?Please this is urgent?? 我正在使用assembly.CreateInstance,它返回null,虽然使用它与不同的项目使用相同的DLL文件“汇编文件”很好,你能告诉我何时以及为什么它返回null的原因?请这是紧急的??

Edit 编辑

The type I'm searching for has a default constructor, but it implements another interface, like this. 我正在搜索的类型有一个默认的构造函数,但它实现了另一个接口,就像这样。 Project1, has the interface A and makes the DLL which contains the new type let it be typeB which implements A. Project2, has the same interface A and use the "CreateInstance" method to locate the type typeB, but here the CreateInstance returns null, any suggestions? Project1,有接口A并使包含新类型的DLL让它成为实现A.Project2的typeB,具有相同的接口A并使用“CreateInstance”方法来定位类型typeB,但是这里CreateInstance返回null,有什么建议么?

I doubt it applies here, but there is 1 edge-case wher CreateInstance returns null (namely Nullable<T> ), and one extreme corner-case ( ProxyAttribute , discussed here ) where a regular class can construct to null . 怀疑它适用于此,但有一个边缘情况, CreateInstance返回null (即Nullable<T> ),以及一个极端的角落情况( ProxyAttribute ,在这里讨论),其中常规类可以构造为null

But more likely: 但更可能的是:

  • it doesn't exist (name wrong, perhaps) 它不存在(名称错误,也许)
  • you are using as , and the interface isn't implemented (perhaps the interface is declared in two different assemblies; it counts separately as different interfaces in each, even if the name and namespace are identical) 您正在使用as ,并且实现接口(可能接口在两个不同的程序集中声明;它们分别计为每个接口中的不同接口,即使名称和名称空间相同)

From the edit, it sounds like the last point; 从编辑中,它听起来像是最后一点; you should have the interface defined once only , and a reference between assemblies so that other assemblies can see (and implement) that interface. 您应该定义一次接口,并在程序集之间引用 ,以便其他程序集可以查看(并实现)该接口。

Please see the documentation: 请参阅文档:

http://msdn.microsoft.com/en-us/library/aa329906(v=VS.71).aspx http://msdn.microsoft.com/en-us/library/aa329906(v=VS.71).aspx

It is returning null because the type you are passing in is not found. 它返回null,因为找不到您传入的类型。 If you post your code perhaps we can be more specific! 如果您发布代码,我们可能会更具体!

The function returns a null if it cannot find the type specified or if the type does not have a default constructor. 如果函数找不到指定的类型或者类型没有默认构造函数,则该函数返回null See the documentation on MSDN. 请参阅MSDN上的文档

You need to make sure that your code is looking for the right assembly and type in the right place and that you have the appropriate permissions. 您需要确保您的代码正在寻找正确的程序集并键入正确的位置并且您具有适当的权限。

It just so happens that Assembly.CreateInstance will return null if it is called by a class that resides in the same assembly as the requested type. 恰好,如果由与所请求类型位于同一程序集中的类调用Assembly.CreateInstance,它将返回null。 I guess the .NET folks thought you would never need to do such a thing, which was a very wrong assumption. 我想.NET人认为你永远不需要做这样的事情,这是一个非常错误的假设。

The Assembly.CreateInstance call must be made from a class residing outside the assembly that contains the object-type you are trying to create. Assembly.CreateInstance调用必须来自驻留在包含您尝试创建的对象类型的程序集外部的类。

Go figure. 去搞清楚。

更可靠的方法是使用Activator.CreateInstance并直接传入类型。

As a follow up to the other replies, it can also occur because dependent assembly cannot be found (or loaded). 作为其他回复的后续行动,它也可能发生,因为无法找到(或加载)依赖程序集。 Possible reasons include file not existing, different versions, strong name verification, permissions, etc. 可能的原因包括文件不存在,版本不同,强名称验证,权限等。

您可以尝试使用程序集绑定日志查看器 (fuslogvw)来尝试查看是否存在任何失败的绑定来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM