[英]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. 对他们来说,似乎添加了一种方法,这应该是不间断的更改。
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.