如何使用“委派”访问器创建属性?

时间:2011-05-04 16:58:02

标签: c# properties metaprogramming accessor

我是c#的新手,并且已经困惑了几天。基本上我想创建一个属性的类型,其中getter和setter逻辑委托给该参数所属的基类型。

这只是一个应用程序:一个属性,其值由注册表或某个配置文件设置。

  • get上的属性处理程序会执行类似检查缓存值(或不执行)的操作,如果未缓存则检索值,缓存值(或不缓存)并返回它。
  • setter的行为只允许属性处理程序设置值(如果可能)。

有什么建议吗?我考虑过使用DefaultPropertyAttribute,但我不太清楚如何不用每个访问器编写所有必需的逻辑。


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

“少写代码”是的。没关系。

2 个答案:

答案 0 :(得分:2)

我并不为此感到自豪:

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

答案 1 :(得分:1)

使用这个愚蠢的类:

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

当然这不会处理每一个案例,但它可能会让你走上“正确”的轨道......