取这两个代码件事:C#:TypeDescriptor.GetAttributes()和GetType().GetCustomAttributes之间的区别是什么?
instance.GetType()
.GetCustomAttributes(true)
.Where(item => item is ValidationAttribute);
而且
TypeDescriptor.GetAttributes(instance)
.OfType<ValidationAttribute>();
如果类的样子:
[RequiredIfOtherPropertyIsNotEmpty("State", "City", ErrorMessage = ErrorDescription.CreateAccount_CityRequiredWithState)]
[RequiredIfOtherPropertyIsNotEmpty("State", "Address1", ErrorMessage = ErrorDescription.CreateAccount_Address1RequiredWithState)]
public class ManagePostModel
{
...
}
凡RequiredIfOtherPropertyIsNotEmpty
是ValidationAttribute
并具有AllowMultiple = true
。
第一个返回两个属性,第二个返回一个。
会导致这种情况的区别是什么?
另请参见[typedescriptor-getproperties-vs -type-getproperties](http://stackoverflow.com/questions/1402239/typedescriptor-getproperties-vs-type-getproperties) – nawfal 2014-07-20 06:39:52