Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的System.AttributeTargets.GenericParameter:如何使用这样的属性?_C#_Generics_Annotations - Fatal编程技术网

C#中的System.AttributeTargets.GenericParameter:如何使用这样的属性?

C#中的System.AttributeTargets.GenericParameter:如何使用这样的属性?,c#,generics,annotations,C#,Generics,Annotations,在C#中,当指定如何使用属性类时,System.AttributeTargetsenum中有一个GenericParameter值。我们如何应用这样的属性,语法是什么 [System.AttributeUsage(System.AttributeTargets public sealed class MyAnnotationAttribute : System.Attribute { public string Param { get; private set; } public

在C#中,当指定如何使用属性类时,
System.AttributeTargets
enum中有一个
GenericParameter
值。我们如何应用这样的属性,语法是什么

[System.AttributeUsage(System.AttributeTargets
public sealed class MyAnnotationAttribute : System.Attribute {
    public string Param { get; private set; }
    public MyAnnotationAttribute(string param) { Param = param; }
}
同样的问题也适用于其他外来属性目标,如
System.AttributeTargets.Module
(我甚至不知道如何声明除主模块之外的模块?)、
System.AttributeTargets.Parameter
System.AttributeTargets.ReturnValue
//程序集和模块
// Assembly and module
[assembly: AttributesTest.MyAnnotation("Assembly")]

[module: AttributesTest.MyAnnotation("Module")]

namespace AttributesTest
{
    // The attribute
    [System.AttributeUsage(System.AttributeTargets.All, AllowMultiple = true)]
    public sealed class MyAnnotationAttribute : System.Attribute
    {
        public string Param { get; private set; }
        public MyAnnotationAttribute(string param) { Param = param; }
    }

    // Types
    [MyAnnotation("Class")]
    public class SomeClass { }

    [MyAnnotation("Delegate")]
    public delegate int SomeDelegate(string s, float f);

    [MyAnnotation("Enum")]
    public enum SomeEnum { ValueOne, ValueTwo }

    [MyAnnotation("Interface")]
    public interface SomeInterface { }

    [MyAnnotation("Struct")]
    public struct SomeStruct { }

    // Members
    public class MethodsExample
    {
        [MyAnnotation("Constructor")]
        public MethodsExample() { }

        [MyAnnotation("Method")]
        public int SomeMethod(short s) { return 42 + s; }

        [MyAnnotation("Field")]
        private int _someField;

        [MyAnnotation("Property")]
        public int SomeProperty {
            [MyAnnotation("Method")] get { return _someField; }
            [MyAnnotation("Method")] set { _someField = value; }
        }

        private SomeDelegate _backingField;

        [MyAnnotation("Event")]
        public event SomeDelegate SomeEvent {
            [MyAnnotation("Method")] add { _backingField += value; }
            [MyAnnotation("Method")] remove { _backingField -= value; }
        }
    }

    // Parameters
    public class ParametersExample<T1, [MyAnnotation("GenericParameter")]T2, T3>
    {
        public int SomeMethod([MyAnnotation("Parameter")]short s) { return 42 + s; }
    }

    // Return value
    public class ReturnValueExample
    {
        [return: MyAnnotation("ReturnValue")]
        public int SomeMethod(short s) {
            return 42 + s;
        }
    }
}
[程序集:AttributeTest.MyAnnotation(“程序集”)] [模块:AttributeTest.MyAnnotation(“模块”)] 名称空间属性测试 { //属性 [System.AttributeUsage(System.AttributeTargets.All,AllowMultiple=true)] 公共密封类MyAnnotationAttribute:System.Attribute { 公共字符串参数{get;private set;} 公共MyAnnotationAttribute(字符串参数){param=param;} } //类型 [MyAnnotation(“类”)] 公共类SomeClass{} [MyAnnotation(“委托”)] 公共委托int SomeDelegate(字符串s,浮点f); [MyAnnotation(“Enum”)] 公共枚举SomeEnum{ValueOne,ValueTwo} [MyAnnotation(“接口”)] 公共接口SomeInterface{} [MyAnnotation(“结构”)] 公共结构SomeStruct{} //成员 公共类方法示例 { [MyAnnotation(“构造函数”)] 公共方法示例(){} [MyAnnotation(“方法”)] 公共int方法(短s){return 42+s;} [MyAnnotation(“字段”)] 私人国际某地; [MyAnnotation(“财产”)] 公共属性{ [MyAnnotation(“方法”)]获取{return\u someField;} [MyAnnotation(“方法”)]set{{u someField=value;} } 私人代理(backingField);; [MyAnnotation(“事件”)] 公共事件SomeDelegate SomeEvent{ [MyAnnotation(“方法”)]添加{{u backingField+=value;} [MyAnnotation(“方法”)]删除{{u backingField-=value;} } } //参数 公共类参数示例 { 公共int-SomeMethod([MyAnnotation(“参数”)]short s){return 42+s;} } //返回值 公共类返回值示例 { [返回:MyAnnotation(“返回值”)] 公共int方法(短s){ 返回42+s; } } }
属性
get
set
访问器上的属性,以及事件
add
remove
访问器,实际上都是方法属性(在每种情况下,属性必须指定
AttributeTargets.method
。@280Z28确实,我检查了它,你是对的。谢谢你的编辑!