MethodBase.IsFinal Property


获取一个值,该值指示此方法是否为 finalGets a value indicating whether this method is final.

 property bool IsFinal { bool get(); };
public bool IsFinal { get; }
member this.IsFinal : bool
Public ReadOnly Property IsFinal As Boolean

Property Value


如果方法为 final,则为 true;否则为 falsetrue if this method is final; otherwise, false.



下面的示例显示 IsFinal``false,这可能会导致你认为 MyMethod 是可重写的。The following example displays false for IsFinal, which might lead you to think that MyMethod is overridable. 即使 MyMethod 未标记为 virtual,代码也会打印 false,因此不能重写。The code prints false even though MyMethod is not marked virtual and thus cannot be overridden.

using namespace System;
using namespace System::Reflection;

public ref class MyClass
   void MyMethod(){}

int main()
   MethodBase^ m = MyClass::typeid->GetMethod( "MyMethod" );
   Console::WriteLine( "The IsFinal property value of MyMethod is {0}.", m->IsFinal );
   Console::WriteLine( "The IsVirtual property value of MyMethod is {0}.", m->IsVirtual );
using System;
using System.Reflection;

public class MyClass
    public void MyMethod()
    public static void Main()
        MethodBase m = typeof(MyClass).GetMethod("MyMethod");
        Console.WriteLine("The IsFinal property value of MyMethod is {0}.", m.IsFinal);
        Console.WriteLine("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual);
Imports System.Reflection

Public Class MyClass1
    Public Sub MyMethod()
    End Sub
    Public Shared Sub Main()
        Dim m As MethodBase = GetType(MyClass1).GetMethod("MyMethod")
        Console.WriteLine("The IsFinal property value of MyMethod is {0}.", m.IsFinal)
        Console.WriteLine("The IsVirtual property value of MyMethod is {0}.", m.IsVirtual)
    End Sub
End Class


如果虚方法标记为 final,则不能在派生类中重写。If the virtual method is marked final, it can't be overridden in derived classes. 重写的虚拟方法可以使用密封关键字标记 final 在C# C++/cli 中使用NotOverridable Visual Basic 关键字,或使用密封关键字标记The overridden virtual method can be marked final using the sealed keyword in C#, NotOverridable keyword in Visual Basic, or sealed keyword in C++/CLI. 方法还可以由编译器隐式标记 finalThe method can also be marked final implicitly by the compiler. 例如,可在代码中将方法定义为非虚方法,但它实现接口方法。For example, a method might be defined as non-virtual in your code, but it implements an interface method. 公共语言运行时要求实现接口成员的所有方法都必须标记为 virtual;因此,编译器会将方法标记 virtual finalThe Common Language Runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final.

可以将此属性与 IsVirtual 属性结合使用,以确定方法是否可重写。You can use this property, in conjunction with the IsVirtual property, to determine if a method is overridable. 要使方法具有可重写的,必须 true IsVirtual 属性,并且必须 false``IsFinal 属性。For a method to be overridable, IsVirtual property must be true and IsFinal property must be false. 若要确定某个方法是否可重写,请使用如下所示的代码:To establish with certainty whether a method is overridable, use code such as this:

if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If MethodInfo.IsVirtual AndAlso Not MethodInfo.IsFinal Then

如果 IsVirtual falseIsFinal true,则不能重写此方法。If IsVirtual is false or IsFinal is true, then the method cannot be overridden.

Applies to

See also