标签: allowmultiple

在抽象的Attribute类上指示AllowMultiple = false有什么意义?

在最近关于MVC属性的问题中,有人询问在操作方法上使用HttpPost和HttpDelete属性是否会导致允许请求类型或者不允许任何请求(因为它不能同时是Post和Delete) ).我注意到ActionMethodSelectorAttribute,HttpPostAttribute和HttpDeleteAttribute都派生出来了

[AttributeUsage(AttributeTargets.Method,
                AllowMultiple = false,
                Inherited = true)]
Run Code Online (Sandbox Code Playgroud)

我原以为它不允许在同一个方法上使用HttpPost和HttpDelete因为这个,但是编译器没有抱怨.我的有限测试告诉我,基类的属性用法被简单地忽略了.AllowMultiple似乎只允许将两个相同的属性应用于方法/类,并且似乎不考虑这些属性是否来自配置为不允许多个的同一个类.此外,基类上的属性用法甚至不会阻止您更改派生类的属性用法.既然如此,甚至在基本属性类上设置值有什么意义呢?它只是建议还是我缺少一些基本的工作方式?

仅供参考 - 事实证明,使用两者基本上排除了这种方法的考虑.属性是独立评估的,其中一个将始终指示该方法对请求无效,因为它不能同时是Post和Delete.

.net attributes allowmultiple

16
推荐指数
1
解决办法
8475
查看次数

为什么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
查看次数

在派生属性类型上表彰AttributeUsage

鉴于以下情况,我不希望编译器允许从base属性派生的多个属性,因为它们设置为AllowMultiple = false.事实上它编译没有问题 - 我在这里缺少什么?

using System;

[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
abstract class BaseAttribute : Attribute { }

sealed class DerivedAttributeA : BaseAttribute { }

sealed class DerivedAttributeB : BaseAttribute { }

    class Sample1
    {
        [DerivedAttributeA()]
        [DerivedAttributeB()]
        public string PropertyA{ get; set; } // allowed, concrete classes differ

        [DerivedAttributeA()]
        [DerivedAttributeA()]
        public string PropertyB { get; set; } // not allowed, concrete classes the same, honours AllowMultiple=false on BaseAttribute
    }
Run Code Online (Sandbox Code Playgroud)

c# inheritance attributes allowmultiple

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

使用反射为单个属性获取XmlElementAttribute的每个实例

我正在尝试列出Item可能包含的可能类型.但是我陷入困境,我无法调用Item.GetType()来遍历其属性,因为这只会返回它已经包含的类型的属性.

我尝试过TypeDescriptor.GetProperties(...)但是Attributes容器只包含一个XmlElementAttribute实例,它是应用于属性的最后一个实例(在本例中为WindowTemplate)

这一定是微不足道的,但我找不到任何解决我在线问题的方法.

    [System.Xml.Serialization.XmlElementAttribute("ChildTemplate", typeof(ChildTmpl), Order = 1)]
    [System.Xml.Serialization.XmlElementAttribute("WindowTmeplate", typeof(WindowTmpl), Order = 1)]
    public object Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

.net c# reflection serialization allowmultiple

3
推荐指数
1
解决办法
2524
查看次数