गुण कोडइनहेरिट की गई विशेषताओं
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class IgnoreAttribute : Attribute
{
}
बेस वर्ग
abstract class ManagementUnit
{
[Ignore]
public abstract byte UnitType { get; }
}
मुख्य वर्ग
class Region : ManagementUnit
{
public override byte UnitType
{
get { return 0; }
}
private static void Main()
{
Type t = typeof(Region);
foreach (PropertyInfo p in t.GetProperties())
{
if (p.GetCustomAttributes(typeof(IgnoreAttribute), true).Length != 0)
Console.WriteLine("have attr");
else
Console.WriteLine("don't have attr");
}
}
}
आउटपुट:don't have attr
बताएं कि यह क्यों हो रहा है? आखिरकार, इसे विरासत में मिला होना चाहिए।
नहीं है, के लिए डिफ़ॉल्ट "प्राप्त" सच है। https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited(v=vs.110).aspx – 00jt