2013-08-07 1 views
0

모두 다른 언어로 응용 프로그램을 디버깅하기위한 즉석 메커니즘 제공 외국어로 필요한 리소스 문자열을 사용하여 런타임시 사용자가 요구할 때 영어와 동등합니다. 이것은 GetName 내가 일반적으로 지금런타임에 얻은 변수 이름을 사용하는 방법

Utils.ErrMsg(MessageStrings.SomeMessage); 

또는

Utils.ErrMsg(String.Format(MessageStrings.SomeMessage, param1, param2)); 

처럼 내 응용 프로그램에서 지역화 된 메시지를 내가 할 수있는 표시

public static string GetName<T>(Expression<Func<T>> expression) 
{ 
    return ((MemberExpression)expression.Body).Member.Name; 
} 

으로 정의된다

public static string GetMessage(string messageKey) 
{ 
    CultureInfo culture = Thread.CurrentThread.CurrentCulture; 
    if (!culture.DisplayName.Contains("English")) 
    { 
     string fileName = "MessageStrings.resx"; 
     string appDir = Path.GetDirectoryName(Application.ExecutablePath); 
     fileName = Path.Combine(appDir, fileName); 
     if (File.Exists(fileName)) 
     { 
      // Get the English error message. 
      using (ResXResourceReader resxReader = new ResXResourceReader(fileName)) 
      { 
       foreach (DictionaryEntry e in resxReader) 
        if (e.Key.ToString().CompareNoCase(messageKey) == 0) 
         return e.Value.ToString(); 
      } 
     } 
    } 
    return null; 
} 

를 사용하여 수행됩니다 disp 나는 어떻게

Utils.ErrMsg(Utils.GetMessage(
    Utils.GetName(() => MessageStrings.ErrCellAllocStatZeroTotal)) ?? 
     MessageStrings.ErrCellAllocStatZeroTotal);  

내가 GetName 호출 및 ??를 사용하여 GetMessage과에서 null의 사용에 람다 표현식을 사용하지 않으려 사용하여 다른 문화에서 실행 내 응용 프로그램에서와 관계있는 영어 메시지를 수 누워 이것을 달성 할 수 있다면?

감사합니다.

나는 완전히 당신의 코드를 이해하지만 그냥 동적 객체의 속성에 액세스하려면, (당신이 특정 값으로 [오브젝트]와 "PROPERTYNAME"교체해야)이 시도하지 않는
+0

어떻게'Utils.GetMessage'에서 null이 아닌'messageKey' (원래 값,'GetName'의 반환 값을 가진 덮어 쓰지 않은 버전)을 반환하지 않습니까? 나는 뭔가를 놓치고 있어야합니다. 그래서 제발 저를 밝혀주십시오. –

+0

나는 약간 자신보다 앞서서 질문을 편집해야했습니다. 내가하고있는 것처럼'MessageString' 인스턴스를 전달할 수 있습니다. 그렇지 않으면 reflection이'GetMessage'에 지역 변수 이름을 반환합니다. 이제 두 가지 문제가 있습니다 - 호출에서 람다를 피하고 null 병합 연산자를 사용하지 않아도됩니다! 시간 내 줘서 고마워. – MoonKnight

답변

1

:

// get the property from object 
PropertyInfo Property = [Object].GetType().GetProperty("PropertyName"); 

// get the value 
int value = (int)Property.GetValue([Object], null); 
관련 문제