C# 通过对象实例查找属性';属性值
我有一个DTO,看起来像这样:C# 通过对象实例查找属性';属性值,c#,reflection,C#,Reflection,我有一个DTO,看起来像这样: public class MyDto { [MyAttribute("attribute1")] public string Property1 {get;set;} [MyAttribute("attribute2")] public string Property2 {get;set;} } 如果我有字符串“attribute1”,我如何在MyDto的实例中使用它来获得Property1的值?使用。不幸的是,无法从属性获取属
public class MyDto
{
[MyAttribute("attribute1")]
public string Property1 {get;set;}
[MyAttribute("attribute2")]
public string Property2 {get;set;}
}
如果我有
字符串
“attribute1”,我如何在MyDto
的实例中使用它来获得Property1
的值?使用。不幸的是,无法从属性获取属性:您必须遍历每个属性并检查其属性。使用。不幸的是,无法从属性获取属性:您必须遍历每个属性并检查其属性。这不是最健壮的代码,但请尝试以下方法:
public class MyAttributeAttribute : Attribute
{
public MyAttributeAttribute(string value)
{
Value=value;
}
public string Value { get; private set; }
}
public class MyDto
{
[MyAttribute("attribute1")]
public string Property1 { get; set; }
[MyAttribute("attribute2")]
public string Property2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyDto dto=new MyDto() { Property1="Value1", Property2="Value2" };
string value=GetValueOf<string>(dto, "attribute1");
// value = "Value1"
}
public static T GetValueOf<T>(MyDto dto, string description)
{
if(string.IsNullOrEmpty(description))
{
throw new InvalidOperationException();
}
var props=typeof(MyDto).GetProperties().ToArray();
foreach(var prop in props)
{
var atts=prop.GetCustomAttributes(false);
foreach(var att in atts)
{
if(att is MyAttributeAttribute)
{
string value=(att as MyAttributeAttribute).Value;
if(description.Equals(value))
{
return (T)prop.GetValue(dto, null);
}
}
}
}
return default(T);
}
}
公共类MyAttributeAttribute:属性
{
公共MyAttributeAttribute(字符串值)
{
价值=价值;
}
公共字符串值{get;private set;}
}
公共类MyDto
{
[MyAttribute(“attribute1”)]
公共字符串属性1{get;set;}
[MyAttribute(“attribute2”)]
公共字符串属性2{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
MyDto dto=新的MyDto(){Property1=“Value1”,Property2=“Value2”};
字符串值=GetValueOf(dto,“attribute1”);
//value=“Value1”
}
公共静态T GetValueOf(MyDto dto,字符串描述)
{
if(string.IsNullOrEmpty(description))
{
抛出新的InvalidOperationException();
}
var props=typeof(MyDto).GetProperties().ToArray();
foreach(道具中的var道具)
{
var atts=prop.GetCustomAttributes(false);
foreach(var att在atts中)
{
如果(att是MyAttributeAttribute)
{
字符串值=(att作为MyAttributeAttribute).value;
if(描述等于(值))
{
返回(T)prop.GetValue(dto,null);
}
}
}
}
返回默认值(T);
}
}
不是最健壮的代码,但请尝试以下方法:
public class MyAttributeAttribute : Attribute
{
public MyAttributeAttribute(string value)
{
Value=value;
}
public string Value { get; private set; }
}
public class MyDto
{
[MyAttribute("attribute1")]
public string Property1 { get; set; }
[MyAttribute("attribute2")]
public string Property2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyDto dto=new MyDto() { Property1="Value1", Property2="Value2" };
string value=GetValueOf<string>(dto, "attribute1");
// value = "Value1"
}
public static T GetValueOf<T>(MyDto dto, string description)
{
if(string.IsNullOrEmpty(description))
{
throw new InvalidOperationException();
}
var props=typeof(MyDto).GetProperties().ToArray();
foreach(var prop in props)
{
var atts=prop.GetCustomAttributes(false);
foreach(var att in atts)
{
if(att is MyAttributeAttribute)
{
string value=(att as MyAttributeAttribute).Value;
if(description.Equals(value))
{
return (T)prop.GetValue(dto, null);
}
}
}
}
return default(T);
}
}
公共类MyAttributeAttribute:属性
{
公共MyAttributeAttribute(字符串值)
{
价值=价值;
}
公共字符串值{get;private set;}
}
公共类MyDto
{
[MyAttribute(“attribute1”)]
公共字符串属性1{get;set;}
[MyAttribute(“attribute2”)]
公共字符串属性2{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
MyDto dto=新的MyDto(){Property1=“Value1”,Property2=“Value2”};
字符串值=GetValueOf(dto,“attribute1”);
//value=“Value1”
}
公共静态T GetValueOf(MyDto dto,字符串描述)
{
if(string.IsNullOrEmpty(description))
{
抛出新的InvalidOperationException();
}
var props=typeof(MyDto).GetProperties().ToArray();
foreach(道具中的var道具)
{
var atts=prop.GetCustomAttributes(false);
foreach(var att在atts中)
{
如果(att是MyAttributeAttribute)
{
字符串值=(att作为MyAttributeAttribute).value;
if(描述等于(值))
{
返回(T)prop.GetValue(dto,null);
}
}
}
}
返回默认值(T);
}
}
这基本上是以同样的方式解决的,这基本上是以同样的方式解决的,这很好。阿德里安当然也是对的,但看到一个例子来帮助思考解决方案总是很好的。那太好了。阿德里安当然也是对的,但看到一个例子来帮助思考解决方案总是很好的。