Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird compilation error when indirectly refer to an assembly that declares a generic extension method with type restriction
    primarykey
    data
    text
    <p>Well, it's clear for me that the title of my question is too complicated. I just tried to make it as specific as possible. So, I'll try to explain the problem better.</p> <h2>Problem context</h2> <p>Let's assume we have three .NET projects in a solution. The main project is a simple console application <strong>ApplicationAssembly</strong>. This project has a reference to another managed assembly library <strong>DirectlyReferencedLibrary</strong>. At the same time DirectlyReferencedLibrary refers to <strong>IndirectlyUsedLibrary</strong>.</p> <p>So, the project usages looks like that: <strong>ApplicationAssembly --> DirectlyReferencedLibrary --> IndirectlyUsedLibrary</strong>.</p> <p>Notice that ApplicationAssembly doesn't use directly any type declared IndirectlyUsedLibrary. Let's also assume that all types declared in these assemblies reside in the same namespace.</p> <p>This solution compiles and works fine.</p> <h2>Weird problem</h2> <p>The problem occurs when I have together the following conditions:</p> <ol> <li>the <strong>ApplicationAssembly</strong> project has usages of LINQ expressions. For example, if there is the invocation of Select() on any object of enumerable type.</li> <li>The <strong>DirectlyReferencedLibrary</strong> declares a class which has a generic extension method with a type restriction. The type restriction says that the generic type must be a descendant of a class from the <strong>IndirectlyUsedLibrary</strong>.</li> </ol> <p>Here is the example of a such class.</p> <pre><code>using System; namespace Test { public static class UnUsedType { // It's a generic extension method with a type restriction. public static void Magic&lt;T&gt;(this T @this) // It's a type restriction that uses a type from the IndirectlyUsedLibrary. where T : ProblemType { Console.WriteLine("I do nothing actually."); } } } </code></pre> <p>When I try to compile this project, I get the following error:</p> <blockquote> <p>Error <strong>The type 'Test.ProblemType' is defined in an assembly that is not referenced. You must add a reference to assembly 'IndirectlyUsedLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. C:\Projects\Test\ApplicationAssembly\Program.cs</strong> 22 13 ApplicationAssembly</p> </blockquote> <h2>Question</h2> <p>Can anyone help me to understand why is it so?</p> <h2>P.S.</h2> <p>I've made a tiny solution for investigation. <em>If you are so kind to help me, you will be able to take <strong><a href="http://www.mediafire.com/?ca697za1zs4dhad" rel="nofollow" title="here">an archived solution here</a></em></strong></p> <h2>P.P.S.</h2> <p>Sorry for my poor English.</p> <h2>UPD1</h2> <p>Another strange thing is that different invocations of the LINQ method may or may not produce the compile time error:</p> <pre><code>// Ok. Let's do some work using LINQ we love so much! var strings = new[] { "aaa", "bbb", "ccc" }; Func&lt;string, object&gt; converter = item =&gt; (object) item; // The following line makes problems. var asObjects1 = strings.Select(converter); // Everything is OK if we use the following line: var asObjects2 = Enumerable.Select(strings, converter); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload