简体   繁体   English

将静态方法的修饰符从内部更改为公共是否是重大变化?

[英]Is changing the modifier of a static method from internal to public a breaking change?

If I change the access modifier of the static method within a public class from internal to public, will it break the external assembles which call the method? 如果我将公共类中静态方法的访问修饰符从内部更改为公共,是否会破坏调用该方法的外部装配?

NOTES : the internal method can be called by an external assembly with the use of InternalsVisibleTo attribute: 注意 :内部方法可以由外部程序集使用InternalsVisibleTo属性调用:

[assembly: InternalsVisibleTo("TheExternalAssembly")]    
namespace TheAssemblyWithInternalMethod 

It is possible. 有可能的。

Imagine this shared library code: 想象一下这个共享库代码:

namespace SharedLibrary
{

    public class SharedLibraryClassOne
    {
        public static void PotentialCollisionMethod()
        {
        }

        internal static void SharedLibraryMethodTwo()
        {
        }
    }

    public class SharedLibraryClassTwo
    {
        internal static void PotentialCollisionMethod() 
        {
        }

        public static void SharedLibraryMethodThree()
        {
        }
    }
}

Here is a console app that links to the library: 这是一个链接到库的控制台应用程序:

using static SharedLibrary.SharedLibraryClassOne;
using static SharedLibrary.SharedLibraryClassTwo;

namespace StackOverflowChallenge20160121
{
    class Program
    {
        static void Main(string[] args)
        {
            PotentialCollisionMethod(); // Invokes SharedLibraryClassOne.PotentialCollisionMethod
            SharedLibraryMethodThree();
        }
    }
}

This all compiles. 全部编译。 But, if I change SharedLibraryClassTwo.PotentialCollisionMethod from internal to public, my dependent application no longer compiles. 但是,如果我将SharedLibraryClassTwo.PotentialCollisionMethod从内部更改为公共,则我的从属应用程序将不再编译。

Simply by making an internal method public, I get the following error in previously compiling code: 只需将内部方法公开,就可以在以前的编译代码中得到以下错误:

error CS0121: The call is ambiguous between the following methods or properties: 'SharedLibraryClassOne.PotentialCollisionMethod()' and 'SharedLibraryClassTwo.PotentialCollisionMethod()' 错误CS0121:以下方法或属性之间的调用不明确:“ SharedLibraryClassOne.PotentialCollisionMethod()”和“ SharedLibraryClassTwo.PotentialCollisionMethod()”

Errr... NO ? Errr ... If it's internal, there are no external callers yet. 如果是内部电话,则没有外部呼叫者。 To them, it looks like a method was added, which should be a non-breaking change. 对他们来说,似乎添加了一种方法,这应该是不间断的更改。

Uhm, as internal is already saying. 嗯,就像internal已经说过的那样。 The method cannot be accessed from other assemblies. 无法从其他程序集访问该方法。 Some additional info . 一些附加信息

So no, it won't break anything. 所以不,它不会破坏任何东西。 It will offer new opportunities. 它将提供新的机会。

No it wont break while changing from internal to public. 不,当从内部变为公共时,它不会中断。 If it was vice versa there is a higher probability of breaking the code. 反之亦然,则更有可能破坏代码。 Your could refer to Jeffrey Richter book Clr via C# where its clearly mentioning of it. 您可以通过C#查阅Jeffrey Richter的《 Clr》一书,其中明确提到了该书。 "make the class internal unless I want the class to be publicly exposed outside of my assembly" so hence no problem “除非我想让班级在我的大会之外公开露面,否则请设为班级内部”,因此没有问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将.NET中的访问修饰符从共享组件中的私有更改为私有是一项重大更改吗? - Is changing an access modifier in .NET from private to public in share component a breaking change? 是否将虚拟方法的可见性从受保护的内部更改为受保护的更改? - Is changing the visibility of a virtual method from protected internal to protected a breaking change? 如何将窗口的访问修饰符从公共更改为内部? - How can i change the Access Modifier of a Window from public to internal? 修改公共财产的访问修饰符是一个突破性的变化吗? - Is it a breaking change that modifying the access modifier of a public property? 更改localization.resx始终会将public更改为internal修饰符 - Changing the localisation.resx always changes the public to internal modifier 向公共类添加公共方法是一个重大变化吗? - Is adding a public method to a public class a breaking change? 'public / static' 修饰符对此项无效 - The 'public / static' modifier is not valid for this item 关键字 this 在 static 属性、static 方法或 static 字段初始化程序中无效。 修饰符“公共”对此项目无效 - Keyword this is not valid in a static property, static method, or static field initilizer. The modifier 'Public' is not Valid for this item Resharper建议对公开,内部或受保护的对象使用无效方法-如何始终拥有需求访问修饰符 - Resharper suggest void method to public, internal or protected - how to always have demand access modifier 如何将resharpers默认访问修饰符更改为public而不是内部生成的类? - How to change resharpers default access modifier to public instead of internal for generated classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM