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

发布于 2024-11-05 02:29:52 字数 501 浏览 6 评论 0原文

我是 C# 新手,几天来一直对此感到困惑。基本上我想创建一个 type 属性,并将 getter 和 setter 逻辑委托给该参数所属的基本类型。

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

  • get 上的属性处理程序会执行一些操作,例如检查缓存的值(或不检查)、检索该值(如果未缓存)、缓存该值(或不缓存)并返回它。
  • 设置器的行为将仅允许属性处理程序设置值(如果可能)。

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


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

“少写代码“ 是的。就这样吧。

I'm new to c# and have been puzzling over this for a couple of days. Basically I want to create a type of property with getter and setter logic delegated to a base type to which this parameter belongs.

This is just one application: a property whose value is set by, say, the registry or some config file.

  • The property handler on a get would do something like check a cached value (or not), retrieve the value if not cached, cache the value (or not) and return it.
  • Behavior for the setter would allow only the property handler to set the value (if possible).

Any suggestions? I've thought about using DefaultPropertyAttribute, but I can't quite see how not to write all the logic necessary with each accessor.


Looks like this is what I want: http://www.sharpcrafters.com/postsharp

"Write less code" Yup. That's it alright.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叶落知秋 2024-11-12 02:29:52

我并不为此感到自豪:

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

I'm not proud of it:

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% untested.

UPDATE Using a combination of the above, and @hunter's answer, you could do something like:

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
}

With that, you can now do something like:

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; }
    }
}
坚持沉默 2024-11-12 02:29:52

使用这个愚蠢的类:

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

当然,这不能处理所有情况,但它可能会让你走上“正确”的轨道......

Using this stupid 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; }
    }
}

you can do something like this:

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

Of course this won't handle every case but it might get you on the "right" track...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文