簡體   English   中英

如何使用“委托”訪問器創建屬性?

[英]How to create properties with “delegated” accessors?

我是 c# 的新手,幾天來一直對此感到困惑。 基本上,我想創建一種屬性類型,將 getter 和 setter 邏輯委托給該參數所屬的基本類型。

這只是一個應用程序:一個屬性,其值由注冊表或某個配置文件設置。

  • get 上的屬性處理程序將執行諸如檢查緩存值(或不緩存),如果未緩存則檢索該值,緩存該值(或不緩存)並返回它。
  • 設置器的行為將僅允許屬性處理程序設置值(如果可能)。

有什么建議么? 我考慮過使用DefaultPropertyAttribute ,但我不太明白如何不為每個訪問器編寫所有必要的邏輯。


看起來這就是我想要的: http://www.sharpcrafters.com/postsharp

“少寫代碼”是的。 沒關系。

我並不以此為榮:

public abstract class HorribleBaseType
{
  private Lazy<string> _connectionString;
  private Action<string> _connectionStringSetter;
  private Func<string> _connectionStringGetter;

  public HorribleBaseType(
    Func<string> connectionStringGetter, 
    Action<string> connectionStringSetter)
  {
    _connectionStringGetter = connectionStringGetter;
    _connectionStringSetter = connectionStringSetter;

    _connectionString = new Lazy<string>(connectionStringGetter);
  }

  public string ConnectionString
  {
    get { return _connectionString.Value; }
    set 
    { 
      _connectionStringSetter(value);
      _connectionString = new Lazy<string>(_connectionStringGetter);
    }
  }
}

public class HorribleType : HorribleBaseType
{
  public HorribleType()
    : base(() => MyConfiguration.ConnectionString,
           (v) => MyConfiguration.ConnectionString = v) { }
}

100% 未經測試。

更新結合使用上述內容和@hunter的答案,您可以執行以下操作:

public class DelegateProperty<T>
{
    #region Fields
    private readonly Func<T> _getter;
    private readonly Action<T> _setter;
    private Lazy<T> _lazy;
    #endregion

    #region Constructors
    public DelegateProperty(Func<T> getter, Action<T> setter)
    {
        _getter = getter;
        _setter = setter;

        _lazy = new Lazy<T>(getter);
    }
    #endregion

    #region Properties
    public T Value
    {
        get { return _lazy.Value; }
        set
        {
            _setter(value);
            _lazy = new Lazy<T>(_getter);
        }
    }
    #endregion

    #region Operators
    public static implicit operator T(DelegateProperty<T> prop)
    {
        return prop.Value; 
    }
    #endregion
}

有了它,您現在可以執行以下操作:

class Program
{
    static void Main(string[] args)
    {
        string name = "Matt";
        var prop = new DelegateProperty<string>(
            () => name,
            value => name = value);

        var test = new Test(prop);
        Console.WriteLine(test.Name);

        test.Name = "Ben";
        Console.WriteLine(name);

        Console.ReadKey();
    }
}

public class Test
{
    private readonly DelegateProperty<string> NameProperty;

    public Test(DelegateProperty<string> prop)
    {
        NameProperty = prop;   
    }

    public string Name
    {
        get { return NameProperty; }
        set { NameProperty.Value = value; }
    }
}

使用這個愚蠢的 class:

public class Property<T>
{
    Func<T> _func;
    T _value;
    bool _fetched;

    public Property(Func<T> func)
    {
        _func = func;
    }

    public T Value
    {
        get 
        {
            if (!_fetched)
            {
                _value = _func();
                _fetched = true;
            }
            return _value;
        }
        set { _value = value; }
    }
}

你可以這樣做:

public class TestClass
{
    Property<int> _propertyInt;
    public int MyInt
    {
        get { return _propertyInt.Value; }
        set { _propertyInt.Value = value; }
    }

    Property<string> _propertyString;
    public string MyString
    {
        get { return _propertyString.Value; }
        set { _propertyString.Value = value; }
    }
}

當然,這不會處理所有情況,但它可能會讓你走上“正確”的軌道......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM