简单的Attribute实现.

C# Attribute class
    [AttributeUsage(AttributeTargets.Method)]
    
public class FastyouAttribute:Attribute
    
{
        
private string _name = string.Empty;
    
        
public FastyouAttribute(string name)
        
{
            _name 
= name; 
        }


        
public string Name
        
{
            
get return _name; }
            
set { _name = value; }
        }

    }
应用这个类
    public class AttributeClass
    
{
        [Fastyou(
"what's your name")]
        
public void getName()
        
{
            
//string name = "name";
        }

    }
访问结果
        public void AttributeFastyou()
        
{
            Type type 
= typeof(AttributeClass);
            
foreach (MethodInfo mothod in type.GetMethods())
            
{
                
foreach (Attribute at in mothod.GetCustomAttributes(true))
                
{
                    FastyouAttribute att 
= at as FastyouAttribute;
                    
if (att != null)
                    
{
                        
string sss = att.Name;//"what's your name"
                        string ddd = mothod.Name;//getName
                    }

                }


            }

        }


posted @ 2007-07-29 22:52  skylai  阅读(376)  评论(0编辑  收藏  举报