<tfoot id='uuaef'></tfoot>

    <i id='uuaef'><tr id='uuaef'><dt id='uuaef'><q id='uuaef'><span id='uuaef'><b id='uuaef'><form id='uuaef'><ins id='uuaef'></ins><ul id='uuaef'></ul><sub id='uuaef'></sub></form><legend id='uuaef'></legend><bdo id='uuaef'><pre id='uuaef'><center id='uuaef'></center></pre></bdo></b><th id='uuaef'></th></span></q></dt></tr></i><div id='uuaef'><tfoot id='uuaef'></tfoot><dl id='uuaef'><fieldset id='uuaef'></fieldset></dl></div>

    <legend id='uuaef'><style id='uuaef'><dir id='uuaef'><q id='uuaef'></q></dir></style></legend>
      <bdo id='uuaef'></bdo><ul id='uuaef'></ul>

      <small id='uuaef'></small><noframes id='uuaef'>

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

      时间:2023-07-28

      1. <legend id='wwuNn'><style id='wwuNn'><dir id='wwuNn'><q id='wwuNn'></q></dir></style></legend>

        • <tfoot id='wwuNn'></tfoot>
          <i id='wwuNn'><tr id='wwuNn'><dt id='wwuNn'><q id='wwuNn'><span id='wwuNn'><b id='wwuNn'><form id='wwuNn'><ins id='wwuNn'></ins><ul id='wwuNn'></ul><sub id='wwuNn'></sub></form><legend id='wwuNn'></legend><bdo id='wwuNn'><pre id='wwuNn'><center id='wwuNn'></center></pre></bdo></b><th id='wwuNn'></th></span></q></dt></tr></i><div id='wwuNn'><tfoot id='wwuNn'></tfoot><dl id='wwuNn'><fieldset id='wwuNn'></fieldset></dl></div>

            <small id='wwuNn'></small><noframes id='wwuNn'>

                <tbody id='wwuNn'></tbody>
              • <bdo id='wwuNn'></bdo><ul id='wwuNn'></ul>
                本文介绍了在方法调用上生成编译错误的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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.

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

                上一篇:使用自定义属性最简单最优雅的方法是什么 下一篇:C# 动态属性参数

                相关文章

                  <i id='ecCLs'><tr id='ecCLs'><dt id='ecCLs'><q id='ecCLs'><span id='ecCLs'><b id='ecCLs'><form id='ecCLs'><ins id='ecCLs'></ins><ul id='ecCLs'></ul><sub id='ecCLs'></sub></form><legend id='ecCLs'></legend><bdo id='ecCLs'><pre id='ecCLs'><center id='ecCLs'></center></pre></bdo></b><th id='ecCLs'></th></span></q></dt></tr></i><div id='ecCLs'><tfoot id='ecCLs'></tfoot><dl id='ecCLs'><fieldset id='ecCLs'></fieldset></dl></div>
                    <bdo id='ecCLs'></bdo><ul id='ecCLs'></ul>

                1. <legend id='ecCLs'><style id='ecCLs'><dir id='ecCLs'><q id='ecCLs'></q></dir></style></legend>
                  <tfoot id='ecCLs'></tfoot>
                  1. <small id='ecCLs'></small><noframes id='ecCLs'>