【问题标题】:How to use conditional statement IF ELSE on property attribute如何在属性属性上使用条件语句 IF ELSE
【发布时间】:2012-11-21 00:25:36
【问题描述】:

我想根据 MyPropertySelected 显示 MyProperty1MyProperty2。如何使用基于MyPropertySelected的条件语句ifelse?谢谢。

// [Browsable(true)
// ????? conditional statement IF ELSE in here..
// IF (MyPropertySelected) MyProperty1 will be show ELSE MyProperty2 will be show.
public bool MyPropertySelected { get; set; }

// [Browsable(true) or [Browsable(false) depending on MyPropertySelected condition.
public int MyProperty1 { get; set; }

// [Browsable(true) or [Browsable(false) depending on MyPropertySelected condition.
public int MyProperty2 { get; set; }

【问题讨论】:

  • 不清楚,你想怎么使用属性...
  • 我希望将以上属性绑定到属性网格,条件如下:MyPropertySelected 始终可浏览(true)。如果我在属性网格上将 MyPropertySelected 值选择为 true,则 MyProperty1 将设置为 Browsable(true),将 MyProperty2 设置为 MyProperty2 到 Browsable(false)。

标签: c# winforms properties attributes conditional-operator


【解决方案1】:

您可能想要这样的中间属性:

class Foo
{
    public bool MyPropertySelected
    {
        get;
        set;
    }

    public readonly int MyProperty
    {
        get 
        {
            return MyPropertySelected ? MyProperty1 : MyProperty2;
        }
    }

    private int MyProperty1
    {
        get;
        set;
    }

    private int MyProperty2
    {
        get;
        set;
    }
}

【讨论】:

    【解决方案2】:

    你把苹果和橙子混为一谈了。

    属性是元数据,属性值在运行时获取其值

    换句话说:attribute 是您可以使用 reflection 访问的东西,它们与特定对象无关,而是与对象的类型相关联(即)。

    另一个问题是,您希望根据无法在编译时工作的条件向属性添加属性。

    你的MyPropertySelected 在它的封闭类被实例化之前不会获得任何值——这是在创建一个对象,例如:MyClass a = new MyClass()-意味着添加或不添加属性永远不会是编译时的选择

    我想明确一点:你不能仅仅使用属性来做你想做的事!

    您不能根据运行时值有条件地应用属性

    最后,我怀疑您想根据条件制作Browsable,就像您自己的问题所说的那样。你不能那样做。

    好吧好吧,但是什么...?

    您可以使用不同的软件设计来解决您的情况。

    1)

    首先,创建一个界面,该界面将具有可浏览或不可浏览的任何属性。但不要将属性[Browsable(bool)] 应用于接口属性。

    2)

    创建两个实现先前创建的接口的类。

    在第一个类中,实现接口属性并在其上放置[Browsable(true)] 属性。在第二节课上,做同样的事情,但这次在他们身上加上[Browsable(false)]

    3)

    创建对象实例的一些代码也将决定将实例化哪个实例

    也就是说,将MyPropertySelected 外部化到两个类之外,并在调用者中执行整个条件切换。

    public interface IBrowsableProperties
    {
       int Property1 { get;set; }
       int Property2 { get;set; }
    }
    
    public class A : IBrowsableProperties
    { 
       [Browsable(true)]
       public int Property1 { get;set; }
    
       [Browsable(true)]
       public int Property1 { get;set; }
    }
    
    public class B : IBrowsableProperties
    {
       [Browsable(false)]
       public int Property1 { get;set; }
    
       [Browsable(false)]
       public int Property1 { get;set; }
    }
    
    // Somewhere in some method...
    bool propertySelected = true;
    
    IBrowsableProperties instance = null;
    
    if(propertySelected) 
    {
       instance = new A();
    }
    else
    {
       instance = new B();
    }
    
    // ... do stuff with your instance of IBrowsableProperties!
    

    更新

    我查看了您的一些问题的 cmets,发现您正在使用 PropertyGrid 控件。

    无论如何,你可以在你的案例中应用这个概念。 PropertyGrid 可以被继承。你可以同时创建PropertyGrid1PropertyGrid2 派生类,它们都实现了建议的接口!

    【讨论】:

    • @YD4 没问题!不客气。在我的答案底部查看我的更新,这对你来说应该是一个很好的提示:)
    • @KonstantinVasilcov 谢谢!我得到了启发:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2020-01-17
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多