有没有一种方法,在设计时,初始化与在Visual Studio 2010中的所有属性的对象? [英] Is there a way, at design time, to initialize an object with all properties in Visual Studio 2010?

查看:307
本文介绍了有没有一种方法,在设计时,初始化与在Visual Studio 2010中的所有属性的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我在VS 2010中,.NET 4,C#编写代码。此外,如果它的事项,我使用的是最新版本的ReSharper的。





让我们说我有这个模型:

 公共类SomeObject 
{
公共字符串红{搞定;组; }
公共字符串绿{搞定;组; }
公共字符串蓝{搞定;组; }
公共字符串黄{搞定;组; }
公共字符串紫{搞定;组; }
公共字符串橙色{搞定;组; }
公共字符串黑色{搞定;组; }
}



其他地方的代码,我需要实例化这些对象之一,如所以:

  SomeObject myObject的=新SomeObject {
红=一些价值,
蓝色=其他一些价值,
。 。 。,
黑=最后的值
};



*的注:的*我有时会想初始化仅此对象其可能的特性:总的一个子集(即只红色和蓝色)。



目前,我不必键入每个属性(红,蓝,绿等)为每个新实例 SomeObject 。有没有在VS2010热键或东西有这些属性预填充的,所以我只需要值分配给每个而不是键入每一个?


解决方案

目前,我不必键入每个属性(红,蓝,绿等)为SomeObject的每个新实例。




就个人而言,如果你需要设置这些每一个对象实例,我会加入一个构造函数的所有参数的处理这个问题,只是不能使用对象初始化语法。这会给你充分的智能感知,同时也有利于/强迫你来构造对象的正确的每一次。



记住,一个类型的构造函数的的迫使你提供给对象正确初始化到合适状态所需的所有参数。一个缺点对象初始化的是,他们没有在编译时强制此。因此,如果所有这些特性都的需要的每一个对象实例,构造函数应该提供给他们,而不是使用对象初始化。


Background

I am writing code in VS 2010, .NET 4, C#. Also, in case it matters, I am using the latest version of ReSharper.

Question

Let's say I have this model:

public class SomeObject
        {
            public string Red{ get; set; }
            public string Green{ get; set; }
            public string Blue{ get; set; }
            public string Yellow{ get; set; }
            public string Purple{ get; set; }
            public string Orange{ get; set; }
            public string Black{ get; set; }
        }

Elsewhere in the code, I need to instantiate one of these objects, like so:

SomeObject myObject = new SomeObject{
                                     red = "some value", 
                                     blue = "some other value",
                                     . . .,
                                     black="last value"
                                    };

*NOTE:*I will sometimes want to initialize this object with only a subset of its total possible properties (i.e. just red and blue).

At the moment, I am having to type in each property (red, blue, green, etc) for each new instance of SomeObject. Is there a hotkey or something in VS2010 to have those properties pre-populated so I just have to assign values to each rather than typing each one?

解决方案

At the moment, I am having to type in each property (red, blue, green, etc) for each new instance of SomeObject.

Personally, if you need to set these for every object instance, I would handle this by adding a constructor that takes all of the parameters, and just not use the object initialization syntax. This will give you full intellisense, but also help/force you to construct the object correctly every time.

Remember, a type's constructor should force you to supply all parameters required to correctly initialize the object to a proper state. The one downside to object initializers is that they don't force this at compile time. As such, if all of these properties are required for each object instance, the constructor should supply them instead of using object initializers.

这篇关于有没有一种方法,在设计时,初始化与在Visual Studio 2010中的所有属性的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆