自定义属性的强制使用 [英] Force usage of custom attribute

查看:115
本文介绍了自定义属性的强制使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
我有一个基类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.

这篇关于自定义属性的强制使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆