我发现我试图对我的资源类型错误地使用反射,并且能够创建一些方法来获取正确的资源:
public string GetLabel(string control)
{
string strResourceName;
try
{
AddressInfoField field = AddressFields.First(f => f.m_strControl == control);
strResourceName = field.m_strName;
}
catch // Catch everything
{
return string.Empty;
}
if (string.IsNullOrEmpty(strResourceName))
return string.Empty;
return GetResource(strResourceName);
}
public string GetValidationMessage(string control)
{
string strResourceName;
try
{
AddressInfoField field = AddressFields.First(f => f.m_strControl == control);
strResourceName = field.m_strName;
}
catch // Catch everything
{
return Addressing.Required;
}
if (string.IsNullOrEmpty(strResourceName))
return Addressing.Required;
return GetResource(strResourceName + "Required");
}
private static string GetResource(string strResourceName)
{
PropertyInfo property = typeof (Addressing).GetProperty(strResourceName, BindingFlags.Public | BindingFlags.Static);
if (property == null)
throw new InvalidOperationException("Could not locate a resource for Addressing." + strResourceName);
if (property.PropertyType != typeof(string))
throw new InvalidOperationException("The requested resource does not return a string.");
return (string) property.GetValue(null, null);
}
<li id="city" if="Model.IsCityVisible">
<label for="City">${Model.GetLabel("City")}</label>
${Html.EditorFor(x => x.City)}
!{Html.ValidationMessage("City", Model.GetValidationMessage("City"))}
</li>