. , (, IBase, Base Derived, IBase ).
public interface IBase {...}
public class Base : IBase {...}
public class Derived : Base {...}
, ext IBase. ext Base, Derived.
public class MyClass {
public IBase ext;
}
AddedMethod(), IBase , . , , :
public static class MyExtensions
{
public static void AddedMethod(this IBase arg) {...}
public static void AddedMethod(this Base arg) {...}
public static void AddedMethod(this Derived arg) {...}
}
ext.AddedMethod() MyClass. : , (.. AddedMethod ( IBase arg)) ext.
IBase, , , :
public static class MyExtensions
{
public static void AddedMethod(this IBase arg){
Type itsType = arg.GetType();
MethodInfo mi = typeof(MyExtensions).GetMethod("innerAddedMethod",
BindingFlags.NonPublic | BindingFlags.Static,
null,
new Type[] { itsType },
null);
if (mi != null) {
mi.Invoke(null, new object[] { arg });
}
}
private static void innerAddedMethod(Base arg) {
}
private static void innerAddedMethod(Derived arg) {
}
Derived2 IBase, MyExtensions innerAddedMethod(), Derived2 .