Click here to Skip to main content
Click here to Skip to main content

Tagged as

Forcing Derived Type to Apply Custom Attribute

, 17 Jan 2013
Rate this:
Please Sign up or sign in to vote.
Forcing Derived Type to Apply Custom Attribute.

Introduction

This article illustrates options to force attributes on derived types. 

Background  

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 forcer the attributes on derived types at compile time and at run time. 

Approach  

There are two approaches (with different intensions) 

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 Stackoverflow.com

Using the code 

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Forcing attribute at run time   

  • Creating a Custom Attribute
  • class CustomAttribute : System.Attribute {} 
  • In the constructor of base check the attribute 
  •  public Base () { CheckCustomAttribute(); } 
  • If class type is base, skip the attribute check 
  •  if(!(this.GetType() == typeof(Base)))  
  • If class is derived type, check whether attribute exists or not. The IsAssignableFrom take care of any attribute that derives from CustomAttribute 
  • var attr = System.Attribute.GetCustomAttributes(
                             this.GetType())
                            .SingleOrDefault(t => typeof(CustomAttribute)
                            .IsAssignableFrom(t.GetType()));   
  • Throw error if attribute does not exist. 
  • if (attr == null)
     {
         throw new Exception(String.Format(
                             "Derived class {0} doesnot apply {1} attribute",
                             this.GetType().Name,
                             typeof(CustomAttribute).Name));
     }

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 

  • Create a custom attribute   
  •  public class MandatoryAttribute : Attribute{} 
  • Decorate the custom attribute with ObsoleteAttribute 
  • [Obsolete("MandatoryAttribute is required", true)]
    public class MandatoryAttribute : Attribute{} 
  • Apply Custom attribute to class/member to get notification (as compile time error) on any type 
  • //[MandatoryAttribute]
    public class Base
    {
     //[MandatoryAttribute]
     public void SayHello() {}
    } 
Uncommenting MandatoryAttribute raises an awkward compile time error. This is awkward because the error message is like 

“MandatoryAttribute’ is obsolete: ‘MandatoryAttribute is required’    <filename>    <projectname>’”

License

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

Share

About the Author

Fun@learn
Software Developer (Senior)
India India
Software Engineer based out in Noida.
 
Technology skillset – .NET, WPF, WCF, LINQ, XAML.
 
Started blogging on http://1wpf.wordpress.com/
 

Stackoverflow Profile -> http://stackoverflow.com/users/649524/tilak

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 17 Jan 2013
Article Copyright 2013 by Fun@learn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid