Yes, making a private
member public
can break the build.
Consider this class:
public class Test
{
private int Method(int x = 0) => x;
public int Method(int x = 1, int y = 2) => x + y;
public int Foo => Method(0);
}
And this code:
class Program
{
static void Main()
{
Console.WriteLine(new Test().Method());
}
}
This will compile OK, and will print 3
.
Now if you make public the private overload of Method()
the code above will no longer compile. You will get the following compile error:
Error CS0121 The call is ambiguous between the following methods or properties:
'Test.Method(int)' and 'Test.Method(int, int)'
However, with regard to your question about the plugin - you did not specify whether it uses SharedLibrary
. If it doesn't, then changing SharedLibrary
cannot break the plugin.
If it does, then you could get a nasty runtime error, depending on how it uses it - but that's actually not guaranteed to happen.
Here's an example of using a plugin which WILL break at runtime if you make a method public.
Firstly, we have a shared class library containing the following class:
public class Test
{
private int Method(int x = 0) => x;
public int Method(int x = 1, int y = 2) => x + y;
public int Foo => Method(0);
}
Secondly, we have a plugin class that uses that library using reflection like so:
public class Plugin
{
public int Test()
{
var testClass = typeof(Test);
var testInstance = Activator.CreateInstance(testClass);
var method = testClass.GetMethod("Method");
int result = (int)method.Invoke(testInstance, new object[]{1, 2});
return result;
}
}
Now suppose we have an application that uses the shared class, and also contains a copy of the plugin assembly DLL in its output folder:
static void Main()
{
Console.WriteLine(new Test().Method(1, 2));
var plugin = Assembly.LoadFile(Path.GetFullPath("PluginLibrary.dll"));
var testClass = plugin.GetType("PluginLibrary.Plugin");
var testInstance = Activator.CreateInstance(testClass);
var method = testClass.GetMethod("Test");
int result = (int) method.Invoke(testInstance, null);
Console.WriteLine(result);
}
This will run ok.
Now suppose you make the private int Method(int x = 0)
public and recompile the shared class library and the application that uses it, but DO NOT recompile the plugin - i.e. you continue using the old plugin DLL.
Now if you run the program you'll get an Ambiguous match found
exception.
This is an example where you have broken an unchanged plugin by making public a method that wasn't even being used by that plugin.