Is it possible to make the UserControl derived class run in another AppDomain?

  • Thread starter Thread starter ZhangZQ
  • Start date Start date
Z

ZhangZQ

Supposing I have a class "UserControl1 : System.Windows.Forms.UserControl"
in an Assembly "UI.dll", now I create an Application project and want to use
the "UserControl1" in another AppDomain,

private void CreateUIInAnotherDomain()
{
UI.UserControl1 ui =
(UI.Cls)domain.CreateInstanceFromAndUnwrap(@".\UI.dll", "UI.UserControl1");

this.Controls.Add(ui);
}

Obviously, the obove code is wrong, so Is it possible to make the
UserControl derived class run in another AppDomain?


Thank you!
 
In order to cross AppDomains you must use Remoting, and there are only two
options:
- derive from MarshalByRefObject, which UserControl doesn't
- be serializable, which UserControl is not

However, you *could* implement ISerializable on your UserControl-derived
class and handle the serialization/deserialization yourself. However, it
would be quite tricky, since you'd have to recreate window handles and
suchlike.
 
Yes, can you give me more hint and if there is any documentation about this
? Thank you very much!


Regards,
ZhangZQ
 
ZhangZQ said:
Yes, can you give me more hint and if there is any documentation about
this ? Thank you very much!

I personally have never marshalled a control across app domain boundaries,
and don't think it's a particularly good idea. Wouldn't it better to send
objects containing the information you require across the boundary? The
other side can then use that information to create the relevant controls. In
that case you can just decorate your objects with the SerializableAttribute.

If you're determined to go the UserControl route, google for "Implementing
ISerializable". Note that when you implement this interface, you must also
implement a constructor of the form: MyClass(SerializationInfo info,
StreamingContext context) {}. This constructor handles deserialization. I
don't know if you'll have to handle any special coding in order to implement
the private properties of the UserControl. Possible not. If not, you could
just serialize the public properties across. With a control, these are quite
a few, so you can expect a lot of work...
 
Back
Top