C#,创建类字段集合
有没有办法创建现有类字段的集合 我有一个类,它有许多字段/属性,这些字段/属性的类型不同,名称有意义C#,创建类字段集合,c#,collections,properties,field,C#,Collections,Properties,Field,有没有办法创建现有类字段的集合 我有一个类,它有许多字段/属性,这些字段/属性的类型不同,名称有意义 class ExampleClass { public string meaningfulName1 { get; set;} public double meaningfulName2 { get; set;} ... public myOtherClass meaningfulNameN { get; set;} } 我需要从外部(不是我的)程序中生成的文件中读取这
class ExampleClass
{
public string meaningfulName1 { get; set;}
public double meaningfulName2 { get; set;}
...
public myOtherClass meaningfulNameN { get; set;}
}
我需要从外部(不是我的)程序中生成的文件中读取这些属性的值
由于有很多字段/属性,读取值并逐个赋值似乎效率低下。因此,我需要这些字段/属性的集合。差不多
foreach (fieldReference in ExampleClass.fieldReferenceCollection)
{
readValueFromFile(fieldReference);
}
但是我如何在保留所有名字的同时制作一个呢
使用所有参数值而不是单独的字段创建集合似乎是合乎逻辑的,但是字段名将丢失。而且,考虑到字段的数量,如果可能的话,我们希望保留这些名称以简化进一步的开发
因此,我需要单独的字段/属性以及它们的集合同时可用
字典集合不是很快,所以参数名作为值的键似乎也不太合适
我发现的另一个选项是反射,但我还不确定反射集合中字段的顺序是如何确定的。字段的顺序非常重要,因为我从中读取值的文件没有元数据,只有一系列十六进制值。另外,反射对于从文件中读取值来说似乎有些过分,而且速度也很慢
所以问题是:为了同时拥有类字段和它们的集合,我应该做什么
我对这项任务的假设是错误的吗?有没有其他方法可以将大量哑值从文件读入复杂对象
还有,我的第一个问题是,英语是我的第二语言,所以我为我的错误感到抱歉。看来你必须用反思来做你想做的事。至于确定字段的顺序,我建议使用自定义属性标记属性 这里有一个例子。希望这能引导您找到所需的解决方案
namespace ConsoleApplication2
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class FileField : Attribute
{
public int Index { get; set; }
public FileField() { }
}
class ExampleClass
{
[FileField(Index = 0)]
public string meaningfulName1 { get; set; }
[FileField(Index = 2)]
public double meaningfulName2 { get; set; }
[FileField(Index = 1)]
public MyOtherClass meaningfulNameN { get; set; }
}
class MyOtherClass
{
public string Something { get; set; }
}
class Program
{
static void Main(string[] args)
{
var exampleClassFieldProperties = GetExampleClassFieldProperties();
var lines = File.ReadAllLines("datafile.txt");
var records = new List<ExampleClass>();
foreach (var line in lines)
{
var fields = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var record = new ExampleClass();
for(int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
{
if (exampleClassFieldProperties.ContainsKey(fieldIndex))
{
ReadValueFromFile(fields[fieldIndex], exampleClassFieldProperties[fieldIndex], record);
}
}
records.Add(record);
}
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
public static Dictionary<int, PropertyInfo> GetExampleClassFieldProperties()
{
var exampleClassFieldProperties = new Dictionary<int, PropertyInfo>();
var properties = typeof(ExampleClass).GetProperties();
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(false);
int index = 0;
foreach (var attribute in attributes)
{
if (attribute is FileField)
{
index = ((FileField)attribute).Index;
if (exampleClassFieldProperties.ContainsKey(index) == false)
{
exampleClassFieldProperties.Add(index, property);
}
}
}
}
return exampleClassFieldProperties;
}
public static void ReadValueFromFile(string field, PropertyInfo exampleClassField, ExampleClass record)
{
if (exampleClassField.PropertyType.Name == typeof(string).Name)
{
record.GetType().InvokeMember(exampleClassField.Name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder, record, new object[] { field });
}
else if (exampleClassField.PropertyType.Name == typeof(double).Name)
{
record.GetType().InvokeMember(exampleClassField.Name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder, record, new object[] { double.Parse(field) });
}
else if (exampleClassField.PropertyType.Name == typeof(MyOtherClass).Name)
{
var other = new MyOtherClass();
// TO DO: Parse field to set properties in MyOtherClas
record.GetType().InvokeMember(exampleClassField.Name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder, record, new object[] { other });
}
}
}
}
命名空间控制台应用程序2
{
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
公共密封类文件字段:属性
{
公共int索引{get;set;}
公共文件字段(){}
}
类示例类
{
[文件字段(索引=0)]
有意义的公共字符串name1{get;set;}
[文件字段(索引=2)]
公共双意义名称2{get;set;}
[文件字段(索引=1)]
公共MyOtherClass表示名称{get;set;}
}
类MyOtherClass
{
公共字符串Something{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
var exampleClassFieldProperties=GetExampleClassFieldProperties();
var lines=File.ReadAllLines(“datafile.txt”);
var记录=新列表();
foreach(行中的var行)
{
var fields=line.Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
var记录=新的ExampleClass();
for(int fieldIndex=0;fieldIndex
不要仅仅假设解决方案太慢,而是编写它们并找出它们是否太慢。如果是,那么你可以看看如何解决这些问题,如果不是,你有你的解决方案?XML,JSO