zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }

    看看运行的结果



  • 相关阅读:
    转载---JVM四种引用--用于记录知识
    Ionic的安装、创建、及一些记录
    Angular响应式表单--附上完整代码演示
    Angular自定义模块—使用路由实现懒加载--及错误解决
    Angular自定义模块(普通)
    Angula获取服务器数据
    Angular同步与异步获取服务数据(附完整代码)
    Angular父子组件的方法传递以及数据传递
    logrotate
    Capistrano 3
  • 原文地址:https://www.cnblogs.com/jacktu/p/910310.html
Copyright © 2011-2022 走看看