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

2023-12-30

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

[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 and 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;
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 将查询结果追加到表中

    当我尝试通过 API 插入查询作业 将查询结果附加到另一个表 时 我现在遇到了一些问题 查看文档 这似乎可以通过插入查询作业 设置目标表并将 writeDisposition 设置为 WRITE APPEND 来实现 如下所示 config
  • 是否有推荐的方法在使用 GWT 的 MVP 中使用观察者模式?

    我正在考虑使用 GWT 根据 MVP 模式实现用户界面 但对如何进行有疑问 这些是我的 一些 目标 演示者对 UI 技术一无所知 即没有使用 com google 中的任何内容 该视图对演示者一无所知 还不确定我是否希望它与模型无关 模型对
  • Collection.toArray() java.lang.ClassCastException

    import java util HashMap import java util Map public class Main public static void main String args Map
  • 如何用简单的代码修复“下标超出范围”错误

    我开始使用 VBA for Dummies 书学习如何编写 VBA 代码 我从书中复制并粘贴了这段代码 但它给了我一个错误 你能帮忙吗 Sub ShowValue Contents Worksheets Sheet1 Range A1 Va
  • 缩放标签文本与标签尺寸一致

    寻找一种调整大小的方法Labels文字随着尺寸的变化而变化Label 即标签尺寸是否应增加50 那么文本的大小也应该增加大约50 有很多文章可以调整文本以完全适合页面的大小Label 这不适合我想要的 我已经有了这个的工作版本 不知何故 我
  • 在node.js中模拟数据库?

    我如何在我的node js应用程序中模拟数据库 在本例中使用mongodb作为博客 REST API 的后端 当然 我可以将数据库设置为特定的testing database 但我仍然会保存数据 不仅测试我的代码 还会测试数据库 所以我实际
  • 绝对定位的元素隐藏其前面的透明元素

    我有三个彼此相邻的块元素 第一个和最后一个是透明的 中间的是绝对定位的 box width 300px height 300px box1 background yellow opacity 0 5 box2 background red
  • 从 iCloud 删除核心数据失败

    我正在尝试使用以下方法从 iCloud 中删除核心数据 NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL options error 但我
  • 在生产中找不到模块“worker_threads”错误

    我创建了一个利用工作线程的 Node js 项目 当我在 VS Code 中运行 npm start 时 代码工作正常 但是当在Ubuntu服务器中构建和复制时 它显示 找不到模块 worker threads 在生产环境中使用工作线程部署
  • 如何传递非插值数据 OpenGL ES (GLSL)

    我正在尝试将简单的 FLOAT 值从顶点传递到片段着色器 如何在不进行插值的情况下 按原样 传递它 在桌面上我可以使用平面变化来禁用插值 openGL es中有类似的东西还是唯一的方法是通过纹理 GLSL ES 目前不支持flat关键字 所
  • 使用 Jquery 将事件动态插入到 Fullcalendar

    我在使用 Jquery 将新事件添加到 fullCalendar 时遇到问题 我正在使用 Eclipse 开发 Web 对 Ajax 完全不熟悉 而且它不适用于我的 Eclipse 一切都写在 jquery 中的 Button click
  • 允许在数据表的“fnServerData”中进行服务器端缓存

    默认情况下 DataTables 中的 Ajax 调用通过在 url 上添加虚假字段来避免缓存 例如http yoursite com api 348522852542 我想禁用此功能 看来ajax函数调用fnServerData它的定义是
  • 以编程方式访问 Eclipse 的调用层次结构

    我需要以编程方式生成调用图作为 Eclipse 插件的一部分 我知道 Eclipse 具有内置的 Open Call Hierarchy 功能 但我一直无法找到一种方法来访问它作为插件开发的一部分 有谁知道如何使用 Eclipse 的调用层
  • 使用 Windows 命令提示符或 PowerShell 在 Spyder 中运行脚本

    我想知道是否有一种方法可以使用 Windows cmd 或 PowerShell 而不是 IPython 控制台在spyder ide 中运行 python 脚本 有时 Ipython 的工作方式与在 cmd 中运行脚本的方式不同 专门用于
  • Python 使用什么字节序写入文件?

    当使用 file write 和 wb 标志时 Python 使用大端还是小端 或者 sys byteorder 值 我如何确定字节序不是随机的 我问是因为我在同一个文件中混合 ASCII 和二进制数据 并且对于二进制数据 我使用 stru
  • 未知提供者:$uibModalInstanceProvider - Bootstrap UI 模式

    我的 UI Bootstrap 模式有问题 在一个控制器中我有这个 app controller tableCtrl scope http uibModal log function scope http uibModal log scop
  • 如何在VLOOKUP中设置范围条件?

    我的 Excel 文件中有下表 我想找到相应输入的值列 Col A Col B Col C Min Max Value 101 AA High 0 10 1 101 AA High 11 20 2 101 AA Low 0 10 3 101
  • 没有模板的组件

    我有一些代码可以对服务器进行 api 调用并返回一些 JSON 它确实作为我的组件中的一种方法存在 但由于它变得有点长 我想将其提取到它自己的文件中 在 vuejs 中 这里的最佳实践是什么 它应该是一个没有模板的组件吗 这将如何运作 我会
  • 期望从可观察的下一个处理程序中抛出错误,用 Jasmine 测试 Angular

    在 Angular 应用程序中 有一个具有以下方法的控制器 onClick void this http get some endpoint subscribe response gt const editUrl this service
  • C# 中的 System.AttributeTargets.GenericParameter:如何使用这样的属性?

    在 C 中 当指定属性类应如何使用时 有一个GenericParameter值在System AttributeTargets枚举 我们如何应用这样的属性 语法是什么 System AttributeUsage System Attribu