我可以隐式地将属性设置为任何方法吗? [英] Can I set attribute to any method implicitly?
问题描述
我可以使用代码将属性设置为任何方法或任何类的任何成员吗?
我正在创建 .dll
仅供CLI或Form或IIS应用程序调用的程序。每个应用程序的实现方法由不同的类分开;即 ConsoleApplication
, FormApplication
和 WebApplication
类。
我想确保,如果最终用户从CLI或IIS应用程序中调用 .dll
文件, FormApplication
类的方法应视为 [不建议使用]
;也就是说,为每个方法实现 ObsoleteAttribute
。
我明确表示不愿意归属于所有方法,因为来自 FormApplication
类的任何调用都可能无法调用它,否则。我只想将它们添加到方法中,当且仅当我确定 Assembly.EntryAssembly()时,EntryPoint
包含Void Main ()
或Void Main(System.String [])
递归。
有可能这样做吗?
谢谢
我试过的:
好吧,由于C#中没有内置函数来实现这个场景,我最好考虑要求天才。
Can I set attribute to any method or any member of any class using code?
I am creating a .dll
program that intended only to be invoked by any one of CLI or Form or IIS application. The implementation methods for each application are parted by different classes; i.e. ConsoleApplication
, FormApplication
and WebApplication
classes.
I wanted to make sure that, if an end user invoking the .dll
file from either of CLI or IIS application, the methods for FormApplication
class should be treated as [deprecated]
; that is, to implement the ObsoleteAttribute
to each of the method.
I explicitly am not willing to put the attribute down to all methods, as any invocation from the FormApplication
class may fail to call it, otherwise. I just want only to add them up to the methods if and only if I made certain that Assembly.EntryAssembly().EntryPoint
contains either "Void Main()"
or "Void Main(System.String[])"
recursively.
Is is possible to do so?
Thanks
What I have tried:
Well, as there are no in-built functions available in C# to implement the scenario, I better considered to ask for a genius.
推荐答案
这没有任何意义,因为Deprecated或Obsolete属性仅在编译时应用,在运行时不做任何事情,并且在设计时无法分辨 - 代码调用它的时间。功能是没用的。
你所描述的真的是不可能的,你的代码也不应该给你带来什么。在某些时候,你必须依赖开发人员使用库做正确的事情并根据应用程序的要求选择正确的类。您还尝试将类限制为非常具体的情况,在这些情况下,它们最有意义,同时阻止开发人员在对它们有意义的情况下使用类而不是您。
我们不知道这些课程应该做什么。如果这些类用于业务规则或数据,则它们根本不关心什么类型的应用程序调用它们。它们应完全与UI无关。
This doesn't make any sense as the Deprecated or Obsolete attributes only apply at compile -time, don't do anything at run-time, and cannot tell at design-time which code is calling it. The functionality is useless.
What you describe really sin't possible nor should your code give a sh*t. At some point you have to rely on the developer using the library to "do the right thing" and pick the correct classes to use based on the requirements of the application. You're also trying to restrict using the classes to very specific cases where they make the most sense at the same time preventing the developer from using the classes in cases where it makes sense to them but not you.
We have no idea what these classes are supposed to do. If these classes are for business rules or data, they shouldn't care at all what kind of application calls them. They should be completely UI agnostic.
这篇关于我可以隐式地将属性设置为任何方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!