我用一對夫婦的擴展方法來實現這一目標:
public static class ISupportInitializeHelper
{
const string BEGIN_INIT = "System.ComponentModel.ISupportInitialize.BeginInit",
END_INIT = "System.ComponentModel.ISupportInitialize.EndInit";
public static void InvokeBaseBeginInit<T>(this T obj)
where T : ISupportInitialize
{
var baseType = typeof(T).BaseType;
var methodInfo = GetBeginInitMethodInfo(baseType);
if (methodInfo != null)
methodInfo.Invoke(obj, null);
}
static Dictionary<Type, MethodInfo> s_beginInitCache = new Dictionary<Type, MethodInfo>();
private static MethodInfo GetBeginInitMethodInfo(Type type)
{
MethodInfo methodInfo;
if (!s_beginInitCache.TryGetValue(type, out methodInfo))
{
methodInfo = type.GetMethod(BEGIN_INIT,
BindingFlags.NonPublic |
BindingFlags.Instance);
s_beginInitCache[type] = methodInfo;
}
return methodInfo;
}
public static void InvokeBaseEndInit<T>(this T obj)
where T : ISupportInitialize
{
var baseType = typeof(T).BaseType;
var methodInfo = GetEndInitMethodInfo(baseType);
if (methodInfo != null)
methodInfo.Invoke(obj, null);
}
static Dictionary<Type, MethodInfo> s_endInitCache = new Dictionary<Type, MethodInfo>();
private static MethodInfo GetEndInitMethodInfo(Type type)
{
MethodInfo methodInfo;
if (!s_endInitCache.TryGetValue(type, out methodInfo))
{
methodInfo = type.GetMethod(END_INIT,
BindingFlags.NonPublic |
BindingFlags.Instance);
s_endInitCache[type] = methodInfo;
}
return methodInfo;
}
}
在你的類,明確實施ISupportInitialize
並調用相應的擴展方法,例如:
public class MyBindingSource
: BindingSource,
ISupportInitialize
{
void ISupportInitialize.BeginInit()
{
this.InvokeBaseBeginInit();
// More begin init logic
}
void ISupportInitialize.EndInit()
{
this.InvokeBaseEndInit();
// More end init logic
}
}
您不必明確實現這兩種方法,因爲基類已經實現了,所以如果你只添加感興趣的LO gic初始化後,你可以省略BeginInit()
。