C#中:什么是TypeDescriptor.GetAttributes()和的GetType().GetCustomAttributes之间的区别?

StackOverflow https://stackoverflow.com/questions/1833288

  •  11-09-2019
  •  | 
  •  

取这两个代码的东西:

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
{
   ...
}

其中RequiredIfOtherPropertyIsNotEmptyValidationAttribute并且具有AllowMultiple = true

第一个返回两个属性,所述第二返回一个。

有什么会导致这种不同?

scroll top