反對我有一個擴展的通用方法獲取參考從C#表達
public static void AddError<TModel>(
this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression,
string resourceKey,
string defaultValue)
{
// How can I get a reference to TModel object from expression here?
}
我需要從表達的參考TModel的對象。 該方法通過下面的代碼名爲:
ModelState.AddError<AccountLogOnModel>(
x => x.Login, "resourceKey", "defaultValue")
有一個表達式沒有這樣的對象 - 'x'是一個參數的表達,你應該將類型的對象傳遞給它。 (或者我正在理解你想要實現的錯誤。) – millimoose 2012-01-09 19:14:27
感謝您的回覆,Inerdial) – 2012-01-09 19:18:23
您是否真的希望爲AddModelError(key,errorMessage)方法使用對象或文本'Login'?使用'ExpressionHelper.GetExpressionText'(內置於MVC)從lambda表達式獲取屬性名稱。 – kamranicus 2012-01-09 19:24:47