2016-08-27 19 views
0

私はSystem.Reflectionを使用しなければならないと仮定します。しかし、前に定義したtypeAのリストを作成したい場合は、そのリストをどのように構築すればよいでしょうか?C#でクラス内の特定のタイプのオブジェクトを検索するにはどうすればよいですか?

public TypeA foo; 
System.Attribute.GetCustomAttributes? 

私は実際に質問をどのように改善するかわかりません。私はちょうど私のクラスのすべてのTypeAオカレンスのリストを作成するために探しています。

+0

あなたのクラスのプロパティを列挙し、タイプAのものをフィルタリングしますか? –

+0

Gerardo、はい、まさに私がしたいことです。 –

+0

これは私が探しているものですか? typeof(Foo).GetProperties(); –

答えて

1

上記のコメントから、あなたがしたいことを既に知っているようです。しかし、簡単な例を聞いて

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; } 
} 
関連する問題