Доброго времени суток.
У меня возникло ощущение, что в реализации System.Windows.Forms.Control+ControlCollection имеется недоработка.
Суть сабжа следующая.
Вот код метода Add:
public virtual void Add(Control value)
{
if (value != null)
{
if (value.GetTopLevel())
{
throw new ArgumentException(SR.GetString("TopLevelControlAdd"));
}
if (this.owner.CreateThreadId != value.CreateThreadId)
{
throw new ArgumentException(SR.GetString("AddDifferentThreads"));
}
Control.CheckParentingCycle(this.owner, value);
if (value.parent == this.owner)
{
value.SendToBack();
}
else
{
if (value.parent != null)
{
value.parent.Controls.Remove(value);
}
base.InnerList.Add(value);
if (value.tabIndex == -1)
{
int num = 0;
for (int i = 0; i < (this.Count - 1); i++)
{
int tabIndex = this[i].TabIndex;
if (num <= tabIndex)
{
num = tabIndex + 1;
}
}
value.tabIndex = num;
}
this.owner.SuspendLayout();
try
{
Control parent = value.parent;
try
{
value.AssignParent(this.owner);
}
finally
{
if ((parent != value.parent) && ((this.owner.state & 1) != 0))
{
value.SetParentHandle(this.owner.InternalHandle);
if (value.Visible)
{
value.CreateControl();
}
}
}
value.InitLayout();
}
finally
{
this.owner.ResumeLayout(false);
}
LayoutTransaction.DoLayout(this.owner, value, PropertyNames.Parent);
this.owner.OnControlAdded(new ControlEventArgs(value));
}
}
}
Если в качестве параметра передается контрол, созданный в отдельном AppDomain, то в выделенной строке мы получим исключение
Remoting cannot find field 'parent' on type 'System.Windows.Forms.Control'
Это происходит из-за того, что TransparentProxy не генерит заглушку для private member parent класса Control.
Тогда возникает вопрос, каким образом добавлять контролы, созданные в другом AppDomain (скажем реализуется сценарий Plugins в виде UserControl-ов).
Если семантически невозможно такое сделать — второй вопрос: Зачем наследовать Control от Component, который, в свою очередь, наследуется
от MarshalByRefObject?
Это не поддерживается
[msdn]
[Note] Note:
Windows Forms controls are not designed to be marshaled across application domains. For this reason, Microsoft does not support passing a Windows Forms control across an AppDomain boundary, even though the Control base type of MarshalByRefObject would seem to indicate that this is possible. Windows Forms applications that have multiple application domains are supported as long as no Windows Forms controls are passed across application domain boundaries. [/msdn]
поддержка только в WPF и Framework 3.5
support-for-windows-forms-in-hosts-and-add-ins
Здравствуйте, pocheptsov, Вы писали:
P>Это не поддерживается
P>[msdn]
P>[Note] Note:
P>Windows Forms controls are not designed to be marshaled across application domains. For this reason, Microsoft does not support passing a Windows Forms control across an AppDomain boundary, even though the Control base type of MarshalByRefObject would seem to indicate that this is possible. Windows Forms applications that have multiple application domains are supported as long as no Windows Forms controls are passed across application domain boundaries. [/msdn]
P>поддержка только в WPF и Framework 3.5 support-for-windows-forms-in-hosts-and-add-ins
Благодарю за помощь.
Теперь все понятно.
Здравствуйте, D.Triton, Вы писали:
DT>Здравствуйте, pocheptsov, Вы писали:
P>>Это не поддерживается
P>>[msdn]
P>>[Note] Note:
P>>Windows Forms controls are not designed to be marshaled across application domains. For this reason, Microsoft does not support passing a Windows Forms control across an AppDomain boundary, even though the Control base type of MarshalByRefObject would seem to indicate that this is possible. Windows Forms applications that have multiple application domains are supported as long as no Windows Forms controls are passed across application domain boundaries. [/msdn]
P>>поддержка только в WPF и Framework 3.5 support-for-windows-forms-in-hosts-and-add-ins
DT>Благодарю за помощь.
DT>Теперь все понятно.
Всегда пожалуйста, это Женя Забелешинский попросил чтобы помогли

тебе