"You must add a reference to assembly" compiler error in weird case
I have two applications App1 and App2. App1 added a reference to App2 and App2 added a reference to System.Drawing
.
In App2 there's a function with two overloads, the first one is protected and has a parameter from System.Drawing.Bitmap
and the second one is public has an integer parameter.
namespace App1Namespace
{
class Program
{
static void Main(string[] args)
{
App2Namespace.StaticClass.Func(4);
namespace App2Namespace
{
public class StaticClass
{
protected static void Func(System.Drawing.Bitmap bitmapParam) { }
public static void Func(int intParam) { }
When the App1 is built, the following compile time error will be thrown:
Error 1 T开发者_开发问答he type 'System.Drawing.Bitmap' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Although Func
which has Bimap
param is protected, the App1 needs having a reference to System.Drawing
.
The questions are that why does App1 need System.Drawing
? and why this error appears only when the second overload is called and doesn't appear when another overload (for example with two parameters) is called?
Note: When the first overload becomes private the error will disappear.
The reason is likely because you are declaring StaticClass
as a regular class. So it is possible to override the class in any referencing assemblies (thus why using protected
would almost make sense for a static method). If all the methods are truly static methods, then make the class itself static and change the scope from protected
to internal
or private
.
It may simply be that it needs a reference, because since your App2Namespace.StaticClass.Func could be inherited from (since it's protected) in a class in the App1 namespace. If that were to happen, it would also need a reference to System.Drawing.
精彩评论