[英]How do I find objects of a certain type in a Class in C#?
I am going to assume that I have to use System.Reflection
. 我假设我必须使用System.Reflection
。 However when I want to build a list of typeA
that have been defined before, how would I build that list? 但是,当我要构建以前定义的typeA
列表时,如何构建该列表?
public TypeA foo;
System.Attribute.GetCustomAttributes?
I'm not really sure how to better phrase the question. 我不确定如何更好地表达这个问题。 I'm just looking to build a list of all the TypeA
occurrences in my class. 我只是想建立一个类中所有TypeA
事件的列表。
From the above comments it seems like you already know what you want to do. 从以上评论看来,您似乎已经知道您想做什么。 But heres a quick example 但是这是一个简单的例子
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.