[英]C# elegant way to assign properties values rather then duplication
i have searched for something similiar in stackoverflow and couldnt find anything which will give me some hint. 我已经在stackoverflow中搜索了类似的东西,但找不到任何可以给我一些提示的东西。
i have following code: 我有以下代码:
DATA val1 = new DATA();
val1.Name = "KeyValue";
val1.Value = "805373069";
DATA val2 = new DATA();
val2.Name = "Tel";
val2.Value = "0123456789";
DATA val3 = new DATA();
val3.Name = "TargetID";
val3.Value = "43301";
DATA val4 = new DATA();
val4.Name = "ServiceLevel";
val4.Value = "Y";
DATA val5 = new DATA();
val5.Name = "TypeId";
val5.Value = "13505";
DATA val6 = new DATA();
val6.Name = "DateTime";
val6.Value = System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt");
DATA val7 = new DATA();
val7.Name = "DbDateTime";
val7.Value = System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt");
and once all the objects are populated i put them in Single array. 一旦所有对象都被填充,我就将它们放到单个数组中。 ie to be used somewhere else
即在其他地方使用
DATA[] array = {val1,val2,val3,val4,val5,val6,val7}; DATA []数组= {val1,val2,val3,val4,val5,val6,val7};
and Proxy class which i cant change is: 我不能更改的代理类是:
public partial class DATA {
private string nameField;
private string valueField;
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
this.RaisePropertyChanged("Name");
}
}
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
this.RaisePropertyChanged("Value");
}
}
Now what i have tried and failed to make it easier is used Dictionary and also jagged array and multi dimensional array which didnt worked as i hoped. 现在,我尝试过但未能使它变得更容易的事情是使用了Dictionary,还有锯齿状数组和多维数组,它们并没有按我希望的那样工作。
can someone give me hint of a better solution then having 7 different objects created, as this data is dynamic i have to do this runtime data population. 有人可以给我一个更好的解决方案的提示,然后创建7个不同的对象,因为此数据是动态的,因此我必须执行此运行时数据填充。
suggestions please? 建议吗?
Put all data in a dictionary if you want to make sure names must not be duplicated: 如果要确保名称不能重复,请将所有数据放入字典中:
var data = new Dictionary<string, string>();
// fill dictionary:
data.Add("name1", /*value*/);
data.Add("name2", /*value*/);
data.Add("name3", /*value*/);
data.Add("name4", /*value*/);
Then convert it to array: 然后将其转换为数组:
return data.Select(d => new Data(){ Name = d.Key, Value = d.Value}).ToArray();
Make sure you have included using System.Linq
in top. 确保顶部包含
using System.Linq
。
UPDATE : As @LukeH suggested, You can simply use collection initializer like this: 更新 :如@LukeH所建议,您可以像这样简单地使用集合初始化程序 :
var data = new Data[]
{
new Data(){ Name = "Sylvester", Value = /*value*/ },
new Data(){ Name = "Whiskers", Value = /*value*/ },
new Data(){ Name = "Sasha", Value = /*value*/ }
};
Which doesn't prevent duplicate names for Data
type instances. 这不会阻止
Data
类型实例的重复名称。
You could just declare the objects in-line as part of the array declaration, if all you're trying to do is avoid having the variables: 如果您只是想避免使用变量,则可以在数组声明中直接内嵌声明对象:
DATA[] array = {
new DATA { Name = "something", Value = "something else" },
new DATA { Name = "something", Value = "something else" },
new DATA { Name = "something", Value = "something else" },
new DATA { Name = "something", Value = "something else" }
};
Anywhere that you have a variable, you can instead have the operation which created that variable. 在任何有变量的地方,都可以使用创建该变量的操作。 The order of operations will result in evaluating to the same thing.
操作顺序将导致对同一事物进行评估。 Where you'd need a variable is where you want to use the same instance of something multiple times, or the same value without having to re-calculate it.
在需要变量的地方,您想多次使用某事物的同一实例 ,或者在不必重新计算的情况下使用同一值。
You can create extension method something like this to overcome the problem of assign properties values rather then duplication, 您可以创建类似这样的扩展方法来克服分配属性值而不是重复的问题,
static class Extensions
{
public static void AddDataObject(this List<DATA> dataList, params string[] values)
{
dataList.Add(new DATA() { Name = values[0], Value = values[1] });
}
}
and passing that values as per given below, 并按照以下给出的值进行传递,
List<DATA> dataList = new List<DATA>();
dataList.AddDataObject("KeyValue", "805373069");
dataList.AddDataObject("Tel", "0123456789");
Here in above example I used List instead of array, you can change according to your requirements 在上面的示例中,我使用列表而不是数组,可以根据需要进行更改
You could initialize an anonymous object and then convert to an array of data like this: 您可以初始化一个匿名对象,然后将其转换为数据数组,如下所示:
var data = new {
KeyValue="805373069",
Tel="0123456789",
TargetID="43301",
ServiceLevel="Y",
TypeId="13505",
DateTime=System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt"),
DbDateTime=System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt")
};
var array = data.GetType()
.GetProperties()
.Select(x=>new DATA{Name=x.Name,Value=(string)x.GetValue(data)})
.ToArray();
You could also do it like this: 您也可以这样:
var data = new {
KeyValue="805373069",
Tel="0123456789",
TargetID="43301",
ServiceLevel="Y",
TypeId="13505",
DateTime=System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt"),
DbDateTime=System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt")
};
var array=System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes(data)
.Select(x=>new DATA {Name=x.Key,Value=(string)x.Value})
.ToArray();
If you need to take an array of data and convert it back into a class object (not anonymous), you can do the first method, just in reverse as well. 如果需要获取数据数组并将其转换回类对象(不是匿名对象),则可以执行第一种方法,也可以相反。 Or put extension methods on it to convert from/to your data array.
或在其上放置扩展方法以从/向数据数组转换。
static class Extensions
{
public static DATA[] ToDataArray(this object data)
{
return data.GetType()
.GetProperties()
.Select(x=>new DATA{Name=x.Name,Value=(string)x.GetValue(data)})
.ToArray();
}
}
var data = new {
KeyValue="805373069",
Tel="0123456789",
TargetID="43301",
ServiceLevel="Y",
TypeId="13505",
DateTime=System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt"),
DbDateTime=System.DateTime.Now.ToString("ddMMyyyyHHmmssffftt")
};
var array=data.ToDataArray();
However, David's answer is better. 但是,大卫的答案更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.