方法不适用于代码属性?

发布于 2025-01-02 19:49:39 字数 396 浏览 0 评论 0原文

我的应用程序中有几个共享方法,旨在与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-01-09 19:49:39

您可以尝试:

<Obsolete("This isn't the method you are looking for")>

如果在代码中调用,这将产生警告。如果直接调用,您还可以使用 ObsoleteAttribute 上的选项将其标记为错误(在编译时)。

显然,另一种选择就是将其设为非公开。这里其他有用的属性包括 EditorBrowsableAttributeBrowsableAttribute

You could try:

<Obsolete("This isn't the method you are looking for")>

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 and BrowsableAttribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文