【问题标题】:C# Class with many Properties and similar Setters, how to reuse logic具有许多属性和类似 Setter 的 C# 类,如何重用逻辑
【发布时间】:2021-03-12 11:00:17
【问题描述】:

我有一个包含许多属性和公共设置器的类。在每个 Setter 上,我想检查值是否已更改,如果更改则调用 EventHandler 方法。代码最终看起来太长、太重复并且很容易出现程序员错误,因为我可能会在多个 setter 方法上犯一些小错误并搞砸事情。

我想问一下有没有什么办法可以让这段代码更小、更可重用(例如,如果我想添加新的属性):

using System;

public class CosmeticsModel
{
    private string _skin;
    public string Skin {
        get => _skin;
        set
        {
            if (value != _skin)
            {
                _skin = value;
                OnCosmeticChanged("Skin", _skin);
            }
        }
    }

    private string _eyes;
    public string Eyes {
        get => _eyes;
        set
        {
            if (value != _eyes)
            {
                _eyes = value;
                OnCosmeticChanged("Eyes", _eyes);
            }
        }
    }

    private string _mouth;
    public string Mouth {
        get => _mouth;
        set
        {
            if (value != _mouth)
            {
                _mouth = value;
                OnCosmeticChanged("Mouth", _mouth);
            }
        }
    }

    private string _accessory;
    public string Accessory {
        get => _accessory;
        set
        {
            if (value != _accessory)
            {
                _accessory = value;
                OnCosmeticChanged("Accessory", _accessory);
            }
        }
    }

    private string _shoes;
    public string Shoes {
        get => _shoes;
        set
        {
            if (value != _shoes)
            {
                _shoes = value;
                OnCosmeticChanged("Shoes", _shoes);
            }
        }
    }

    private string _hat;
    public string Hat {
        get => _hat;
        set
        {
            if (value != _hat)
            {
                _hat = value;
                OnCosmeticChanged("Hat", _hat);
            }
        }
    }

    private string _oneHandedWeapon;
    public string OneHandedWeapon {
        get => _oneHandedWeapon;
        set
        {
            if (value != _oneHandedWeapon)
            {
                _oneHandedWeapon = value;
                OnCosmeticChanged("OneHandedWeapon", _oneHandedWeapon);
            }
        }
    }

    // [... rest of the Class]
}

【问题讨论】:

标签: c#


【解决方案1】:

您可以提取一个名为SetProperty 的方法。也可以使用CallerMemberName自动设置属性名。

private void SetProperty(ref string property, string value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") {
    if (value != property)
    {
        property = value;
        OnCosmeticChanged(propertyName, property);
    }
}

用法:

private string _hat;
public string Hat {
    get => _hat;
    set => SetProperty(ref _hat, value);
}

【讨论】:

  • (如果你的某些属性不是字符串,你也可以很容易地使这个方法通用)
  • 不知道那个属性
  • 我仍然需要将private string _property; 添加到我的所有属性中,不是吗?
  • @fguillen 是的,但在那里打错字更难,不是吗?因为它只是一个变量名,所以编译器会检查你是否输入错误。
  • 明白了,我添加了缺少的所需代码。对于像我这样的 C# 新手来说,最好把事情弄清楚:)
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多