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

后端 未结 7 2393
忘了有多久
忘了有多久 2021-02-20 15:55

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:52

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

提交回复
热议问题