方法不适用于代码属性?
我的应用程序中有几个共享方法,旨在与 ObjectDataSource(以及其他基于反射的对象)一起使用。它们处理大量自动参数检查、过滤等。但是,它们都是一次性使用方法(它们在每次调用时重建所有内容)。对于直接编写代码来调用实例方法的人来说,这样会更好,这样所有的过滤等都可以在调用之间存储。
我可以在方法中添加一个属性来表明这一点吗?最好的办法是防止在没有反射的情况下调用该方法。其次最好是发出警告,指出该方法使用错误。
这是我想要实现的目标的示例:
<MethodNotIntendedForCode()> _
Public Shared Function DataAccessMethod(Parameters....) As ObjectQuery(Of DataType)
I have a couple of shared methods in my app which are intended to be used with ObjectDataSource's (and other reflection based objects). They handle a lot of automatic parameter checking, filtering, etc. They are, however, all one time use methods (they rebuild everything on every call). It would be much better for someone writing code directly to call the instance methods so that all the filtering, etc, is stored between calls.
Is there an attribute I can add to my methods to indicate this? The best would be if it would prevent the method from being called without reflection. Second best would be to issue a warning that the method is being used wrong.
Here's a sample of what I want to acheive:
<MethodNotIntendedForCode()> _
Public Shared Function DataAccessMethod(Parameters....) As ObjectQuery(Of DataType)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试:
如果在代码中调用,这将产生警告。如果直接调用,您还可以使用
ObsoleteAttribute
上的选项将其标记为错误(在编译时)。显然,另一种选择就是将其设为非公开。这里其他有用的属性包括
EditorBrowsableAttribute
和BrowsableAttribute
。You could try:
which will create a warning if called in code. You can also use an option on
ObsoleteAttribute
to mark it as an error (at compile) if called directly.The other option, obviously, is simply to make it non-public. Other useful attributes here include
EditorBrowsableAttribute
andBrowsableAttribute
.