Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unable to cast an object created by Activator.CreateInstance.

Posted on 2004-09-07
11
Medium Priority
?
1,352 Views
Last Modified: 2012-06-27
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!

Thanks

Wint.
0
Comment
Question by:WinterMuteUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 1500 total points
ID: 11997769
make sure they are referencing the same version of the assembly ...
0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11997816
Yup, the path given is the same path as the build...
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11997817
Can you post your *actual* DynamicRetrieval method?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11997899
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' />

0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 11998068
Ahem,

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?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12000744
yeah an object out of 1 dll 1.1.2.1001 is not the same as an oject from a dll 1.1.2.1002
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12000749
now when you say the same are they the same including build number ?
0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 12004012
Yes both files were: 1.7.3.1338, which is why I couldn't see why it wasn't working....
0
 
LVL 3

Expert Comment

by:PocketMatt
ID: 12004373
uhm... doesn't Activator.CreateInstance return an ObjectHandle?
if so, you'll have to unwrap it first and cast the unwrapped Object.

cheers
0
 
LVL 9

Author Comment

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

Wint....
0
 
LVL 9

Author Comment

by:WinterMuteUK
ID: 12006463
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

Wint.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question