Lisa
Lisa

Reputation: 3181

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

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?

Upvotes: 4

Views: 14927

Answers (7)

Dave
Dave

Reputation: 945

A more reliable way to do this might be to use Activator.CreateInstance and pass in the type directly.

Upvotes: 0

Mark Jones
Mark Jones

Reputation: 2064

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. I guess the .NET folks thought you would never need to do such a thing, which was a very wrong assumption.

The Assembly.CreateInstance call must be made from a class residing outside the assembly that contains the object-type you are trying to create.

Go figure.

Upvotes: -1

Marc Gravell
Marc Gravell

Reputation: 1063338

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.

Upvotes: 7

Randy Levy
Randy Levy

Reputation: 22655

You can try troubleshooting your issue by using Assembly Binding Log Viewer (fuslogvw) to try to see if there are any failed binds.

Upvotes: 0

cristobalito
cristobalito

Reputation: 4282

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.

Upvotes: 0

Oded
Oded

Reputation: 499132

The function returns a null if it cannot find the type specified or if the type does not have a default constructor. See the documentation on 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.

Upvotes: 4

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

Please see the documentation:

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. If you post your code perhaps we can be more specific!

Upvotes: 3

Related Questions