Extension method not found (no problem with assembly reference)

I have the following extension method:

public static EntitySet<T> ToEntitySetFromInterface<T, U>(this IList<U> source) where T : class, U { var es = new EntitySet<T>(); IEnumerator<U> ie = source.GetEnumerator(); while (ie.MoveNext()) { es.Add((T)ie.Current); } return es; } 

and Im trying to use it like this:

  List<IItemMovement> p = new List<IItemMovement>(); EntitySet<ItemMovement> ims = p.ToEntitySetFromInterface<ItemMovement, IItemMovement>(); 

where ItemMovement implements IItemMovement. The compiler complains:

'System.Collections.Generic.List' does not contain the definition of 'ToEntitySetFromInterface' and the extension method 'ToEntitySetFromInterface' that takes the first argument of the type 'System.Collections.Generic.List' can be found (do you have a using directive or an assembly reference?)

Not. I do not miss the link. If I just type the name of a static class containing the method that it pops up, as well as the extension method. Thnx

+4
source share
2 answers

This code works for me, and its a direct copy of your code, minus ItemMovement and its interface, so maybe something is wrong with this part?

 public class TestClient { public static void Main(string[] args) { var p = new List<IItem>(); p.Add(new Item { Name = "Aaron" }); p.Add(new Item { Name = "Jeremy" }); var ims = p.ToEntitySetFromInterface<Item, IItem>(); foreach (var itm in ims) { Console.WriteLine(itm); } Console.ReadKey(true); } } public class Item : IItem { public string Name { get; set; } public override string ToString() { return Name; } } public interface IItem { } public static class ExtMethod { public static EntitySet<T> ToEntitySetFromInterface<T, U>(this IList<U> source) where T : class, U { var es = new EntitySet<T>(); IEnumerator<U> ie = source.GetEnumerator(); while (ie.MoveNext()) { es.Add((T)ie.Current); } return es; } } 
+2
source

This part of the compiler error is the key: "no extension method" ToEntitySetFromInterface "takes the first argument of the type" System.Collections.Generic.List ".

Your ToEntitySetFromInterface<T,U> extension method is defined to accept IList<U> , but you are trying to call it with List<T> , not IList<T> . The compiler does not find your extension method because the type does not match.

+1
source

Source: https://habr.com/ru/post/1399987/


All Articles