exception when cast the reflection created object.

M

mark

hi, I get the problem here, I have exception thrown when I try to cast
the reflection created object.
first there is BaseObject dll that implemation a interface, factory
dll use the reflection load the dll, create the object and return.
BaseObject reference the factory dll.

factory.dll
public object CreateObj(string objectType, ref string suppMsg)
{
string LoadObj = string.Format("WitsmlObjects.{0}",objectType);
witsObject = WitsmlObjectsDll.CreateInstance(LoadObj);
}

test code:
Factory fac = new Factory();
object s = fac.CreateObj("baseObject",ref suppMsg);
// if check the type both are not equal.

if(s.GetType()== typeof(baseObject);)
{
.....
}
// this fail, but would be ok to
(Object)fac.CreateObj(strObj,ref suppMsg). if cast objec ot baseobject
fail.
obj = (BaseObject)fac.CreateObj(strObj,ref suppMsg);
 
E

Eric Gunnerson [MS]

Mark,

There are two usual causes of this:

1) The interface that's used on both sides must be the same interface in the
same assembly (ie the add-in must reference the interface in the main
assembly). If not, the interface will have the same name, but since it's
from a different assembly, it's the same type.

2) The interfaces must match on version. If you recompile the base assembly
but not the add-in, they end up with different version numbers and are
therefore not the same type.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top