在 MetadataProvider 中获取多个相同类型的属性
Get multiple attributes of the same type in the MetadataProvider
我有一个属性,它有 AllowMultiple
:
[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;
}
}
我的问题是,我总是在 attributes
中只有一个 AllowedDeviceTypesAttribute
:
如何获得这两个属性?
[AllowedDeviceTypes(DeviceTypes.StaticRegister)]
[AllowedDeviceTypes(DeviceTypes.MobileRegister)]
为了使这项工作正常,我们需要覆盖 TypeId
属性:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class AllowedDeviceTypesAttribute : System.Attribute
{
//...
public override object TypeId
{
get
{
return this;
}
}
}
我有一个属性,它有 AllowMultiple
:
[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;
}
}
我的问题是,我总是在 attributes
中只有一个 AllowedDeviceTypesAttribute
:
如何获得这两个属性?
[AllowedDeviceTypes(DeviceTypes.StaticRegister)]
[AllowedDeviceTypes(DeviceTypes.MobileRegister)]
为了使这项工作正常,我们需要覆盖 TypeId
属性:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class AllowedDeviceTypesAttribute : System.Attribute
{
//...
public override object TypeId
{
get
{
return this;
}
}
}