Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird compile-time error on Select() and ToList() LINQ methods
    primarykey
    data
    text
    <p>I have a few simple lines of code in a WPF project which give me compilation errors.</p> <pre><code>var resourceNames = new List&lt;string&gt; { "cmbItem1", "cmbItem2", "cmbItem3", "cmbItem4" }; var comboBoxItems = resourceNames.Select(id =&gt; (string)FindResource(id)).ToList(); // other code </code></pre> <p>The compilation error text is</p> <blockquote> <p><strong><em>Error 2 The type 'System.Windows.Forms.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.</em></strong></p> </blockquote> <p>Why have I reference that assembly? I don't use any type from System.Windows.Forms.Control!</p> <p>If I rewrite the code to make static methods invocations instead of extension methods invocations, it works fine:</p> <pre><code>var resourceNames = new List&lt;string&gt; { "cmbItem1", "cmbItem2", "cmbItem3", "cmbItem4" }; var comboBoxItems = Enumerable.Select(resourceNames, id =&gt; (string)FindResource(id)); comboBoxItems = Enumerable.ToList(comboBoxItems); // other code </code></pre> <p>Why is it so? What am I doing wrong?</p> <p><strong><em>UPD1:</em></strong> <img src="https://i.stack.imgur.com/DtKYu.png" alt="An error is in the list."></p> <p><strong><em>UPD2: THE ANSWER</em></strong></p> <p>I and my colleague have investigated the project and found that the reason of the problem. Here are several facts about our visual studio solution:</p> <ol> <li>The problematic code resides in project called Flyer2. That project has a reference to another project called Global.</li> <li>The project Global has several classes which provide global accessible extension methods.</li> <li>One of the extension method has a restriction for a generic type in where clause. The restriction says that the generic type must be an descendant of System.Windows.Control... For some reason existence of a such method produces the compile-time error I've described...</li> </ol> <p>Here is the code of the extension method and the class defining it:</p> <pre><code>using System.Windows.Forms; namespace Global { public static class ControlExtensions { // If I remove the where-clause, the code problem disappears! public static T FindParentByType&lt;T&gt;(this Control parentControl) where T : Control { var currentControl = parentControl != null ? parentControl.Parent : null; while (currentControl != null) { if (currentControl is T) return (T)currentControl; currentControl = currentControl.Parent; } return null; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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