C#高级编程之“代码属性”

本文主要通过一个实际的例子来解释“代码属性”的概念

代码属性是与代码一起在程序的运行过程中为代码服务的,理解“代码属性”概念的关键在于认识到“代码属性”也是一种类,它是一种特殊的类,一种从System.Attribute继承出来的类。

下面的代码主要实现的功能是:在代码运行异常的时候,根据该段代码的“代码属性”给出这段代码的作者以及这名作者的电子信箱。

下面给出一个自定义的“代码属性”的类:

 

using System;

namespace CodeProperty
{
    
/// <summary>
    
/// CustomAttribute 的摘要说明。
    
/// </summary>
    [AttributeUsage(AttributeTargets.All)]
    
public class CustomAttribute:System.Attribute
    {
        
public CustomAttribute(string name,string email)
        {
            
this.CodeAuthorName=name;
            
this.CodeAuthorEmail=email;
        }
        
private string CodeAuthorName=string.Empty;
        
private string CodeAuthorEmail=string.Empty;
        
public string AuthorName
        {
            
get
            {
                
return this.CodeAuthorName;
            }
            
set
            {
                
this.CodeAuthorName=value;
            }
        }
        
public string AuthorEmail
        {
            
get
            {
                
return this.CodeAuthorEmail;
            }
            
set
            {
                
this.CodeAuthorEmail=value;
            }
        }

    }
}

 

为了使用这个自定义的“代码属性”的类,我们写了一个有问题的代码:

 

using System;
using System.Reflection;

namespace CodeProperty
{
    
/// <summary>
    
/// ClassWithError 的摘要说明。
    
/// </summary>
    public class ClassWithError
    {
        
public ClassWithError()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
            
        }
        [Custom(
"testname""testname@21cn.com")]
        
public void DoSomething()
        {
            
try
            {
                
int x=20;
                
int y=4;
                
int z=y-4;
                
int result=x/z;
            }
            
catch
            {
                Console.WriteLine(
"代码有一些错误发生");
                Console.WriteLine(
"int x=20;");
                Console.WriteLine(
"int y=4;");
                Console.WriteLine(
"int z=y-4;");
                Console.WriteLine(
"int result=x/z;");
                CustomAttribute ca
=CustomAttributeTool.GetAttributeData(MethodBase.GetCurrentMethod());
                Console.WriteLine(
"本段代码由{0}编写,他/她的电子信箱是{1}",ca.AuthorName,ca.AuthorEmail);                
            }
        }

    }
}

 

注意在里面有一个CustomAttributeTool,这个类的目的是得到CustomAttribute类的作者属性与作者的电子信箱

代码如下:

 

using System;
using System.Reflection;
namespace CodeProperty
{
    
/// <summary>
    
/// CustomAttributeTool 的摘要说明。
    
/// </summary>
    public class CustomAttributeTool
    {
        
public CustomAttributeTool()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }
        
public static CustomAttribute GetAttributeData(MethodBase method)
        {
            
object[] attributes=method.GetCustomAttributes(typeof(CustomAttribute),true);
            
return (CustomAttribute)attributes[0];
        }
    }
}

 

好了,最后我们来写一个带main的类来调用它们就可以了

带main的类的代码如下:

 

using System;

namespace CodeProperty
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>
    class Class1
    {
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        [STAThread]
        
static void Main(string[] args)
        {
            
//
            
// TODO: 在此处添加代码以启动应用程序
            
//
            ClassWithError cwe=new ClassWithError();
            cwe.DoSomething();
            Console.ReadLine();
        }
    }
}

 

看看运行的结果



posted @ 2007-09-29 09:50  jacktu  阅读(475)  评论(0编辑  收藏  举报