致力于技术进步

专注于编程艺术

博客园 首页 新随笔 联系 订阅 管理

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

代码属性是与代码一起在程序的运行过程中为代码服务的,理解“代码属性”概念的关键在于认识到“代码属性”也是一种类,它是一种特殊的类,一种从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();
        }
    }
}
看看运行的结果

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/niuyongjie/archive/2006/09/11/1210363.aspx

posted on 2011-06-09 10:10  stephen&amp;#183;周  阅读(264)  评论(0)    收藏  举报