简体   繁体   English

如何在属性属性上使用条件语句IF ELSE

[英]How to use conditional statement IF ELSE on property attribute

I want to show MyProperty1 or MyProperty2 based on MyPropertySelected . 我想说明MyProperty1MyProperty2基于MyPropertySelected。 How to use a conditional statement if or else based on MyPropertySelected ? 如何使用条件语句if还是else基于MyPropertySelected? Thanks. 谢谢。

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

You're confusing apples with oranges. 你把苹果和桔子弄混了。

Attributes are metadata and a property value acquires its value on run-time. 属性是元数据,属性在运行时获取其值

In other words: attribute is something that you'll access using reflection and those aren't tied to a particular object but to the type of the object (ie the class ). 换句话说: 属性是您将使用反射访问的属性 ,它们并不与特定的对象绑定,而是与对象的类型(即class )绑定。

Another issue is that you want to add an attribute to a property based on a condition that can't work in compile-time. 另一个问题是,您要基于在编译时无法工作的条件向属性添加属性。

Your MyPropertySelected won't get any value until its enclosing class gets instantiated - that's creating an object, for example: MyClass a = new MyClass() - , meaning that adding or not adding the attribute would never be a compile-time choice . 在实例化其封闭的类之前, MyPropertySelected不会获得任何值-这将创建一个对象,例如: MyClass a = new MyClass() -这意味着添加或不添加该属性永远不是编译时的选择

I want to be clear: you can't do what you want purely using attributes! 我想说清楚: 您不能仅使用属性来做您想做的事情!

You can't conditionally apply attributes based on run-time values . 您不能有条件地根据运行时值应用属性

Finally, I suspect you want to make something Browsable based on a condition, like your own question says. 最后,我怀疑您想根据条件使某些内容Browsable ,如您自己的问题所述。 You can't do that. 你不能那样做。

Ok ok, but what...? 好吧好吧,但是...?

You can workaround your situation with a different software design. 您可以使用其他软件设计来解决您的情况。

1) 1)

First, create an interface that will have any of the properties that would be browsable or not. 首先,创建一个具有任何可浏览或不可浏览属性的接口。 But don't apply the attribute [Browsable(bool)] to the interface properties. 但是不要将属性[Browsable(bool)]应用于接口属性。

2) 2)

Create two classes that implements the previously created interface. 创建两个实现先前创建的接口的类。

In the first class, implement the interface properties and put a [Browsable(true)] attribute on them. 在第一类中,实现接口属性,并在其上放置[Browsable(true)]属性。 In the second class, do the same, but this time put a [Browsable(false)] on them. 在第二个类中,执行相同的操作,但是这次在它们上放了一个[Browsable(false)]

3) 3)

Some code that creates the instance of the object will be the one that will also decide which one will be instantiated . 一些创建对象实例的代码将是确定对象实例化的代码

That is, externalizing MyPropertySelected outside of both classes and performing the whole condition switch in the caller. 也就是说,将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!

UPDATE 更新

I've reviewed some of your question's comments and I've found you're working with PropertyGrid control. 我已经查看了您的问题的一些评论,发现您正在使用PropertyGrid控件。

Anyway, you can apply the concept in your case. 无论如何,您可以在您的案例中应用该概念。 PropertyGrid can be inherited. 可以继承PropertyGrid You can create both PropertyGrid1 and PropertyGrid2 derived classes that both implement the proposed interface! 您可以创建PropertyGrid1PropertyGrid2派生的类,它们都实现建议的接口!

You probably want an intermediary property like this: 您可能想要这样的中介属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM