标签: attributeusage

为什么在创建属性时想要使用AttributeUsage AllowMultiple?

根据我正在阅读的一本书,AllowMultiple公共财产AttributeUsage规定:

...目标是否可以将多个属性实例应用于它.

我为什么要/不想使用它?

c# custom-attributes attributeusage

17
推荐指数
2
解决办法
1万
查看次数

特定类的C#AttributeUsage

是否有可能像AttributeUsage这样的东西来限制属性对特定类的使用(不仅仅是AttributeTargets.Class - 那将是任何类)?

c# attributes attributeusage

9
推荐指数
2
解决办法
5284
查看次数

为什么F#不允许C#的多个属性?

以下代码在C#中编译:

[ContentType("text")]
[ContentType("projection")]
public class Class1
{
}
Run Code Online (Sandbox Code Playgroud)

F#中的以下代码无法编译:

[<ContentType("text")>]
[<ContentType("projection")>]
type Class1() = class end
Run Code Online (Sandbox Code Playgroud)

F#中的编译错误是:"属性类型'ContentTypeAttribute'具有'AllowMultiple = false'.此属性的多个实例不能附加到单个语言元素."

通过反编译ContentType,我可以看到ContentType继承自MultipleUsase中具有'AllowMultiple = true'的 MultipleBaseMetadataAttribute .

实际上,似乎F#不从父类继承AttributeUsage.

[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>]
type FooAttribute() = 
    inherit Attribute()

type BarAttribute() =
    inherit FooAttribute()

[<Foo>]
[<Foo>]
type MyClassCompiles() = class end
Run Code Online (Sandbox Code Playgroud)

哪里

[<Bar>]
[<Bar>]
type MyClassDoesNotCompile() = class end
Run Code Online (Sandbox Code Playgroud)

f# attributes allowmultiple attributeusage

8
推荐指数
1
解决办法
652
查看次数

C#:AttributeUsage 在继承的属性上表现奇怪 - 这是预期的行为吗?

我正在使用自定义属性,它们也有一个公共基类。AttributeUsage 属性是在基属性类上声明的,而不是在具体属性类上声明的。我在基类上的属性用法说 AttributeTargets.Class, AllowMultiple = true, Inherited = true

现在,当我使用具体属性类时,他们使用 Target 和 AllowMultiple 的基属性类中的 AttributeUsage 设置,但不使用 Inherited -> 这对我来说没有任何意义。

这是一个小代码示例,它没有输出我期望的结果:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public abstract class TestAttributeBase : Attribute
{
}

public class TestAttribute : TestAttributeBase
{
    public string Value {get; private set;}
    
    public TestAttribute(string value)
    {
        Value = Value;  
    }
}

[TestAttribute("A")]
public class A {
}

[TestAttribute("B")]
[TestAttribute("C")]
public class B : A
{
}

public static void Main()
{
    var customAttributes = typeof(B).GetCustomAttributes<TestAttributeBase>(true).ToList(); …
Run Code Online (Sandbox Code Playgroud)

.net c# attributeusage

5
推荐指数
1
解决办法
132
查看次数

使用AttributeTargets.Class的自定义ValidationAttribute的客户端验证

是否可以对用于Class范围的自定义ValidationAttribute进行客户端验证?例如我的MaxLengthGlobal,它应确保所有输入字段的全局最大限制。

[AttributeUsage(AttributeTargets.Class)]
public class MaxLengthGlobalAttribute : ValidationAttribute, IClientValidatable
{
    public int MaximumLength
    {
        get;
        private set;
    }

    public MaxLengthGlobalAttribute(int maximumLength)
    {
        this.MaximumLength = maximumLength;
    }

    public override bool IsValid(object value)
    {
        var properties = TypeDescriptor.GetProperties(value);

        foreach (PropertyDescriptor property in properties)
        {
            var stringValue = property.GetValue(value) as string;

            if (stringValue != null && (stringValue.Length > this.MaximumLength))
            {
                return false;
            }
        }

        return true;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {       
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this.FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType …
Run Code Online (Sandbox Code Playgroud)

c# model-view-controller class client-side-validation attributeusage

4
推荐指数
1
解决办法
2224
查看次数