Can you post your class hierarchy?
The following code shows that the most derived compile-time type Extension Method is choosen.
Does it applies to your code?
[TestMethod]
public void Test1()
{
IEnumerable<MyCustomClass> myCustomList = new MyCustomList()
{
new MyCustomClass() { Int1 = 1},
new MyCustomClass() { Int1 = 2},
new MyCustomClass() { Int1 = 3},
};
//Ignores MyCustomList method and uses IEnumerable<> method.
Assert.AreEqual(2, myCustomList.Where(x => x.Int1 > 1).Count());
}
[TestMethod]
public void Test2()
{
MyCustomList myCustomList = new MyCustomList()
{
new MyCustomClass() { Int1 = 1},
new MyCustomClass() { Int1 = 2},
new MyCustomClass() { Int1 = 3},
};
//Uses MyCustomList method
Assert.AreEqual(1, myCustomList.Where(x => x.Int1 > 1).Count());
}
[TestMethod]
public void Test3()
{
ISomeInterface myCustomList = new MyCustomList()
{
new MyCustomClass() { Int1 = 1},
new MyCustomClass() { Int1 = 2},
new MyCustomClass() { Int1 = 3},
};
//If your type is ISomeInterface, the compiler uses ISomeInterface method, even if the runtime instance is MyCustomList
Assert.AreEqual(0, myCustomList.Where(x => x.Int1 > 1).Count());
}
[TestMethod]
public void Test4()
{
MyDerivedCustomList myCustomList = new MyDerivedCustomList()
{
new MyCustomClass() { Int1 = 1},
new MyCustomClass() { Int1 = 2},
new MyCustomClass() { Int1 = 3},
};
//Even if MyDerivedCustomList implements ISomeInterface, the compiler uses MyCustomList method
Assert.AreEqual(1, myCustomList.Where(x => x.Int1 > 1).Count());
}
[TestMethod]
public void Test5()
{
ISomeInterface myCustomList = new MyDerivedCustomList()
{
new MyCustomClass() { Int1 = 1},
new MyCustomClass() { Int1 = 2},
new MyCustomClass() { Int1 = 3},
};
//If your type is ISomeInterface, the compiler uses ISomeInterface method, even if the runtime instance is MyDerivedCustomList
Assert.AreEqual(0, myCustomList.Where(x => x.Int1 > 1).Count());
}
Used this classes:
public class MyCustomClass
{
public int Int1 { get; set; }
}
public class MyCustomList : List<MyCustomClass>, ISomeInterface
{
public MyCustomList() : base() { }
public MyCustomList(IEnumerable<MyCustomClass> coll) : base(coll) { }
}
public interface ISomeInterface : IEnumerable<MyCustomClass>
{
}
public class MyDerivedCustomList : MyCustomList, ISomeInterface
{
public MyDerivedCustomList() : base() { }
public MyDerivedCustomList(IEnumerable<MyCustomClass> coll) : base(coll) { }
}
public static class MyExtensions
{
/// <summary>
/// Where(x => x.Int1 > 2)
/// </summary>
public static IEnumerable<MyCustomClass> Where(this MyCustomList myList, Func<MyCustomClass, bool> predicate)
{
return new MyCustomList(Enumerable.Where(myList, predicate).Where(x => x.Int1 > 2));
}
/// <summary>
/// Where(x => x.Int1 > 3)
/// </summary>
public static IEnumerable<MyCustomClass> Where(this ISomeInterface myList, Func<MyCustomClass, bool> predicate)
{
return new MyCustomList(Enumerable.Where(myList, predicate).Where(x => x.Int1 > 3));
}
}