2012-11-20 6 views
2

가능한 복제를 속성을 검색 :
Get property value from string using reflection in C#C#을 반사를 통해

나는 이메일과 문자로 데이터베이스에서 필드를 병합하는 응용 프로그램이 있습니다. 다른 사용자가 있기 때문에 병합 할 다른 필드를 요청합니다. 새 병합 필드를 만들 때 문서도 다시 작성해야합니다. 문제가 생겨서 문서를 자동화하고 싶습니다.

나는 (내가 stringField라는 단지 2 mergefields를 정의이 예에서,하지만 현재는 allmost 100의) 다음 코드를 내놓았다 :

namespace ReflectionTest 
{ 

    public class clReflection 
    { 
     private System.Data.DataTable dtClient = new System.Data.DataTable(); 

     public int ClientNumber { get; set; } 
     public int AdressNumber { get; set; } 



     public stringField ClientName 
     { 
      get 
      { 
       stringField _ClientName = new stringField(); 
       _ClientName.FieldContent = "Obama"; 
       _ClientName.FieldNote = "Last name of client"; 
       //Available email and available letter should be default true 
       return _ClientName; 
      } 
      set { } 
     } 

     public stringField ClientEmail 
     { 
      get 
      { 
       stringField _ClientEmail = new stringField(); 
       _ClientEmail.FieldContent = "[email protected]"; 
       _ClientEmail.FieldNote = "use only tested email adresses"; 
       _ClientEmail.AvailableLetter = false; 
       return _ClientEmail; 
      } 
      set 
      { } 
     } 



    } 

    public static class FindStringFields 
    { 
     public static System.Data.DataTable stringFields() 
     { 
      System.Data.DataTable dtStringFields = new System.Data.DataTable(); 
      dtStringFields.Columns.Add(new System.Data.DataColumn("FieldName", typeof(string))); 
      dtStringFields.Columns.Add(new System.Data.DataColumn("FieldNote", typeof(string))); 
      dtStringFields.Columns.Add(new System.Data.DataColumn("AvailableEmail", typeof(bool))); 

      clReflection test = new clReflection(); 
      System.Reflection.PropertyInfo[] props = test.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); 
      for (int i = 0; i < props.Length; i++) 
      { 
       if (props[i].PropertyType == typeof(stringField)) 
       { 
        dtStringFields.Rows.Add(new object[] { props[i].Name , "FieldNote", true }); 

       } 
      } 
      return dtStringFields; 
     } 
    } 


    public class stringField 
    { 
     private bool _AvailableEmail = true; 
     private bool _AvailableLetter = true; 


     public string FieldContent { get; set; } 
     public string FieldNote { get; set; } 
     public bool AvailableEmail 
     { 
      get { return _AvailableEmail; } 
      set { AvailableEmail = value; } 

     } 

     public bool AvailableLetter 
     { 
      get { return _AvailableLetter; } 
      set { _AvailableLetter = value; } 
     } 

    } 
} 

당신이 명령을 해고하는 경우 : System.Data.DataTable dtTest = FindStringFields.stringFields(); 모든 정의 된 stringField를 사용하여 데이터 테이블을 가져옵니다. 그러나 stringfield의 이름을 제외하고는 stringfield의 속성을 검색 할 수 없습니다. 발견 된 stringField를 인스턴스화하여 다른 속성을 검색 할 수 있습니까?

감사합니다,

+0

내 문제에 답하는 데 노력을 기울이는 모든 사람에게 감사드립니다. Matias Fidemraizer가 준 포인터는 정확하고 효과적입니다! – RobRdam

답변

0

사용 Type.GetProperties 방법은 또한 당신이 BindingFlags의를 지정할 수 있습니다 과부하가

Type type = typeof(stringField); 
PropertyInfo[] properties = type.GetProperties(); 

유형

의 모든 공용 속성을 얻을 수 있습니다.

그럼 당신은 속성의 값을 얻을 수 있습니다 :

object value = property.GetValue(_ClientEmail); 
0

당신이 문자열 필드 이름이 함께 시도가있는 경우 :

public static object GetPropValue(object src, string propName) 
{ 
    return src.GetType().GetProperty(propName).GetValue(src, null); 
} 

이 SO 게시물에서 추출 :

Get property value from string using reflection in C#

+0

정확한 복제본 인 경우 "기존 질문의 정확한 복제본"이므로 닫는 것이 좋습니다. D –

+0

정확히 동일하게 수행 할 것인지 확실하지 않습니다. Thanks @ Matías Fidemraizer –

+0

흠, 질문을주의 깊게 읽으십시오 !! ;) –

0

다음과 같이 할 수 있습니다.

//its better to use a property than get it from the array every time! 
    PropertyInfo pi = prop[i]; 

    //get the underlying value 
    stringField underlyingStringField = prop.GetValue(test, null) as stringField; 

    //use the underlying value now 
    Debug.Write(underlyingStringField.AvalilableEmail); 

    ... 
관련 문제