470,566 Members | 1,535 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

exception when cast the reflection created object.

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);
Nov 15 '05 #1
1 2962
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.
"mark" <ma*******@yahoo.com> wrote in message
news:42**************************@posting.google.c om...
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);

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by guy | last post: by
5 posts views Thread by Nick Flandry | last post: by
6 posts views Thread by Steve Long | last post: by
reply views Thread by Ben Crinion | last post: by
11 posts views Thread by Dinsdale | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.