如果库 B 的 extn 方法在库 A ' 中使用,为什么我需要对库 B 的 extn 方法使用 using 语句?它是我的客户使用的库 A?
我有:
- 主程序类 - 使用库 A
- 库 A - 具有混合库 B 的方法的部分类
- 库 B - 混合方法和方法接口
为什么我需要 LibaryB 的 using 语句才能让它们的扩展方法在主类中工作?假设库 B 定义了将要扩展的类。
编辑 - 除了代码
// *** PROGRAM ***
using TopologyDAL;
using Topology; // *** THIS WAS NEEDED TO GET EXTN METHODS APPEARING ***
class Program
{
static void Main(string[] args)
{
var context = new Model1Container();
Node myNode; // ** trying to get myNode mixin methods to appear seems to need using line to point to Library B ***
}
}
// ** LIBRARY A
namespace TopologyDAL
{
public partial class Node
{
// Auto generated from EF
}
public partial class Node : INode<int> // to add extension methods from Library B
{
public int Key
}
}
// ** LIBRARY B
namespace ToplogyLibrary
{
public static class NodeExtns
{
public static void FromNodeMixin<T>(this INode<T> node) {
// XXXX
}
}
public interface INode<T>
{
// Properties
T Key { get; }
// Methods
}
}
I have:
- Main Program Class - uses Library A
- Library A - has partial classes which mix in methods from Library B
- Library B - mix in methods & interfaces
Why would I need a using statement to LibaryB just to get their extension methods working in the main class? That is given that it's Library B that defines the classes that will be extended.
EDIT - Except from code
// *** PROGRAM ***
using TopologyDAL;
using Topology; // *** THIS WAS NEEDED TO GET EXTN METHODS APPEARING ***
class Program
{
static void Main(string[] args)
{
var context = new Model1Container();
Node myNode; // ** trying to get myNode mixin methods to appear seems to need using line to point to Library B ***
}
}
// ** LIBRARY A
namespace TopologyDAL
{
public partial class Node
{
// Auto generated from EF
}
public partial class Node : INode<int> // to add extension methods from Library B
{
public int Key
}
}
// ** LIBRARY B
namespace ToplogyLibrary
{
public static class NodeExtns
{
public static void FromNodeMixin<T>(this INode<T> node) {
// XXXX
}
}
public interface INode<T>
{
// Properties
T Key { get; }
// Methods
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当您导入定义扩展方法的命名空间时,扩展方法才可用。
否则,编译器将需要解析每个引用库中的每个扩展方法,这会减慢编译时间。
此外,如果不同的命名空间包含具有相同签名的扩展方法,则将无法使用扩展方法。
总之,扩展方法本身就是一种功能,需要导入其名称空间;它们不会使用它们扩展的类的名称空间自动导入。
Extension methods are only available if you import the namespace that they are defined in.
Otherwise, the compiler would need to resolve against every single extension method in every single referenced library, which would slow down the compile time.
Also, that would make it impossible to use an extension method if a different namespace contains an extension method with the same signature.
In summary, extension methods are features in their own right and require their namespace to be imported; they are not automatically imported with the namespace of the class that they extend.
这是扩展方法的一个不幸的可发现性问题。为了使它们可用,您需要为包含具有扩展的静态类的命名空间添加一个
using
语句。查看此博客,了解扩展方法。以下是有关扩展方法的一些背景:
我的理解是,使用扩展方法就像使用任何其他类型一样,只是您无法限定它们(这在语法上是不可能的),因此需要
using
语句。由于您可以在不同命名空间的不同类中定义多个它们,因此编译器需要一种方法来解决歧义。我设想将来 Visual Studio 将添加一项功能,以便在您键入方法名称时导入正确的命名空间,这与类和接口名称的处理方式类似。
考虑这种情况:
为了使用任何这些扩展方法,您需要导入正确的命名空间:
您可能会问导入两个命名空间时会发生什么。你会得到一个编译器错误,就像这样:
This is an unfortunate discoverability issue with extension methods. In order to make them available you need to add a
using
statement for the namespace containing the static class that has the extensions. Check out this blog about extension methods.Here is some background on extension methods:
My understanding is that using extension methods is just like using any other type, except that you can't qualify them (that is just syntactically impossible), hence the need for
using
statement. Since you can define multiple of them in different classes in different namespaces, the compiler needs a way to resolve ambiguity.I envisage that in future Visual Studio will add a feature to import the right namespace when you type in the method name, in a similar way it does so for class and interface names.
Consider this scenario:
In order to use any of those extension methods you need to import the right namespace:
You might ask what happens when you import both namespaces. You get a compiler error just like this: