Multiple versions of the same type coexisting in the same AppDomai

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have the requirement of being able to load different versions of the same
type (same class name) in the executing AppDomain. I am aware that I would
not be able to unload obsolete instances, but I don't mind. The problem is
that, it seems, only one instance of the same class type can be present in
one particular AppDomain, and whatever attempt to load a class with the same
name will result in the old instance being passed.

I would assume that a possible solution would be to have class names get
different names (eg. SampleClassName_{some Guid}) before being loaded, but
can I really do this hacking into a compiled DLL?

I have Googled around but couldn't find anything related to this. Any ideas?

Thanks in advance, regards,
Vanni
 
Hi,

Vanni said:
Hello,

I have the requirement of being able to load different versions of the
same
type (same class name) in the executing AppDomain. I am aware that I would
not be able to unload obsolete instances, but I don't mind. The problem is
that, it seems, only one instance of the same class type can be present in
one particular AppDomain, and whatever attempt to load a class with the
same
name will result in the old instance being passed.

You can only have one type per AppDomain, otherwise how the compiler can
determine which version to use when you use a new statement:

object o = new XXXX

Why you have to load it in the same AppDomain? I guess you could load it in
a different AppDomain. But even so how you are going to know what features
provide each version?
 
Vanni,

You can do this, but you have to know at compile time what the two
separate versions are. You would use the "extern alias" keywords, along
with a special way of referencing the dlls which contain the types when
calling the compiler (there is support in VS.NET for this too).

Take a look at the documentation for "extern alias" located at:

http://msdn2.microsoft.com/en-us/library/ms173212(VS.80).aspx

Anson Horton also has a walkthrough which will help:

http://blogs.msdn.com/ansonh/archive/2006/09/27/774692.aspx
 
Vanni said:
I have the requirement of being able to load different versions of the same
type (same class name) in the executing AppDomain. I am aware that I would
not be able to unload obsolete instances, but I don't mind. The problem is
that, it seems, only one instance of the same class type can be present in
one particular AppDomain, and whatever attempt to load a class with the same
name will result in the old instance being passed.

I would assume that a possible solution would be to have class names get
different names (eg. SampleClassName_{some Guid}) before being loaded, but
can I really do this hacking into a compiled DLL?

I have Googled around but couldn't find anything related to this. Any ideas?

Are you trying to do this by dynamically loading the DLLs, or do you
need to do it at compile time? At compile time you can use "extern
aliases" - at execution time it's somewhat different, due to the
mucking around with reflection.
 
Hi Nicholas,

Thanks to you and all those who replied.
The extern alias solution matches my requirement very, very closely, that
sounds excellent news!

However, how would I be able to specify aliasing when loading the two
different DLL's in the AppDomain?

Thanks,
V.

Nicholas Paldino said:
Vanni,

You can do this, but you have to know at compile time what the two
separate versions are. You would use the "extern alias" keywords, along
with a special way of referencing the dlls which contain the types when
calling the compiler (there is support in VS.NET for this too).

Take a look at the documentation for "extern alias" located at:

http://msdn2.microsoft.com/en-us/library/ms173212(VS.80).aspx

Anson Horton also has a walkthrough which will help:

http://blogs.msdn.com/ansonh/archive/2006/09/27/774692.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Vanni said:
Hello,

I have the requirement of being able to load different versions of the
same
type (same class name) in the executing AppDomain. I am aware that I would
not be able to unload obsolete instances, but I don't mind. The problem is
that, it seems, only one instance of the same class type can be present in
one particular AppDomain, and whatever attempt to load a class with the
same
name will result in the old instance being passed.

I would assume that a possible solution would be to have class names get
different names (eg. SampleClassName_{some Guid}) before being loaded, but
can I really do this hacking into a compiled DLL?

I have Googled around but couldn't find anything related to this. Any
ideas?

Thanks in advance, regards,
Vanni
 

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

Back
Top