我可以安全地使用 WinForms 设计器中仅具有公共工厂方法的控件吗?

发布于 2024-12-20 04:01:07 字数 306 浏览 0 评论 0原文

我知道我可以手动编辑 form.designer.cs 以调用工厂方法来代替当我从工具栏拉出控件时它插入的不可访问的构造函数。但由于 VS 随心所欲地重写了设计器文件,所以我不想做一些在表单设计器重写时最终会破坏的事情。

//VS Generated code
this.myControl1 = new Foo.Bar.Controls.MyControl();

//Factory replacement 
this.myControl1 = Solution.Project.ClassFactory.CreateMyControl();

I know I can manually edit form.designer.cs to call the factory method in place of the inaccessible constructor it inserts when I pull the control from the toolbar. But since VS rewrites the designer file at whim I don't want to do something that's going to end up breaking when the form designer does it's rewrite thing.

//VS Generated code
this.myControl1 = new Foo.Bar.Controls.MyControl();

//Factory replacement 
this.myControl1 = Solution.Project.ClassFactory.CreateMyControl();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-12-27 04:01:07

您可以使用以下方法:

[TypeConverter(typeof(MyControlFactoryConverter))]
public class MyControl : Control {
    public string Parameter { get; private set; }
    public MyControl() { }
    public MyControl(string parameter) {
        Parameter = parameter;
    }
}
public static class MyControlFactory {
    public static MyControl CreateControl(string parameter) {
        return new MyControl(parameter);
    }
}
//
public class MyControlFactoryConverter : ExpandableObjectConverter {
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
        if(destinationType == typeof(InstanceDescriptor)) return true;
        return base.CanConvertTo(context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
        if(destinationType == typeof(InstanceDescriptor) && value != null) {
            MethodInfo mInfo = typeof(MyControlFactory).GetMethod("CreateControl",
                BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
            return new InstanceDescriptor(mInfo, new object[] { "Hello World" });
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

Thia方法在designer.cs文件中生成以下代码:

this.myControl1 = WindowsFormsApplication3.MyControlFactory.CreateControl("Hello World");

You can use the following approach:

[TypeConverter(typeof(MyControlFactoryConverter))]
public class MyControl : Control {
    public string Parameter { get; private set; }
    public MyControl() { }
    public MyControl(string parameter) {
        Parameter = parameter;
    }
}
public static class MyControlFactory {
    public static MyControl CreateControl(string parameter) {
        return new MyControl(parameter);
    }
}
//
public class MyControlFactoryConverter : ExpandableObjectConverter {
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
        if(destinationType == typeof(InstanceDescriptor)) return true;
        return base.CanConvertTo(context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
        if(destinationType == typeof(InstanceDescriptor) && value != null) {
            MethodInfo mInfo = typeof(MyControlFactory).GetMethod("CreateControl",
                BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
            return new InstanceDescriptor(mInfo, new object[] { "Hello World" });
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

Thia approach generated the following code within the designer.cs file:

this.myControl1 = WindowsFormsApplication3.MyControlFactory.CreateControl("Hello World");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文