2
聲明我有以下的推導:檢查一類的屬性或方法作爲密封
interface IMyInterface
{
string myProperty {get;}
}
class abstract MyBaseClass : IMyInterface // Base class is defining myProperty as abstract
{
public abstract string myProperty {get;}
}
class Myclass : MyBaseClass // Base class is defining myProperty as abstract
{
public sealed override string myProperty
{
get { return "value"; }
}
}
我想能夠檢查如果一個類的成員,密封聲明。有點像這樣:
PropertyInfo property = typeof(Myclass).GetProperty("myProperty")
bool isSealed = property.GetMethod.IsSealed; // IsSealed does not exist
所有這一切的意義在於能夠運行測試,檢查代碼/項目的一致性。
下面的測試失敗:
PropertyInfo property = typeof(Myclass).GetProperty("myProperty")
Assert.IsFalse(property.GetMethod.IsVirtual);
屬性不能密封。類可以。 –
'bool isSealed =!property.GetMethod.IsVirtual;' –
在C#方法默認情況下是「密封的」(不能被重寫)。你必須將它們明確地標記爲「虛擬」,這就是爲什麼檢查虛擬方法比「密封」方法更有意義。 –