[Q] Cast remote object to a local object

G

Guest

hi. guys,

I have two AppDomains, AppDomain0 and AppDomain1. In AppDomain1, I got a
class implements an interface IInterface0. In AppDomain0, I also define a
IInterface0, the code of two interfaces are exactly same.

When I call:

appDomain.CreateInstanceFromAndUnwrap and try to convert to
AppDomain0.IInterface0. I got an error.

I was wondering why? Their codes are exactly same. Is there any way that I
can do such cast?

I hope I explain this problem clearly.

thanks, guys
 
V

Vadym Stetsyak

Hello, Tao!

Its hard to tell the answer without additional information. Can
you post a small code sample, where you define
interfaces and try to convert them?

You wrote on Wed, 8 Nov 2006 10:54:02 -0800:

T> hi. guys,

T> I have two AppDomains, AppDomain0 and AppDomain1. In AppDomain1, I
T> got a
T> class implements an interface IInterface0. In AppDomain0, I also
T> define a
T> IInterface0, the code of two interfaces are exactly same.

T> When I call:

T> appDomain.CreateInstanceFromAndUnwrap and try to convert to
T> AppDomain0.IInterface0. I got an error.

T> I was wondering why? Their codes are exactly same. Is there any way
T> that I
T> can do such cast?

T> I hope I explain this problem clearly.

T> thanks, guys
 
G

Guest

Hi.. Vadym,

The code is something like:

In AppDomain0, I define:
public interface IA { void M(); }

In AppDomain1, there is a interface defined:
public interface IA {void M(); } //exactly same as IA in AppDomain0
public class A : IA {... } //class A implements IA
I compiled A.dll which contains IA and A.

The following program does:

0. launch the main program running in AppDomain0. Main program has a IA
interface defined.
1. create a new AppDomain, AppDomain1
2. load the A.dll into AppDomain1
3. Create a remote object and try to convert it to IA.

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = this.path;
setup.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory;
setup.ApplicationName = "A.dll";
setup.ShadowCopyFiles = "true";
setup.ShadowCopyDirectories = AppDomain.CurrentDomain.BaseDirectory;
AppDomain appDomain = AppDomain.CreateDomain("A", null, setup);
IA a = (IA)appDomain.CreateInstanceFromAndUnwrap(fileName, "A"); //Error!

Compile is fine. When I run the program, it complains about not being able
to cast a transparent proxy to type IA.

thanks again.
 
G

Guest

Thanks Vadym,

I figured out what's going on.

"It forms a type boundary. Every type's identity includes the name of the
assembly in which it resides. A type called MyType loaded in the scope of one
assembly is not the same as a type called MyType loaded in the scope of
another assembly."

So even codes are exactly same, their assembly name is different. Therefore,
I cannot cast from one type to another.

Thanks.. :)
 

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