2016-08-17 7 views
0

クラスからプロパティ名を取得したいとします。
私は、プロパティ名を取得するためにGetType()を使用していますが、親...を含むシステムのようなプロパティを生成gettypeからユーザー作成のプロパティを取得します。

public class Classtest : Activity 
    { 
     public int To { get; set; } 
     public int From { get; set; } 
    } 

      Classtest testclass = new Classtest(); 

      foreach (Activity activity in testclass.Activities) 
      { 
       Type type = activity.GetType(); 
       PropertyInfo[] propertyInfo = type.GetProperties(); 


       foreach (var p in propertyInfo) 
       {     
        Console.WriteLine(p.Name); 

        prpoertyCount--; 
       } 
      } 

出力を示している私だけFromToプロパティが、親、名前などの無システムプロパティを必要としています。あなたのケースでは

+0

答えはお役に立ちましたか? – Marusyk

答えて

1

はあなたがActivityから型を取得becasue基本クラスからプロパティを得た:activity.GetType();

あなたが最初Classtestの種類を取得し、配列PFのプロパティを取得する必要があります。ちょうど:

Classtest testclass = new Classtest(); 
var propertyInfo = testclass.GetType().GetProperties(System.Reflection.BindingFlags.Public 
| System.Reflection.BindingFlags.Instance 
| System.Reflection.BindingFlags.DeclaredOnly); 


foreach (var p in propertyInfo) 
{     
    Console.WriteLine(p.Name); 
} 

ここで私はいくつかのバインディングフラグを渡しました。あなたはそのバインディングフラグの詳細について読むことができますMSDN

関連する問題