Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assembly.CreateInstance returns null

Tags:

c#

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??

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?

like image 760
Lisa Avatar asked Dec 16 '22 23:12

Lisa


1 Answers

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.

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)

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.

like image 122
Marc Gravell Avatar answered Jan 03 '23 01:01

Marc Gravell