自定义属性的强制使用

编程入门 行业动态 更新时间:2024-10-21 05:48:51
本文介绍了自定义属性的强制使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

场景:我有一个基类MyBase。我有一个自定义属性MyAttrib

Scenario: I have a base class "MyBase". I have a custom attribute "MyAttrib"

通过我这样做:

[MyAttrib(1234)] class MyClass : MyBase() { MyClass() { } }

问题:我可以在任何方面力量类从MyBase inherting有属性MyAttrib?

Question: Can I in any way force classes inherting from MyBase to have the attribute MyAttrib?

推荐答案

没有,有没有办法让编译器需要在C#中的属性。你有提供给你一些其他的选择。你可以写一个单元测试,在组装和检查属性所有类型的体现。但不幸的是有没有办法让编译器的力量属性的用法。

No, there is no way to have the compiler require an attribute in C#. You do have some other options available to you. You could write a unit test that reflects on all types in the assembly and checks for the attribute. But unfortunately there is no way to have the compiler force the usage of an attribute.

更多推荐

自定义属性的强制使用

本文发布于:2023-10-27 03:11:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!