Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I am loading a class from an assembly and trying to instantiate and instance.

Here's what I have

C#
public interface IActivity

C#
public abstract class BaseActivity:IActivity

C#
public class MyActivity:BaseActivity //I don't really call stuff MyXxx

And to load it:
C#
Type type = validIActivityTypes.Single(); //I've already performed my checks for this
ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
   return result;
IActivity activity = (IActivity)constructor.Invoke(null);


At this point, the 'type' is 'MyActivity' but...

Boom:
Quote:
A first chance exception of type 'System.InvalidCastException' occurred in ServerControls.dll

Additional information: Unable to cast object of type 'WorkFlowEngine.Activities.MyActivity' to type 'WorkFlowEngine.Interfaces.IActivity'.


What am I doing wrong?

Thanks

Edit: Just tested type.GetInterfaces():

>type.GetInterfaces() | {System.Type[1]}
>[0] | {Name = "IActivity" FullName = "WorkFlowEngine.Interfaces.IActivity"}
Posted
Updated 1-May-15 1:31am
v4
Comments
BillWoodruff 1-May-15 7:33am    
I'm puzzled by what you are doing with 'result: where is that declared, what is it ?
Andy Lanng 1-May-15 8:52am    
result is just the return of the method. At this stage it's a string[0].
If the method was void, that line would just be 'return;'
Alan N 1-May-15 8:11am    
Have you declared WorkFlowEngine.Interfaces.IActivity in more than one assembly? Although if you have, there should be a compiler warning on the line where the cast occurs.
Andy Lanng 1-May-15 8:53am    
Nope - my namespaces are very clean. + I don't often use interfaces unless I need to do this sort of casting.
Richard Deeming 1-May-15 8:12am    
Just tried your code in LINQPad, and it works fine. Are you sure you don't have two different versions of the interface?

1 solution

Ok - i was doing it wrong:

My activity type lived in the same assembly as the IActivity interface.

When you load an assembly, all related assemblies will also be loaded into the AppDomain unless it has already been loaded into the AppDomain .

If you load an assembly through reflection then it will still be loaded and will not be the same as the referenced assembly.

So essentially I did have two versions of the IActivity loaded :(


The solution is simple. I have moved my interface declarations into a difference assembly (project) which is referenced by both the app and the loaded dll. This means that the loaded dll will use the pre-loaded interface assembly instead of it's own copy.

Thus the issue (highlighted by Richard Deeming at 1 sec ago) is solved

Thanks for looking into this, guys ^_^
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900