Invoking an extension method that works on a interface from an implementor seems to require the use of the this keyword. This seems odd.
Does anyone know why?
Is there an easier way to get shared implementation for an interface?
This irks me as I'm suffering multiple inheritance/mixin withdrawl.
Toy example:
public interface ITest
{
List<string> TestList { get; }
}
public static class TestExtensions
{
private const string Old = "Old";
private const string New = "New";
public static void ManipulateTestList(this ITest test)
{
for (int i = 0; i < test.TestList.Count; i++)
{
test.TestList[i] = test.TestList[i].Replace(Old, New);
}
}
}
public class Tester : ITest
{
private List<string> testList = new List<string>();
public List<string> TestList
{
get { return testList; }
}
public Tester()
{
testList.Add("OldOne");
testList.Add("OldTwo");
// Doesn't work
// ManipulateTestList();
// Works
this.ManipulateTestList();
}
}