Could i extension the ModelStateDictionary Class in MVC3
in the ModelStateDictionary class there are only AddModelError and Add function , i want extention the class, add the method like AddModeSuccess,AddModelWarning. i have a look at the MVC3 source code and found there a lot of thing need add. i don't want to modify the MVC3 code, i just want to add a extention. how could i do?
public void Add(KeyValuePair<string, ModelState> item) {
((IDictionary<string, ModelState>)_innerDictionary).Add(item);
}
public void Add(string key, ModelState value) {
_innerDictionary.Add(key, value);
}
public void AddModelError(string key, Exception exception) {
GetModelStateForKey(key).Errors.Add(exception);
}开发者_如何学Python
public void AddModelError(string key, string errorMessage) {
GetModelStateForKey(key).Errors.Add(errorMessage);
}
You could add them as extension methods to the ModelStateDictionary
class:
public static class ModelStateExtensions
{
public static void AddModelSuccess(this ModelStateDictionary modelState, ... some parameters)
{
...
}
public static void AddModelWarning(this ModelStateDictionary modelState, ... some parameters)
{
...
}
}
精彩评论