2012-01-26 4 views
-1

초기화되지 않은 변수가있는 주 변수가 있습니다. 리플렉션으로 초기화되지 않은 가변 유형 유형을 검색해야합니다. 나는 값을 동적으로 생성하고 있기 때문에 주요 변수 유형의 변수를 얻을 수 없습니다.초기화되지 않은 변수 유형 가져 오기

그림에서 빠른 시계는 초기화되지 않았어도 변수 이름이 ameliyatGirisBilgileri 인 것으로 표시됩니다.

as shown in Type field of QuickWatch

+1

그 그림은 읽을 수 없으며 설명도 이해하기가 쉽지 않습니다. 당신이하고자하는 것을 보여주는 짧지 만 완전한 프로그램을 주시겠습니까? –

+0

당신이있는 동안 아마 ameliyatGirisBilgileri의 이름을 다른 것으로 바꾼다 – Adrian

+0

만약 이것이 지역 변수라면 반성은 쓸모 없게 될 것입니다. 클래스 필드 나 속성을 의미합니까? –

답변

0
FieldInfo fieldInfo = typeof(MyClass).GetField("ameliyatGirisBilgileri", BindingFlags.Public | BindingFlags.Instance); 
Type fieldType = fieldInfo.FieldType; 

미안하지만 클래스 이름을 완전히 입력하는 것은 너무 습관적입니다.

0

당신은 주요 유형에 GetField(...) 또는 GetFields(...) 방법을 사용하여 유형 내 변수에 대한 FieldInfo을 얻을 수 있어야합니다. 아래는 쇼트 프로그램은 당신이 그것에 대해 이동하는 방법을 시연 :

class Program 
{ 
    public string mStringType = null; 

    static void Main(string[] args) 
    { 
     var program = new Program(); 

     try 
     { 
      var field = program.GetType().GetField("mStringType"); 

      Console.WriteLine("Field '{0}' is of type '{1}' and has value '{2}'.", field.Name, field.FieldType.FullName, field.GetValue(program)); 

      program.mStringType = "Some Value"; 

      Console.WriteLine("Field '{0}' is of type '{1}' and has value '{2}'.", field.Name, field.FieldType.FullName, field.GetValue(program)); 
     } 
     catch (NullReferenceException) 
     { 
      Console.WriteLine("Error"); 
     } 

     Console.ReadKey(); 
    } 
} 

이 콘솔 창에 다음과 같은 출력을 제공합니다

필드 'mStringType은'형 '선택 System.String'이며이 값 ''.

'mStringType'필드의 형식이 'System.String'이고 값이 'Some Value'입니다.

참고 : 필드가없는 public을 경우, 당신은 GetField(...) 또는 GetFields(...) 방법으로 일부 BindingFlags을 통과해야합니다.