静态类方法重载使用导致编译器错误 [英] Static class methods overloads use cause a compiler error

查看:87
本文介绍了静态类方法重载使用导致编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在使用静态类方法重载时遇到问题(不确定它是否在非静态类上重现)

I have a problem using static class method overloads (not sure if it reproduces on non-static classes)


我有3个项目。它们在ref chan中像project-1引用项目-2,项目-3引用项目-2。这意味着project-3没有直接的project-1引用。

I have 3 projects. The are in ref chan like project-1 references project-2, project-3 references project-2. That means project-3 dont have direct project-1 reference.


在项目-2上我有一个包含两个重载方法的静态类:一个有一个字符串的参数,一个 - 来自project-1的类型。当我尝试使用带有字符串参数的重载从project-3调用方法时,编译器引发了一个错误:
i必须在项目3中引用project-1。如果我创建一个新方法并且使用字符串重载方法执行相同操作param - everythig工作得很好。我将尝试用代码解释。

On the project-2 i have a static class that contains method with two overloads: one have a parameter of string, and one - the type from project-1. When im trying to call method from project-3 using overload with string param, the compiler raise an error that i must reference project-1 in project 3. If i create a new method and do the same as overloaded method with string param - everythig works fine. I'll try to explain in code.


Project-1

public class Foo
{
   public string Text { get; set; }
   public string Bar()
   {
      return Text + " bar";
   }
}

Project-2(引用Project-1)

public static class StaticClass
{
   public static string RunAnother(string str)
   {
      var foo = new Foo { Text = str };
      return Run(foo);
   }

   public static string Run (string str)
   {
      var foo = new Foo { Text = str };
      return Run(foo);
   }

   public static string Run(Foo foo)
   {
      return foo.Bar();
   }
}

Project-3(仅参考项目-2)

var t1 = StaticClass.Run("test"); //Compile error: " The type 'Foo' is defined in an assembly that is not referenced."

var t2 = StaticClass.RunAnother("test"); //Works great




为什么会这样?





推荐答案

编译您的代码必须能够访问与所使用的每种类型相关联的元数据。如果找不到它,则会生成编译错误。该类型还包括任何基类型,因为编译器必须确定有关
类型的信息,例如它是值还是引用以及可用的基本成员。因此,如果您使用的方法需要项目1中的类型A,那么您还必须包含对它的引用。最后,你从
项目2引用一个方法的事实在这里是无关紧要的,因为你将需要一个类型(要么传递或作为返回值)来自项目1,所以项目1是必需的引用。

To compile your code the compile must have access to the metadata associated with each type used. If it cannot find it then it generates a compiler error. The type would include any base types as well since the compiler has to determine information about the type such as whether it is value or reference and what base members are available. Hence if you use a method that requires type A from project 1 then you have to include a reference to it as well. Ultimately the fact that you're referencing a method from project 2 is irrelevant here as you're going to need a type (either to pass or as the return value) from project 1 so project 1 is a required reference.

现在,为什么要在Run调用中获取它而不是RunAnother?那是因为超载。因为Run有多个重载,所以编译器必须查看所有重载并根据您的参数缩小调用哪个重载。要做
它需要类型的元数据,因此错误。 

Now, why are you getting it on the Run call but not RunAnother? That is because of overloading. Because there are multiple overloads for Run the compiler has to look at all the overloads and narrow down which one to call based upon your arguments. To do that it needs the metadata for the types, hence the error. 


这篇关于静态类方法重载使用导致编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆