C# 在调用时检测子类是否对基类的方法进行了重写
作者:互联网
使用场景
public bool IsOverrideOnNothing()
{
var mName = nameof(OnNothing);
var key = this.GetType().FullName + "_" + mName;
if (!_overrideCache.ContainsKey(key))
{
//查找 protected https://stackoverflow.com/questions/3393975/getting-methodinfo-for-protected-and-public-methods-only
var m = this.GetType().GetMethod(mName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
var isOverride = !(m.DeclaringType == typeof(Test));
_overrideCache.Add(key, isOverride);
}
return _overrideCache[key];
}
public static Dictionary<string, bool> _overrideCache { get; private set; }
= new Dictionary<string, bool>();
标签:C#,子类,public,overrideCache,BindingFlags,key,基类,var,mName 来源: https://www.cnblogs.com/trykle/p/16291836.html