2016-06-11 9 views
0

異なるクラスのC#.exeファイルがあります。リフレクション(アセンブリ)を使用してクラスプロパティを取得する方法

実行可能ファイル内のクラス内にあるプロパティを取得する必要があります。

は今Imはこれをやって:

Assembly a = Assembly.LoadFile(servicePath); 
      foreach (Type t in a.GetTypes()) 
      { 
       Console.WriteLine(t.Name); 
      } 

そして、私はクラスがコンソールに書き込みされていることを見ることができ、今どのように私はby the way I don't and won't have that class referenced in my projectそのクラスのプロパティを取得することができますか?

これは私のクラスである:

/// <summary> 
    /// Summary description for ProjectInstaller. 
    /// </summary> 
    [RunInstaller(true)] 
    public class SyncServiceInstaller : System.Configuration.Install.Installer 
    { 
     private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; 
     private System.ServiceProcess.ServiceInstaller serviceInstaller1; 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.Container components = null; 

     public SyncServiceInstaller() 
     { 
      // This call is required by the Designer. 
      InitializeComponent(); 

     } 
} 

私はそのクラス内でこの変数を取得する必要があります:serviceInstaller1

任意の手掛かり?

答えて

0

アセンブリをロードしてから、探しているアセンブリ以外のアセンブリを除外してから、結果のアセンブリでGetField("...")を呼び出します。 取得しようとしている項目が、プロパティではない項目です。

FieldInfo field = AppDomain.CurrentDomain.GetAssemblies() 
         .FirstOrDefault(n => n.GetType().Equals(typeof(SyncServiceInstaller)))?.GetType() 
         .GetField("serviceInstaller1", BindingFlags.Instance 
                | BindingFlags.Public 
                | BindingFlags.NonPublic); 
+0

あなたは '.Where()' ')(' .FIrstOrDefaultと交換し、あなたが持っている '.FirstOrDefault()'の呼び出しを削除することができます。 –

+0

@Mattいいアイデア! – Rickey

関連する問題