Skip to main content
 首页 » 编程设计

c#之通过属性值查找对象实例的属性

2024年02月13日11linjiqin

我有一个看起来像这样的 DTO:

public class MyDto 
{ 
    [MyAttribute("attribute1")] 
    public string Property1 {get;set;} 
 
    [MyAttribute("attribute2")] 
    public string Property2 {get;set;} 
} 

如果我有 string“attribute1”,我如何使用它来获取 MyDto 实例中 Property1 的值?

请您参考如下方法:

使用Reflection .遗憾的是,无法从属性中获取属性:您必须遍历每个属性并检查其属性。