将为属性赋值

问题描述

| 如果一个类中有两个公共属性,那么创建该类对象的人将需要我填充其中两个属性; C#中有没有一种方法可以强加此行为? 因此,基本上,如果已给Property1赋值,那么用户应该不能为Property2赋值,反之亦然? 如果不是,那么是否有最佳实践来执行此操作,而不是创建两个单独的类,一个类为Property1,第二个类为Property2? 或者是可以通知用户有关此行为的方法属性?那行得通吗?     

解决方法

您可以在设置器中添加代码。有点像这样...
public class MyClass 
{
int one = -1;
int two = -2;

public int One { get { return this.one; }
                 set { if (this.two != -1 ) this.one == value; }}

public int Two { get { return this.two; }
                 set { if (this.one!= -1 ) this.two== value; }}
}
    ,您可以将逻辑放在属性设置器中,以在设置另一个属性时清除一个属性。     ,只需将用于强制约束的代码放入每个属性的设置器中,就完成了。例:
using System;

public class MyClass {
    public static void Main()   { 
        TestClass tc = new TestClass();
        tc.Str1 = \"Hello\";
        tc.Str2 = \"World!\"; // will not be set because of enforced constraint
        Console.WriteLine(tc.Str1);
        Console.WriteLine(tc.Str2);
        Console.ReadKey();
    }
}

public class TestClass {
    private string _str1;
    public string Str1  {
        get { return _str1; }
        set {
            if (string.IsNullOrEmpty(Str2))
                _str1 = value;
        }
    }

    private string _str2;
    public string Str2  {
        get { return _str2; }
        set {
            if (string.IsNullOrEmpty(Str1))
                _str2 = value;
        }
    }
}
输出:
Hello
请注意,永远不会设置Str2 \的值,因为首先设置了Str1 \'s,因此会输出一个空字符串。     ,对于两个属性,我可能只会像其他答案中那样进行设置器检查。但是你可以做这样的事情... 与其将其设为两个属性,不如将它们设为一个。人为的示例:想象一下,不使用具有UsaZipCode(int)和CanadianPostalCode(string)属性的地址,而是使用一个PostalCode,如下所示:
class Address
{
    public string Street { get; set; }
    public IPostalCode PostalCode { get; set;}
}

public interface IPostalCode
{
    int? Usa { get; }
    string Canadian { get; }
}

public class UsaPostalCode
{
    private int _code;
    public UsaPostalCode(int code) { _code = code; }
    public int? Usa { get { return _code; }
    public string Canadian { get { return null; }
}

public class CanadianPostalCode
{
    private string _code;
    public CanadianPostalCode(string code) { _code = code; }
    public int? Usa { get { return null; }
    public string Canadian { get { return _code; }
}
现在,地址永远不能同时包含美国和加拿大的邮政编码。额外的复杂性值得吗?取决于用例。     ,
public class MyClass
{
    private string pickedProperty = null;
    private object member1;
    private object member2;

    public object Property1 
    {
        get { return this.member1; }
        set 
        {
            if (this.pickedProperty == null)
                this.pickedProperty = \"Property1\";

            if (this.pickedProperty == \"Property1\")
                this.member1 = value;
        }
    }

    public object Property2 
    {
        get { return this.member2; }
        set
        {
            if (this.pickedProperty == null)
                this.pickedProperty = \"Property2\";

            if (this.pickedProperty == \"Property2\")
                this.member1 = value;
        }
    }
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...