Gra*_*ark 9 c# linq extension-methods visual-studio-2013
我有3个C#项目/程序集,A,B和C. B引用A,C引用B.
在一个:
using System.Collections.Generic;
namespace A
{
public class Dict : Dictionary<string, object> {}
public class Options: Dict {}
}
Run Code Online (Sandbox Code Playgroud)
在B:
using System.Collections.Generic;
using A;
namespace B
{
public class Client
{
public void M(IDictionary<string, string> d)
{
var dict = d.ToDict<Options>();
}
}
public static class Extensions
{
public static T ToDict<T>(this IDictionary<string, string> dictionary)
where T : Dict, new()
{
return new T();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在C:
using System.Collections.Generic;
using B;
namespace C
{
public class Worker
{
public void M()
{
var client = new Client();
var d = new Dictionary<string, string> { { "k", "v" } };
var strings = new [] { "one", "two" }; // never used
client.M(d);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都编译好了.
如果我System.Linq在项目C中导入名称空间并strings.Select(s => s);在调用之前添加行(或任何LINQ方法)client.M(),我会收到编译器错误:
"A.Dict"类型在未引用的程序集中定义.您必须添加对程序集"A,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"的引用.
创建Extensions类internal或将其移动到不同的命名空间可以防止错误.
由于B应该包装A并从C隐藏其细节,因此将扩展方法公之于众是错误的.但我不明白为什么只有在引入LINQ时编译器才会选择它.
是否存在至少一种扩展方法会导致编译器的行为略有不同?
添加任何未解析的方法都会触发此错误。
我不记得在哪里读到过这个,但是 C# (VS2013) 编译器在找到匹配的非扩展方法时将停止解析扩展方法。您可以通过在项目 C 中添加以下代码来验证这一点。
public static class Extensions
{
public static void M(this Client c, IDictionary<string, string> d)
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
它不会导致任何错误!
当某个方法未使用非扩展方法解析时(例如,add"abc".Foo()导致编译器错误),现在 C# 编译器开始扫描扩展方法。当它扫描时B.Extensions.ToDict,它不知道Dict是什么。但是,程序集 B 的元数据告诉编译器它来自A.Dict,然后您会看到错误。
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |