C# 在类中轻松初始化字符串数组
如果一个类只包含字符串数组变量,有没有一种简单的方法来初始化它们,而不必反复键入相同的代码 例如,如果我有C# 在类中轻松初始化字符串数组,c#,.net,string,C#,.net,String,如果一个类只包含字符串数组变量,有没有一种简单的方法来初始化它们,而不必反复键入相同的代码 例如,如果我有 [Serializable] public class ReadDiagnosticEntirePointValuesResponse { public string[] pointidentifier; public string[] presentvalue; public string[] priorityarray; public string[]
[Serializable]
public class ReadDiagnosticEntirePointValuesResponse
{
public string[] pointidentifier;
public string[] presentvalue;
public string[] priorityarray;
public string[] alarmstate;
public string[] outofservice;
public string[] correctvalue;
public string[] affect;
public string[] covenable;
public string[] covincrement;
public string[] covtarget;
public string[] covlifetime;
public string[] historyenable;
public string[] historyincrement;
public string[] elapsedactivetime;
public string[] feedbackvalue;
public string[] rawvalue;
...//A lot more
}
我想给它们赋值,我想避免这样做:
ReadDiagnosticEntirePointValuesResponse response = new ReadDiagnosticEntirePointValuesResponse();
response.affect = new string[count];
response.alarmstate = new string[count];
response.correctvalue = new string[count];
response.covenable = new string[count];
response.covincrement = new string[count];
response.covlifetime = new string[count];
response.covtarget = new string[count];
response.elapsedactivetime = new string[count];
response.feedbackvalue = new string[count];
response.historyenable = new string[count];
response.historyincrement = new string[count];
response.outofservice = new string[count];
response.pointidentifier = new string[count];
response.presentvalue = new string[count];
response.priorityarray = new string[count];
response.rawvalue = new string[count];
...
当然,我可以在构造函数中编写这些初始化,但这仍然不能避免我必须手动初始化它们
避免这种情况的好方法是什么?您可以使用数组列表来避免声明大小。您可以使用数组列表来避免声明大小。至少需要手动初始化一次。如果C'tor不适合您,请使用新方法进行初始化。至少需要手动初始化一次。如果C'tor不适合您,请使用新方法进行初始化。您可能需要查看反射以了解这一点。。。在循环中阅读类的属性您可能需要查看该类的反射。。。在循环中读取类的属性可以使用:
public ReadDiagnosticEntirePointValuesResponse()
{
GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)
.ToList()
.ForEach(field => field.SetValue(this, new string[count]));
}
您可以使用来执行此操作:
public ReadDiagnosticEntirePointValuesResponse()
{
GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)
.ToList()
.ForEach(field => field.SetValue(this, new string[count]));
}
这是一种非常糟糕的数据管理方法,但是,下面的方法可以奏效 然而!我强烈建议你重新考虑这个设计。更好的机制是:
class DiagnosticPoint // TODO: rename as appropriate
{ // TODO: check these all need to be strings
public string Affect {get;set;}
public string AlarmState {get;set;}
...
public string RawValue {get;set;}
}
并将其数组作为字段:
public class ReadDiagnosticEntirePointValuesResponse
{
DiagnosticPoint[] points;
...
然后简单地初始化数组:
points = new DiagnosticPoint[count];
和init各:
for(int i = 0 ; i < count ; i++) points[i] = new DiagnosticPoint();
(etc)这是一种非常糟糕的数据管理方法,但是,下面的方法可以奏效 然而!我强烈建议你重新考虑这个设计。更好的机制是:
class DiagnosticPoint // TODO: rename as appropriate
{ // TODO: check these all need to be strings
public string Affect {get;set;}
public string AlarmState {get;set;}
...
public string RawValue {get;set;}
}
并将其数组作为字段:
public class ReadDiagnosticEntirePointValuesResponse
{
DiagnosticPoint[] points;
...
然后简单地初始化数组:
points = new DiagnosticPoint[count];
和init各:
for(int i = 0 ; i < count ; i++) points[i] = new DiagnosticPoint();
(etc)这听起来像是一个糟糕的类定义。也许使用
enum
会更好
如果您可以从以下内容开始:
public enum ReadDiagnostic
{
pointidentifier,
presentvalue,
priorityarray,
alarmstate,
outofservice,
correctvalue,
affect,
covenable,
covincrement,
covtarget,
covlifetime,
historyenable,
historyincrement,
elapsedactivetime,
feedbackvalue,
rawvalue,
}
然后,您可以创建一个字典
,以保存存储在类中的值
您甚至可以使用LINQ在一行代码中创建字典:
var readDiagnosticEntirePointValues =
typeof(ReadDiagnostic)
.GetEnumValues()
.Cast<ReadDiagnostic>()
.ToDictionary(x => x, x => new string[count]);
var readdiagnosticentirepointvalue=
类型(重新诊断)
.GetEnumValues()
.Cast()
.ToDictionary(x=>x,x=>newstring[count]);
这种方法仍然是强类型的,但比当前的方法更容易维护。这听起来像是一个糟糕的类定义。也许使用
enum
会更好
如果您可以从以下内容开始:
public enum ReadDiagnostic
{
pointidentifier,
presentvalue,
priorityarray,
alarmstate,
outofservice,
correctvalue,
affect,
covenable,
covincrement,
covtarget,
covlifetime,
historyenable,
historyincrement,
elapsedactivetime,
feedbackvalue,
rawvalue,
}
然后,您可以创建一个字典
,以保存存储在类中的值
您甚至可以使用LINQ在一行代码中创建字典:
var readDiagnosticEntirePointValues =
typeof(ReadDiagnostic)
.GetEnumValues()
.Cast<ReadDiagnostic>()
.ToDictionary(x => x, x => new string[count]);
var readdiagnosticentirepointvalue=
类型(重新诊断)
.GetEnumValues()
.Cast()
.ToDictionary(x=>x,x=>newstring[count]);
此方法仍然是强类型的,但比当前方法更易于维护。您可以使用以下方法:
response.affect = response.alarmstate = response... = new string[count];
但是,使用此选项仍会使您手动初始化它们。这只是一条捷径
正如其他人可能已经建议的那样,使用将使它更容易。我想推荐一种新的。以下是您可以如何实现它:
enum Foo { Affect, AlarmState, CorrectValue, ... }
public void InitializeArrays(int count)
{
Dictionary<Foo, string[]> response = new Dictionary<Foo, string[]>();
// easy initialization of string arrays
foreach (Foo foo in Enum.GetValues(typeof(Foo)))
{
response.Add(foo, new string[count]);
}
// use this to access the string arrays
response[Foo.Affect][0] = "test";
if (response[Foo.CorrectValue].Length > 0) { ... }
}
您可以使用以下选项:
response.affect = response.alarmstate = response... = new string[count];
但是,使用此选项仍会使您手动初始化它们。这只是一条捷径
正如其他人可能已经建议的那样,使用将使它更容易。我想推荐一种新的。以下是您可以如何实现它:
enum Foo { Affect, AlarmState, CorrectValue, ... }
public void InitializeArrays(int count)
{
Dictionary<Foo, string[]> response = new Dictionary<Foo, string[]>();
// easy initialization of string arrays
foreach (Foo foo in Enum.GetValues(typeof(Foo)))
{
response.Add(foo, new string[count]);
}
// use this to access the string arrays
response[Foo.Affect][0] = "test";
if (response[Foo.CorrectValue].Length > 0) { ... }
}
我建议你重新考虑你的设计。在这种情况下,看起来您只需要一个结构化数据数组。为什么不能使用
List
或List
,其中CustomClass
的所有数据都与@Konrad一致;YourTypeHere[]
其中YourTypeHere
定义PointIdentifier
,PresentValue
,AlarmState
等更可取。也;我怀疑它们是否都需要成为弦!这就是我得到的接口,它遵循一个称为SOAP的协议。我不能偏离它,但如果中间步骤涉及使用list(或任何其他类)来满足我的需要,那么也可以;这只是您选择在SOAP信封中布局数据的方式。事实上,您不能随意更改此布局(客户端和服务器必须在布局上达成一致),但是:在绿地系统上,SOAP中没有任何东西可以让您使用该布局。我建议您重新考虑您的设计。在这种情况下,看起来您只需要一个结构化数据数组。为什么不能使用List
或List
,其中CustomClass
的所有数据都与@Konrad一致;YourTypeHere[]
其中YourTypeHere
定义PointIdentifier
,PresentValue
,AlarmState
等更可取。也;我怀疑它们是否都需要成为弦!这就是我得到的接口,它遵循一个称为SOAP的协议。我不能偏离它,但如果中间步骤涉及使用list(或任何其他类)来满足我的需要,那么也可以;这只是您选择在SOAP信封中布局数据的方式。事实上,您不能随意更改此布局(客户端和服务器必须在布局上达成一致),但是:在绿地系统上,SOAP中没有任何东西可以让您使用该布局。但是,使用固定长度数组可能有一个很好的理由。可能需要精确的count
元素,不多不少。但是使用固定长度数组可能有一个很好的理由。可能需要精确的count
元素,不多不少。