简体   繁体   English

在一行中实例化 object 属性

[英]instantiate object property on get in one line

This is a question out of curious more so than actual need, but I thought it would be interesting to find out, since I have classes soo deep I need 10 lines of instantiation just to set a single property.这是一个出于好奇而不是实际需要的问题,但我认为找出答案会很有趣,因为我的类太深了,我需要 10 行实例化来设置一个属性。

The property [MyObj3] functionally works as I want it to, where on the get property function it checks if the object is null and instantiates it.属性 [MyObj3] 在功能上按我的意愿工作,在 get 属性 function 上,它检查 object 是否为 null 并实例化它。 Whereas [MyObj2] will throw a null reference exception when trying to reference its properties.而 [MyObj2] 在尝试引用其属性时将抛出 null 引用异常。

But to make every single property in this way is a bit of a bigger nightmare than I want to tackle.但是,以这种方式制作每一个属性是一个比我想要解决的更大的噩梦。 So the question here is if there is a simple inline way to handle this.所以这里的问题是是否有一种简单的内联方式来处理这个问题。 Or any other ways I may not be thinking of.或者我可能没有想到的任何其他方式。

-- UPDATE: I forgot to mention, I wanted to keep the object null until it is needed, otherwise it gets included in xml serialization. -- 更新:我忘了提,我想保留 object null 直到需要它,否则它会包含在 xml 序列化中。

public class Obj1
{
    public Obj2 MyObj2 { get; set; }

    private Obj3 _obj3;
    public Obj3 MyObj3
    {
        get => _obj3 == null ? _obj3 = new Obj3() : _obj3;
        set => _obj3 = value;
    }
}

public class Obj2
{
    public string MyProp1 { get; set; }
}

public class Obj3
{
    public string MyProp1 { get; set; }
}

public void test()
{
    var obj = new Obj1();
    obj.MyObj2.MyProp1 = "test";//null exception
    obj.MyObj3.MyProp1 = "test";//works fine
}

You could instantiate Obj2 and Obj3 when you instantiate Obj1 like this当你像这样实例化 Obj1 时,你可以实例化 Obj2 和 Obj3

public class Obj1
{
    public Obj2 MyObj2 { get; set; } = new Obj2();
    public Obj3 MyObj3 { get; set; } = new Obj3();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果不为空,则在一行 LINQ 中获取该属性的值 - If not null, get the value of the property in one LINQ line C# 从 class 实例化 IList object 并在一行中设置本地属性 - C# Instantiate IList object from a class and set local properties in one line 实例化具有对象类型本身属性的对象 - Instantiate objects having a property of the object type itself 序列化为JSON以每行获取一个对象 - Serialize to JSON to get one object per line 如何编写lambda以根据对象中的另一个属性获取一个属性 - How to write a lambda to get one property based on another property in an object 我如何获得此条件包含投影以实例化ref_calendar_premis实体对象的“ premis”实体对象属性? - How do i get this conditional include projection to instantiate the “premis” entity-object property of the ref_calendar_premis entity-object? 有没有办法只实例化一个对象的一个​​实例? - Is there a way to only instantiate one instance of an object? 显示一个OpenFileDialog并从一行中获取FileName属性 - Show an OpenFileDialog and get the FileName property from it in one line EF Core 一对多关系实例化 Object 内部 Object 构造函数 - EF Core One To Many Relationship Instantiate Object Inside Object Constructor C#锯齿状数组获取属性实例化LINQ - C# jagged array get property instantiate linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM