C# 通过匹配类的属性来填充类
所以我有一个导入的文件。基本上,我想使用这个文件的头来知道哪些列应该放在类的哪个变量值中。我想通过C中的变量属性来进行比较,但我不确定如何处理和设置C# 通过匹配类的属性来填充类,c#,.net,reflection,attributes,C#,.net,Reflection,Attributes,所以我有一个导入的文件。基本上,我想使用这个文件的头来知道哪些列应该放在类的哪个变量值中。我想通过C中的变量属性来进行比较,但我不确定如何处理和设置 例如,假设我的类中有一个变量是publicstringname;在导入的文件中,列标题之一是Name。我不希望使用反射直接匹配变量。如何在类变量上设置一个属性,然后使用它与这些本地字符串头变量匹配,并填充正确的变量?下面是一个示例程序,该程序可以满足您的需要。SetOption方法提供反射逻辑,以查找具有指定选项名称的字段并设置其值 using S
例如,假设我的类中有一个变量是publicstringname;在导入的文件中,列标题之一是Name。我不希望使用反射直接匹配变量。如何在类变量上设置一个属性,然后使用它与这些本地字符串头变量匹配,并填充正确的变量?下面是一个示例程序,该程序可以满足您的需要。SetOption方法提供反射逻辑,以查找具有指定选项名称的字段并设置其值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ConsoleApplication1
{
// This is the attribute that we will apply to the fields
// for which we want to specify an option name.
[AttributeUsage(AttributeTargets.Field)]
public class OptionNameAttribute : Attribute
{
public OptionNameAttribute(string optionName)
{
OptionName = optionName;
}
public string OptionName { get; private set; }
}
// This is the class which will contain the option values that
// we read from the file.
public class OptionContainer
{
[OptionName("Name")]
public string MyNameField;
[OptionName("Value")]
public string MyValueField;
}
class Program
{
// SetOption is the method that assigns the value provided to the
// field of the specified instance with an OptionName attribute containing
// the specified optionName.
static void SetOption(object instance, string optionName, string optionValue)
{
// Get all the fields that has the OptionNameAttribute defined
IEnumerable<FieldInfo> optionFields = instance.GetType()
.GetFields()
.Where(field => field.IsDefined(typeof(OptionNameAttribute), true));
// Find the single field where the OptionNameAttribute.OptionName property
// matches the provided optionName argument.
FieldInfo optionField = optionFields.SingleOrDefault(field =>
field.GetCustomAttributes(typeof(OptionNameAttribute), true)
.Cast<OptionNameAttribute>().Single().OptionName.Equals(optionName));
// If the found field is null there is no such option.
if (optionField == null)
throw new ArgumentException(String.Format("Unknown option {0}", optionName), "optionname");
// Finally set the value.
optionField.SetValue(instance, optionValue);
}
static void Main(string[] args)
{
OptionContainer instance = new OptionContainer();
SetOption(instance, "Name", "This is the value of Name");
SetOption(instance, "Value", "This is my value");
Console.WriteLine(instance.MyNameField);
Console.WriteLine(instance.MyValueField);
}
}
}
下面是一个示例程序,它将为您提供所需的内容。SetOption方法提供反射逻辑,以查找具有指定选项名称的字段并设置其值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ConsoleApplication1
{
// This is the attribute that we will apply to the fields
// for which we want to specify an option name.
[AttributeUsage(AttributeTargets.Field)]
public class OptionNameAttribute : Attribute
{
public OptionNameAttribute(string optionName)
{
OptionName = optionName;
}
public string OptionName { get; private set; }
}
// This is the class which will contain the option values that
// we read from the file.
public class OptionContainer
{
[OptionName("Name")]
public string MyNameField;
[OptionName("Value")]
public string MyValueField;
}
class Program
{
// SetOption is the method that assigns the value provided to the
// field of the specified instance with an OptionName attribute containing
// the specified optionName.
static void SetOption(object instance, string optionName, string optionValue)
{
// Get all the fields that has the OptionNameAttribute defined
IEnumerable<FieldInfo> optionFields = instance.GetType()
.GetFields()
.Where(field => field.IsDefined(typeof(OptionNameAttribute), true));
// Find the single field where the OptionNameAttribute.OptionName property
// matches the provided optionName argument.
FieldInfo optionField = optionFields.SingleOrDefault(field =>
field.GetCustomAttributes(typeof(OptionNameAttribute), true)
.Cast<OptionNameAttribute>().Single().OptionName.Equals(optionName));
// If the found field is null there is no such option.
if (optionField == null)
throw new ArgumentException(String.Format("Unknown option {0}", optionName), "optionname");
// Finally set the value.
optionField.SetValue(instance, optionValue);
}
static void Main(string[] args)
{
OptionContainer instance = new OptionContainer();
SetOption(instance, "Name", "This is the value of Name");
SetOption(instance, "Value", "This is my value");
Console.WriteLine(instance.MyNameField);
Console.WriteLine(instance.MyValueField);
}
}
}
如果没有反射,您无法查找类成员的属性,或者我误解了什么?我想我的意思是,我不想使用反射来查找确切的变量名,而是查找属性名并查看它们映射到的变量。这有意义吗?这确实更有意义如果没有反射,您无法查找类成员的属性,或者我误解了什么?我想我的意思是,我不想使用反射来查找确切的变量名,而是查找属性名并查看它们映射到的变量。这有意义吗?这确实更有意义