C# Equivalent for C++ Macros and using Auto<> Properties

后端 未结 7 1936
灰色年华
灰色年华 2021-02-20 16:35

I have some auto-instantiation code which I would like to apply to about 15 properties in a fairly big class. The code is similar to the following but the type is differ

7条回答
  •  故里飘歌
    2021-02-20 16:38

    This might make things a bit neater, you could add this method to introduce some reuse:

    protected ComplexType _propertyName;
    public ComplexType PropertyName
    {
        get
        {
            return GetProperty(ref _propertyName);
        }
    }
    .
    .
    private T GetProperty(ref T property) where T : new()
    {
      if (property == null)
        property = new T();
      return property;
    }
    

提交回复
热议问题