屬性上的Inherited
bool屬性是指什麼?繼承對屬性如何工作?
是否意味着如果我使用屬性AbcAtribute
(即Inherited = true
)定義了我的類,並且如果我從該類繼承另一個類,那麼派生類也會將相同的屬性應用於它?
要澄清一個代碼示例這個問題,設想以下:
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class Random: Attribute
{ /* attribute logic here */ }
[Random]
class Mother
{ }
class Child : Mother
{ }
是否Child
也有適用於它的Random
屬性?
當你問這個問題時,情況並非如此,而是今天[繼承的屬性的官方文檔](https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited。 aspx)有一個精心設計的例子,它顯示了繼承類和覆蓋方法的'Inherited = true'和'Inherited = false'之間的區別。 – 2017-08-28 20:31:38