2016-07-05 3 views
0

내 프로젝트를 사용하여 윈폼의 C#에서 필드에 대한 값 설정이 얼마나 :.이 예는 제대로반사를

DTO_Right.GetType() GetProperty를 (PROPERTYNAME) .SetValue을 (

private clsDTO_Error DTO_Error; --> Have PropertyName = ID 
private clsDTO_Right DTO_Right; 
FieldInfo f = this.GetType().GetField(DTO_Error, BindingFlags.NonPublic | BindingFlags.Instance); 

을하고있다 DTO_Right, "OK", null); .

또는

f.FieldType.GetProperty ("ID") SetValue는 (DTO_Error, "OK", NULL);

나는이 문제가 :

f.FieldType.GetProperty ("ID")를 SetValue를 ("ERROR"f.FieldType, 널 (null)).

어떤 아이디어가 있습니까?

감사합니다!

+1

를 사용하여 내 대답이있다. 'DTO_Right'에 대해서했던 것처럼'DTO_Error'에 대해서 같은 코드를 적용 할 수 있습니다 (위의 코드에서 볼 수있는 한 : DTO_Error의 타입을 얻고, 속성을 찾고, SetValue를 호출합니다). 그 속성에 'DTO_Error' 인스턴스와 속성 값을 제공하거나, 여기에 spanners를 던지지 않도록 DLR을 사용하십시오 :'dynamic errorDto = (dynamic) DTO_Error; errorDto.ID = value;' –

+0

다음에 호출 할 필드를 알고 있으므로 FieldInfo를 사용합니다. 그리고 DTO_Right는 정확히 알고 있습니다. –

+0

---------------- 나는 그것을 얻지 못합니다. 많은 반복 작업이 필요합니다. 이런 식으로 사용하십시오 –

답변

0

이 나는 ​​특성이 반영이 구분에 대한 관심, 필드와 동일하지 참고 마십시오 Directionary

private Dictionary<string, object> myDictionary = new Dictionary<string, object>(); 
//Add this in Form Load 
myDictionary.Add("DTO_Error", DTO_Error); 
//Set Value 
PropertyInField.SetValue(myDictionary[DTO_Error], "OK", null); 
+0

[MSDN 데이터 바인딩] (https://msdn.microsoft.com/en-us/library/ef2xyb33(v=vs.110) .aspx) –

0

어쩌면 :.

f.FieldType.GetProperty ("ID") SetValue는 (이, "ERROR", NULL);

+0

오류 !!! SetValue (this, "Test", null) ''System.Reflection.TargetException '형식의 예외를 throw했습니다. \t void {System.Reflection.TargetException}'( ' 'f.FieldType.GetProperty –