关于 C# 中的 isOverrided
about isOverrided at C#
如何区分派生的class是否实现了方法的重写?
class BaseClass
{
public virtual void targetMethod() { return; }
}
class DerivedClass:BaseClass
{
public bool isOverrideTargetMethod()
{
//Here, I wants to judge whether DerivedClass is overrided targetMethod.
}
public override void targetMethod()
{
base.targetMethod();
}
}
首先,从设计的角度来看,您尝试做的事情不是很好,但如果您真的想做,反思就是答案。
using System.Reflection;
Type classType = typeof(DerivedClass);
MethodInfo method = classType.GetMethod("targetMethod");
if (method.DeclaringType == typeof(BaseClass))
Console.WriteLine("targetMethod not overridden.");
else
Console.WriteLine("targetMethod is overridden " + method.DeclaringType.Name);
如何区分派生的class是否实现了方法的重写?
class BaseClass
{
public virtual void targetMethod() { return; }
}
class DerivedClass:BaseClass
{
public bool isOverrideTargetMethod()
{
//Here, I wants to judge whether DerivedClass is overrided targetMethod.
}
public override void targetMethod()
{
base.targetMethod();
}
}
首先,从设计的角度来看,您尝试做的事情不是很好,但如果您真的想做,反思就是答案。
using System.Reflection;
Type classType = typeof(DerivedClass);
MethodInfo method = classType.GetMethod("targetMethod");
if (method.DeclaringType == typeof(BaseClass))
Console.WriteLine("targetMethod not overridden.");
else
Console.WriteLine("targetMethod is overridden " + method.DeclaringType.Name);