C# 如何将参数传递给继承UITypeEditor的类

C# 如何将参数传递给继承UITypeEditor的类,c#,.net,winforms,C#,.net,Winforms,我已经为属性创建了一个编辑器。 不过,我想将一些参数传递给编辑器的构造函数,但我不确定如何执行此操作 FOO _foo = new foo(); [Editor(typeof(MyEditor), typeof(UITypeEditor))] public object foo { get { return _foo; } set {_foo = value;} } ~ 当然,您可以将另一个(参数化)构造函数添加到myEditor类中,如下所示: public class MyEdi

我已经为属性创建了一个编辑器。 不过,我想将一些参数传递给编辑器的构造函数,但我不确定如何执行此操作

FOO _foo = new foo();
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
public object foo 
 {
 get { return _foo; }
 set {_foo = value;}
 }
~


当然,您可以将另一个(参数化)构造函数添加到myEditor类中,如下所示:

public class MyEditor: UITypeEditor
{
  // new parametrized constructor
  public MyEditor(string parameterOne, int parameterTwo...)
  {
    // here your code
  }

  ...
  ...
}

问题是,您还应该控制谁调用该构造函数,因为只有这样,您才能决定使用哪个构造函数,并且可以指定/分配参数值。

我知道这是一个老问题,但我遇到了类似的问题,唯一提供的答案一点也解决不了

所以我决定写我自己的解决方案,这是一个有点棘手的,更像是一个解决办法,但它确实为我工作,也许会帮助别人

下面是它的工作原理。 由于不创建自己的UITypeEditor派生的实例,因此无法控制传递给构造函数的参数。 您可以做的是创建另一个属性并将其指定给同一属性,您可以指定自己的UITypeEditor并将参数传递给该属性,然后从该属性读取值

[Editor(typeof(MyEditor), typeof(UITypeEditor))]
[MyEditor.Arguments("Argument 1 value", "Argument 2 value")]
public object Foo { get; set; }

class MyEditor : UITypeEditor
{
    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        string property1 = string.Empty, property2 = string.Empty;
        //Get attributes with your arguments. There should be one such attribute.
        var propertyAttributes = context.PropertyDescriptor.Attributes.OfType<ArgumentsAttribute>();
        if (propertyAttributes.Count() > 0)
        {
            var argumentsAttribute = propertyAttributes.First();
            property1 = argumentsAttribute.Property1;
            property2 = argumentsAttribute.Property2;
        }
        //Do something with your properties...
        return obj;
    }

    public class ArgumentsAttribute : Attribute
    {
        public string Property1 { get; private set; }
        public string Property2 { get; private set; }
        public ArgumentsAttribute(string prop1, string prop2)
        {
            Property1 = prop1;
            Property2 = prop2;
        }
    }
}
[编辑器(typeof(MyEditor),typeof(UITypeEditor))]
[MyEditor.Arguments(“参数1值”、“参数2值”)]
公共对象Foo{get;set;}
类MyEditor:UITypeEditor
{
公共重写对象EditValue(System.ComponentModel.ITypeDescriptorContext上下文,System.IServiceProvider提供程序,对象值)
{
string property1=string.Empty,property2=string.Empty;
//使用参数获取属性。应该有一个这样的属性。
var propertyAttributes=context.PropertyDescriptor.Attributes.OfType();
如果(propertyAttributes.Count()>0)
{
var argumentsAttribute=propertyAttributes.First();
property1=argumentsAttribute.property1;
property2=argumentsAttribute.property2;
}
//对你的财产做点什么。。。
返回obj;
}
公共类ArgumentsAttribute:属性
{
公共字符串属性1{get;private set;}
公共字符串属性2{get;private set;}
公共参数属性(字符串prop1、字符串prop2)
{
属性1=属性1;
财产2=财产2;
}
}
}

这算什么答案?问题是您无法控制此类实例化。当然,您可以添加构造函数,但这并不意味着什么,也不会改变什么。你刚刚指出了问题所在,这个答案无论如何也无助于解决问题
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
[MyEditor.Arguments("Argument 1 value", "Argument 2 value")]
public object Foo { get; set; }

class MyEditor : UITypeEditor
{
    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        string property1 = string.Empty, property2 = string.Empty;
        //Get attributes with your arguments. There should be one such attribute.
        var propertyAttributes = context.PropertyDescriptor.Attributes.OfType<ArgumentsAttribute>();
        if (propertyAttributes.Count() > 0)
        {
            var argumentsAttribute = propertyAttributes.First();
            property1 = argumentsAttribute.Property1;
            property2 = argumentsAttribute.Property2;
        }
        //Do something with your properties...
        return obj;
    }

    public class ArgumentsAttribute : Attribute
    {
        public string Property1 { get; private set; }
        public string Property2 { get; private set; }
        public ArgumentsAttribute(string prop1, string prop2)
        {
            Property1 = prop1;
            Property2 = prop2;
        }
    }
}