Click here to Skip to main content
13,450,154 members (55,733 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 28 Jan 2013

Forcing Derived Type to Apply Custom Attribute

, 28 Jan 2013
Rate this:
Please Sign up or sign in to vote.
Forcing Derived type to apply custom attribute


This article illustrates options to force attributes on derived types.


In .NET eco system, Attributes are not inheritable. This means any attribute applied on base class are not applied/inherited on derived class. This article looks at different options available to force the attributes on derived types at compile time and at run time.


There are two approaches (with different intentions)

  1. Forcing at run time: The base class constructor code will check for the presence of attribute and throw error runtime if attribute is not changed.
  2. Forcing at compile time: Using ObsoleteAttribute, the Mandatory attribute will be reflected at compile time and will raise the error. This information is taken from this question on

Using the Code

Forcing Attribute at Run Time

The IsAssignableFrom takes care of any attribute that derives from CustomAttribute.

  1. Create a Custom Attribute:
    class CustomAttribute : System.Attribute { }  
  2. In the constructor of base, check the attribute:
    publicBase() { CheckCustomAttribute(); }
  3. If class type is base, skip the attribute check:
    if (!(this.GetType() == typeof(Base)))
  4. If class is derived type, check whether attribute exists or not.
    var attr = System.Attribute.GetCustomAttributes(
                            .SingleOrDefault(t => typeof(CustomAttribute)
  5. Throw error if attribute does not exist.
    if (attr == null)
         throw new Exception(String.Format(
                             "Derived class {0} doesnot apply {1} attribute",

Forcing Attribute at Compile Time

There is a special attribute ObsoleteAttribute. It is sealed so cannot be subclassed. The C# compiler has special handling for this attribute.

However, to use Obsolete attribute rather awkwardly, It can be applied to a custom attribute. Then applying that custom attribute (on class/structs) will force compiler to show error.

  1. Create a custom attribute:
    public class MandatoryAttribute : Attribute{}
  2. Decorate the custom attribute with ObsoleteAttribute:
    [Obsolete("MandatoryAttribute is required", true)]
    public class MandatoryAttribute : Attribute{}
  3. Apply Custom attribute to class/member to get notification (as compile time error) on any type:
    public class Base
     public void SayHello() {}

Uncommenting MandatoryAttribute raises awkward compile time error.

This is awkward because the error message is like:

"MandatoryAttribute’ is obsolete: ‘MandatoryAttribute is required’    
<filename>    <projectname>’"

Download the source code from src link for this post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Software Engineer based out in Noida.

Technology skillset – .NET, WPF, WCF, LINQ, XAML.

Started blogging on

Stackoverflow Profile ->

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Matej Hlatky29-Jan-13 3:56
memberMatej Hlatky29-Jan-13 3:56 
QuestionI don't get this Pin
John Brett29-Jan-13 1:30
memberJohn Brett29-Jan-13 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180318.3 | Last Updated 28 Jan 2013
Article Copyright 2013 by Fun@learn
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid