在方法调用上生成编译错误的属性?

Attribute to generate compilation error on method call?(在方法调用上生成编译错误的属性?)
本文介绍了在方法调用上生成编译错误的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保永远不会从代码中显式调用方法(在我的情况下实际上是构造函数).它只能在运行时通过反射调用.为此,我想在方法上应用一个属性,如果调用该方法会产生编译器错误,例如:

I would like to ensure that a method (actually a constructor in my case) is never called explicitly from code. It should only be called through reflection at runtime. To do that, I would like to apply an attribute on the method that would generate a compiler error if the method is called, something like :

[NotCallable("This method mustn't be called from code")]
public void MyMethod()
{
}

我知道我可以将方法设为私有,但在这种情况下,我将无法通过部分信任上下文中的反射来调用它...

I know that I could make the method private, but in that case I wouldn't be able to call it through reflection in a partial trust context...

为了完整起见,这里有更多关于我为什么需要这样做的详细信息:

For completeness, here's more details about why I need to do that :

我正在实现一个可重用的 Singleton<T> 类,基于 Jon Skeet 的文章.到目前为止,这是我的代码:

I'm implementing a reusable Singleton<T> class, based on Jon Skeet's article. Here's my code so far :

public static class Singleton<T>
{
    public static T Instance
    {
        get
        {
            return LazyInitializer._instance;
        }
    }

    private class LazyInitializer
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static LazyInitializer()
        {
            Debug.WriteLine(string.Format("Initializing singleton instance of type '{0}'", typeof(T).FullName));
        }

        internal static readonly T _instance = (T)Activator.CreateInstance(typeof(T), true);
    }
}

(注意我使用 Activator.CreateInstance 创建 T 实例的方式)

(Note the way I create the T instance using Activator.CreateInstance)

然后我可以将它与这样的类一起使用:

I can then use it with a class like that :

private class Foo
{
    protected Foo()
    {
    }

    public string Bar { get; private set; }
}

并调用Singleton<Foo>.Instance来访问实例.

在部分信任的情况下,它不会工作,因为 Foo 构造函数不是公共的.但是如果我把它公开,没有什么能阻止从代码中显式调用它......我知道我可以在 Foo 构造函数上应用 ObsoleteAttribute ,但它只会生成一个警告,许多人只是忽略警告.

In partial trust, it won't work because the Foo constructor is not public. But if I make it public, nothing will prevent calling it explicitly from code... I know I could apply the ObsoleteAttribute on the Foo constructor, but it will only generate a warning, and many people just ignore warnings.

那么,是否有类似于 ObsoleteAttribute 的属性会生成错误而不是警告?

So, is there an attribute similar to ObsoleteAttribute that would generate an error instead of a warning ?

任何建议将不胜感激

推荐答案

您可以使用 ObsoleteAttribute 构造函数,它接受一个布尔值,您可以使用它指示调用该方法是一个编译错误:

You can use the ObsoleteAttribute constructor that takes a boolean with which you can indicate that calling the method is a compilation error:

[Obsolete("Don't use this", true)]

但是,如果我是你,我会重新考虑我的设计,因为这样做并不是 API 设计良好的标志.

However, if I were you, I'd reconsider my design, as doing this isn't a sign of a well-designed API.

这篇关于在方法调用上生成编译错误的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)