2016-06-11 2 views
0

다른 클래스가있는 C# .exe 파일이 있습니다.리플렉션 (어셈블리)을 사용하여 클래스 속성을 얻는 방법

실행 파일 내에서 클래스 내에있는 속성을 가져와야합니다.

가 지금 임은이 일을 :

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

당신은 그'어디에요()'와'.FIrstOrDefault()'를 교체하고 제거 할 수있는'.FirstOrDefault()'당신이 가진 것을 요청합니다. –

+0

@Matt 좋은 생각! – Rickey

관련 문제