Skip to main content

Attributes::Advance : C# programming for c++ programmers

1. C# - Attributes:

Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection. Atrributes can be applied on class, methods, assemblies etc. You You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.


A few pre-defined attributes with in the .NET framework.

  • Obsolete - Marks types and type members outdated
  • WebMethod - To expose a method as an XML Web service method
  • Serializable - Indicates that a class can be serialized



Predefined Types Of Atrributes
1). AttributeUsage
2). Conditional
3). Obsolete



1). AttributeUsage:
      It specifies the types of items to which the attribute can be applied.

      Syntax for specifying this attribute is as follows:

      [AttributeUsage(
        validon,   //The parameter validon specifies the language elements on which the attribute can be placed.
    AllowMultiple=allowmultiple,   //If this is true, the attribute is multiuse
    Inherited=inherited   //If it is true, the attribute is inherited by derived classes
)]


Example: 
[AttributeUsage(AttributeTargets.Class |           //attribute can be applied on class
AttributeTargets.Constructor |   //attribute can be applied on Constructor
AttributeTargets.Field |   //attribute can be applied on Field
AttributeTargets.Method |   //attribute can be applied on Method
AttributeTargets.Property,   //attribute can be applied on Property
AllowMultiple = true)]


2). Conditional:
The Conditional Attribute allows you to create conditional methods. A conditional method is invoked only when a specific symbol has been defined via #define. Otherwise, the method is bypassed.

To use the Conditional attribute, you must include the System.Diagnostics namespace.

Conditional methods have a few restrictions.

  • Conditional methods must return void.
  • Conditional methods must be members of a class, not an interface.
  • Conditional methods cannot be preceded with the override keyword.


Example:

#define TRIAL /*w w  w .ja  v a 2s. c o  m*/

using System; 
using System.Diagnostics; 

class MainClass 
 
    [Conditional("TRIAL")]  
    void trial() { 
    Console.WriteLine("Trial version, not for distribution."); 


  
[Conditional("RELEASE")]  
void release() 

    Console.WriteLine("Final release version."); 


public static void Main() 

    MainClass t = new MainClass(); 

    t.trial(); // call only if TRIAL is defined 
    t.release(); // called only if RELEASE is defined 





3). Obsolete:
         Marks types and type members outdated

Example:
Obsolete attribute can be used with types or type members that are obsolete (Outdated). If a developer uses a type or a type member that is decorated with obsolete attribute, the compiler issues a warning or an error depending on how the attribute is configured.


using System;
using System.Collections.Generic;
public class MainClass
{
    private static void Main()
    {
        Calculator.Add(10, 15);
    }
}


public class Calculator
{
    [Obsolete("Use Add(List Numbers) instead")]
    public static int Add(int FirstNumber, int SecondNumber)
    {
        return FirstNumber + SecondNumber;
    }
    public static int Add(List Numbers)
    {
        int Sum = 0;
        foreach (int Number in Numbers)
        {
            Sum = Sum + Number;
        }
        return Sum;
    }
}



Note:

How can we create custom Attributes?
The “Obsolete” attribute which we discussed at the top is a readymade attribute To create a custom attributes you need to inherit from the attribute class. Below is a simple “HelpAttribute” which has a “HelpText” property.

Example: 

class HelpAttribute : Attribute
{
      public string HelpText { get; set; }
}

“HelpAttribute” is applied to the “Customer” as shown in the code below. Now developers who see this class , see the information right in the front of their eyes.


[Help(HelpText="This is a class")]
class Customer
{
       private string _CustomerCode;

       [Help(HelpText = "This is a property")]
       public string CustomerCode
      {
           get { return _CustomerCode; }
           set { _CustomerCode = value; }
      }


      [Help(HelpText = "This is a method")]
      public void Add()
     {
     }
}




Is it possible to restrict a custom attribute to a method only?
By using the “AttributeUsage” and “AttributeTargets” you can restrict the attribute to a particular section like class , method , property etc. Below is a simple custom attribute is now confined only to methods.


[AttributeUsage(AttributeTargets.Method)]
class HelpAttribute : Attribute
{
        public string HelpText { get; set; }

}

If you try to apply the above attribute over a class or property you would get a compile time error.





Custom attribute which describes length of characters for a property of a class.

[AttributeUsage(AttributeTargets.Property)]
class Check : Attribute
{
        public int MaxLength { get; set; }
}

Below is a customer class over which that property is decorated providing information that the maximum length of “CustomerCode” property cannot exceed 10 character.

class Customer
{
private string _CustomerCode;

        [Check(MaxLength = 10)]
public string CustomerCode
        {

        }        
    }



What if we want some attributes to be prevented from inheriting?

[AttributeUsage(AttributeTargets.Property,Inherited=false)]
class Check : Attribute
{
publicintMaxLength { get; set; } 

}




If I want an attribute to be used only once in a program? 

If you specify “AllowMultiple” as true it can be used multiple times in the same program.


[AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
class Check : Attribute
{
          public int MaxLength { get; set; }
}

Comments

Popular posts from this blog

const used with functions

const used with functions : class Dog {    int age;    string name; public:    Dog() { age = 3; name = "dummy"; }         // const parameters and these are overloaded functions    void setAge(const int& a) { age = a; }    void setAge(int& a) { age = a; }         // Const return value    const string& getName() {return name;}         // const function and these are overloaded functions    void printDogName() const { cout << name << "const" << endl; }  // value of name can't be modified    void printDogName() { cout << getName() << " non-const" << endl; } }; int main() {    Dog d;    d.printDogName();        const Dog d2;    d2.printDogName();     }

Structured Bindings

Returning multiple Values from function C++ 11 vs C++ 17 Returning compound objects Iterating over a compound collection Direct initialization Returning multiple Values from function C++ 11 vs C++ 17 :  C++ 11 (std::tie): std::tuple mytuple() {     char a = 'a';     int i = 123;     bool b = true;     return std::make_tuple(a, i, b);  // packing variable into tuple } To access return value using C++ 11, we would need something like: char a; int i; bool b; std::tie(a, i, b) = mytuple();  // unpacking tuple into variables Where the variables have to be defined before use and the types known in advance. C++ 17 : auto [a, i, b] = mytuple(); Returning compound objects :  This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an ...

POST 1 : std::thread in c++ | C++ 11

std::thread   C++  Thread support library std::thread. Threads enable programs to execute across several processor cores. Defined in header Class : thread Member types : native_handle_type Data Member  id    //represents the id of a thread (public member class) Member functions (constructor)    //constructs new thread object (public member function) (destructor)    //destructs the thread object, underlying thread must be joined or detached  (public member function) operator=    //moves the thread object  Observers  // Covered in NEXT POST Operations    // Covered in NEXT POST Constructor :   #include   void f1 ( int n ) { for ( int i = 0 ; i < 5 ; ++ i ) { std:: cout << "Thread 1 executing \n " ; ++ n ; std:: this_thread :: sleep_for ( std:: chrono :: milliseconds ( 10 ) ) ; ...