Unable to cast an object created by Activator.CreateInstance.

Hi there,

I'm having trouble creating objects using the Activator object.

I'm calling it this way:

public object DynamicRetrieval(string classname, string asmPath){
    Assembly asm = Assembly.LoadFrom(asmPath);
    Type dynType = asm.GetType(classname, true, false);
    return = ActivatorCreateInstance(dynType);

In another method (& dll) I call:

MyObj myObj = server.DynamicRetrieval("MyObj", "MyObj.dll") as MyObj;

But this always throws an 'InvalidCastException'.
If I place a break point on the line and see what the type of the object returned is, it tells me it is a 'MyObj' object.

I can't see how, if it knows it is a 'MyObj' it can't be cast to it?

The 'DynamicRetreival' method is in a 'Server.dll' that is referenced by 'MyObj.dll'. MyObj.dll contains two classes:
MyObj and Runner.
If you create a new instance of 'Runner' it will attempt to retrieve a new instance of a 'MyObj' class from the Server.

Um, hope you can help!


Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

gregoryyoungConnect With a Mentor Commented:
make sure they are referencing the same version of the assembly ...
WinterMuteUKAuthor Commented:
Yup, the path given is the same path as the build...
Can you post your *actual* DynamicRetrieval method?
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

WinterMuteUKAuthor Commented:
Yup, here it is!

public object DynamicRetreival(XmlNode node)
      /* Retrieve the data needed... */
      string rNamespace      = node.Attributes.GetNamedItem("namespace").Value;
      string rClassname      = node.Attributes.GetNamedItem("classname").Value;
      string rPath      = node.Attributes.GetNamedItem("path").Value;
      if(rNamespace.Length > 0)
            rClassname = rNamespace + "." + rClassname;

      Assembly asm = Assembly.LoadFrom(rPath);
      Type dynType = asm.GetType(rClassname, true, false);
      object dynObj = Activator.CreateInstance(dynType);
      return dynObj;

The xml is:

<Invoke namespace='Wint' classname='MyObj' path='MyObj01.dll' />

WinterMuteUKAuthor Commented:

I've been messing with the location of the MyObj.dll, and now it seems to work:

Originally I had the path of the dll pointing to the build location of the 'MyObj' project (so c:\x\MyObj\bin\debug\MyObj.dll).
I changed the path to the location of the running code (c:\x\Runner\bin\debug\MyObj.dll) and it works, I checked both versions of the files and they are exactly the same (apparently)...

Seeing as this sort of answered the question, and I can only assume it must be some sort of versioning problem, then the questions now are:
Is there any reason for this? Are the files actually different even though the version nums are the same?
yeah an object out of 1 dll is not the same as an oject from a dll
now when you say the same are they the same including build number ?
WinterMuteUKAuthor Commented:
Yes both files were:, which is why I couldn't see why it wasn't working....
uhm... doesn't Activator.CreateInstance return an ObjectHandle?
if so, you'll have to unwrap it first and cast the unwrapped Object.

WinterMuteUKAuthor Commented:
Depends on which version of CreateInstance you use.
If as i'm doing you pass a 'Type' it will return an object...

WinterMuteUKAuthor Commented:
Gregory's answer was correct, but I still don't know why or how the files differed with exactly the same version nums. At least it works, and that's what counts!

Thanks guys

All Courses

From novice to tech pro — start learning today.