2011-09-17 7 views
3

속성에 대한 편집기를 만들었습니다. 그러나 편집기의 생성자에 인수를 전달하고 싶지만이를 수행하는 방법은 확실하지 않습니다.UITypeEditor를 상속받은 클래스에 인수를 전달하는 방법

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

답변

0

할 수 있습니다 당신 명이 MyEditor 클래스,이 같은 또 다른 (매개 변수화) 생성자를 추가

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

    ... 
    ... 
} 

문제가 당신은 또한에 있어야한다는 것입니다 해당 생성자를 호출하는 사람을 제어 할 수 있습니다. 그러면 사용할 생성자를 결정할 수 있고 매개 변수에 값을 지정하거나 지정할 수 있기 때문입니다.

+0

어떻게 대답하나요? 문제는이 클래스 인스턴스화를 제어 할 수 없다는 것입니다. 물론 생성자를 추가 할 수는 있지만 아무것도 의미가 없으며 아무 것도 변경되지 않습니다. 방금 문제를 지적했는데이 대답은 문제를 해결하는 데 도움이되지 않습니다. –

10

나는 그것이 다소 오래된 문제라는 것을 알고 있지만 비슷한 문제가 발생했으며 제공된 유일한 대답으로는 조금 해결되지 않습니다.

그래서 약간 까다 롭고 해결 방법과 같은 자체 솔루션을 작성하기로 결정했지만 확실히 나를 위해 작동하고 누군가에게 도움이 될 것입니다.

다음은 작동 방식입니다. 자신 만의 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; 
     } 
    } 
} 
관련 문제