0
我有一個屬性,即具有AllowMultiple
:獲取相同類型的多個屬性在MetadataProvider
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class AllowedDeviceTypesAttribute : System.Attribute
{
//...
}
,我使用它在我的物業多次:
[AllowedDeviceTypes(DeviceTypes.StaticRegister)]
[AllowedDeviceTypes(DeviceTypes.MobileRegister)]
public int ClientNr { get; set; }
我有一個自定義模型元數據提供程序:
public class ExtendedModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<System.Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var attributeList = attributes as IList<System.Attribute> ?? attributes.ToList();
var data = base.CreateMetadata(attributeList, containerType, modelAccessor, modelType, propertyName);
//...
var allowedDeviceTypesAttributes = attributeList.Where(a => typeof(AllowedDeviceTypesAttribute) == a.GetType()).ToList();
if (allowedDeviceTypesAttributes.Count > 0)
{
var allowedDeviceTypes = allowedDeviceTypesAttributes.Cast<AllowedDeviceTypesAttribute>().Select(e => e.AllowedDeviceType).ToList();
data.AdditionalValues.Add(AllowedDeviceTypesAttribute.AdditionalMetaDataValue, allowedDeviceTypes);
}
return data;
}
}
我的問題是,我總是隻有一個AllowedDeviceTypesAttribute
在attributes
:
我怎樣才能得到這兩個屬性?
[AllowedDeviceTypes(DeviceTypes.StaticRegister)]
[AllowedDeviceTypes(DeviceTypes.MobileRegister)]