开发者

C# VS2008 Compiler picking up wrong Methods

I've got a really odd problem when building a project in VS2008. Having made some completely un-related changes I started getting a build error. Basically the compiler picks up the incorrect extension method.


Assembly1: References Assembly4.

[DebuggerHidden]
public static List<T> ToList<T>(this IEnumerable<T> source)
{}

[System.Runtime.InteropServices.ComVisible(false), CLSCompliant(false)]
public static List<T> ToList<T>(this IConcreteCollection collection)
{}

Assembly3: Only References Assembly1. Note that CustomClass does not implement IConcreteCollection.

List<CustomClass> list = new CustomClass[].ToList();

Assembly4: IConcreteCollection defined.


Error message:

The type 'Assembly4.Namespace.IConcreteCollection' is defined in an assembly that is not referenced. You must add a reference to assembly 'Assembly4.Namespace, Version=...'.


As you can see, Assembly3 has incorrectly attempted to use the wrong Extension in Assembly1, despite the fact that the types do not match.

VS2开发者_如何学JAVA008 goto-definition however works correctly and points to the correct method in Assembly1.

Anyone else experienced this or know what might be the problem?


Okay, now you've updated the question, the error makes perfect sense. If you're going to have a public method in Assembly1 which references a type from Assembly4, anyone potentially using that method Assembly1 needs a reference to Assembly4. Basically the compiler looks at the candidate method and doesn't know enough to decide whether it's applicable or not.

Just add the reference to Assembly3 from Assembly4 and all should be well. If you don't want to add that reference, you should change the name of one of the methods, or make it internal/private. Don't ask the compiler to examine signatures it doesn't understand :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜