我正在创建一个表面设计器(类似于Visual Studio Designer)和运行时。在设计器中,一切都是100%。我将所有设计器控件和反序列化的控件序列化到运行时,但这些控件仍包含设计器TargetWindow。主要问题是由于有了TargetWindow,运行时仍在保持设计器效果。
我尝试通过反射来更改属性本身,但这会产生怪异的效果。我试图创建控件并复制所有属性,但这并不能复制子控件等所有属性。
//Example of creating the properties to a new control
var ts = Assembly.Load(cont.GetType().Assembly.FullName);
var o = ts.GetType(cont.GetType().FullName);
Control controlform = (Control)Activator.CreateInstance(o);
PropertyInfo[] controlProperties = cont.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo propInfo in controlProperties)
{
if (propInfo.CanWrite)
{
if (propInfo.Name != "Site" && propInfo.Name != "WindowTarget")
propInfo.SetValue(controlform, propInfo.GetValue(cont, null), null);
else
{
}
}
}
//Example of creating trying to change the property directly with reflection.
var ts = Assembly.Load(cont.GetType().Assembly.FullName);
var o = ts.GetType(cont.GetType().FullName);
Control controlform2 = (Control)Activator.CreateInstance(o);
Control controlform = cont;
controlform.Site = null;
if (controlform.Parent == null)
controlform.Parent = this;
PropertyInfo[] controlProperties = controlform.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo propInfo in controlProperties)
{
if (propInfo.CanWrite)
{
if (propInfo.Name == "WindowTarget")
propInfo.SetValue(controlform, propInfo.GetValue(temp), null);
else
{
}
}
}
我认为必须有一种更简单的方法来更改TargetWindow属性或将控件从设计时更改为运行时。