如何在c#的类中找到特定类型的对象?

本文关键字:类型 对象 | 更新日期: 2023-09-27 18:04:20

我将假设我必须使用System.Reflection。然而,当我想要构建一个之前已经定义的typeA列表时,我该如何构建这个列表呢?

public TypeA foo;
System.Attribute.GetCustomAttributes?
我真的不知道如何更好地表达这个问题。我只是想建立一个列表的所有TypeA出现在我的类。

如何在c#的类中找到特定类型的对象?

从上面的评论来看,你似乎已经知道你想做什么了。这里有一个简单的例子

class Program
{
    static void Main(string[] args)
    {
        var properties = typeof(MyObject).GetProperties();
        var stringsInMyObject = properties.Where(x=> x.GetMethod.ReturnType == typeof(string));
    }
}
public class MyObject
{
    public string SomeString { get; set; }
    public string SomeString2 { get; set; }
    public int SomeInt { get; set; }
    public int SomeInt2 { get; set; }
}