创建一个属性来打破建设创建一个、属性

2023-09-03 05:28:24 作者:呸

确定,这种从上进行如下我的previous问题。

我真的很想做的是创建属性的某种形式,让我来装饰方法,将打破建立。很像的过时(理,真)的属性,但没有错误地识别过时的code。

What I would really like to do is create some sort of attribute which allows me to decorate a method that will break the build. Much like the Obsolete("reason", true) attribute, but without falsely identifying obsolete code.

澄清:我不希望它打破建设上的任意的F6(生成)preSS,我只是想,如果一种方法是打破构建饰有属性称为别的地方在code。就像我说的,类似于的过时,但不一样的。

To clarify: I dont want it to break the build on ANY F6 (Build) press, I only want it to break the build if a method decorated with the attribute is called somewhere else in the code. Like I said, similar to obsolete, but not the same.

我知道我不是一个人在这,因为的其他用户想要使用它的其他原因。我从来没有创建自定义属性之前,因此它是所有新的给我!

I know I am not alone in this, since other users want to use it for other reasons. I have never created custom attributes before so it is all new to me!

推荐答案

如果这是XML序列化和NHibernate,你想要的参数的构造函数可以访问(如在的例如你引用的),然后使用专用或受保护参数的构造函数序列化,或NHibernate的一个受保护的构造。与受保护的版本,你打开自己可达继承类能够调用code。

If this is for XML serialization and NHibernate, where you want the parameterless constructor to be accessible (as is the case in the example you referenced), then use a private or protected parameterless constructor for serialization, or a protected constructor for NHibernate. With the protected version, you are opening yourself up to inherited classes being able to call that code.

如果你不想code调用的方法,不要让它访问。

If you don't want code calling a method, don't make it accessible.

编辑:也许回答了更深层次的问题,AFAIK,编译器只知道三个属性:的过时,有条件的,AttributeUsage 。要添加特殊处理的其他属性将需要修改编译器。

To perhaps answer the deeper question, AFAIK the compiler only knows about three attributes: Obsolete, Conditional, and AttributeUsage. To add special handling for other attributes would require modifying the compiler.