Geeks With Blogs
Geekette Mai Blog

 

       
       
 


I have not programming in a while, and I have forgotten some of the concept and definition of terminology.  Until I was being asked, they took me a while before they snapped back in my brain :-)

Custom Attribute?
For example: How I create a Custom attribute?

 

From what I remember the primary step to properly design custom attribute classes  follows:

 

Applying the AttributeUsageAttribute([AttributeUsage(AttributeTargets.All, Inherited=false, AllowMutiple=true)])

 

a)     Declaring the attribute.  (Class public class MyAttribute: System. Attribute {//...}

 

b)     Declaring constructors (Public  MyAttribute(bool  myvalue){this.myvalue= myvalue;})

 

c)     Declaring properties

 

Public bool MyProperty

{

get {return this myvalue;}

Set {this.myvalue = value;}

      }

 

 

The following is the sample demonstrates the basic way of using reflection to get access to custom attributes:

 

 Class MainClass

{

public static void Main()

{

 System.Reflection.MemberInfo info = typeof(myclass);

 object [] attributes  =  info.getcustomAttributes( );

 for  (int  i = 0  ;  i  <  attributes. Length;  i ++)

{

System.Console.Writeline(attributes  [ i ] ;

}

}

}

Static constructor
If I were asked that different type constructor and instance constructor. then I will flick to

 

What is static constructor, when it will be fired?

 

How to use it Instance constructor is executed when a new instance of type is created and the class instructor is executed after the type is loaded and before one of the type members is accessed. ( it will get executed only 1st time, when we call any static methods/fields in the same class) Class constructor is used for static field initialization.

 

Only one class constructor per type is permitted, and it cannot use the variable argument calling convention

 

A static constructor is used to initialize a class.  

 

It is called automatically to initialize the class before the first instance created or any static members are referenced.

Posted on Friday, February 12, 2010 12:29 PM | Back to top


Comments on this post: C#, Custom Attribute, Static Constructor

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mai Nguyen | Powered by: GeeksWithBlogs.net