how do I pass an argument to a class that inherits UITypeEditor

只谈情不闲聊 提交于 2021-02-16 18:36:31

问题


I have created an editor for a property. I would however like to pass some arguments to the constructor of the editor, but I am unsure as to how to do this.

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

~

class MyEditor: UITypeEditor
{
  public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
  {
    //some other code
    return obj;
  }
}

回答1:


of course you can add another (parametrized) constructor to your myEditor class, something like this:

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

  ...
  ...
}

the problem is that you should also be in control of who calls that constructor because only then you can decide which constructor to use and you can specify/assign a value to the parameters.




回答2:


I know it's kinda old issue, but I've encountered similar problem and the only provided answer does not solve it a bit.

So I decided to write my own solution which is a little tricky and is more like a workaround, but it sure works for me, and maybe will help somebody.

Here's how it works. Since you don't create the instance of your own UITypeEditor derivative, you have no control over what arguments are passed to the constructor. What you can do, is to create another attribute and assign it to the same property, you are assigning your own UITypeEditor and pass your arguments to that attribute, and later read values from that attribute.

[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;
        }
    }
}


来源:https://stackoverflow.com/questions/7456738/how-do-i-pass-an-argument-to-a-class-that-inherits-uitypeeditor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!