using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AttributeExam
{
    class Program
    {
        private static readonly StringBuilder outputText = new StringBuilder(1000);
        private static DateTime backDateTo = new DateTime(2010, 2, 1);
        static void Main(string[] args)
        {
            Assembly theAssembly = Assembly.Load("VectorLibrary");
            Attribute supportsAttribute =
                Attribute.GetCustomAttribute(
                    theAssembly, typeof(SupportsWhatsNewAttribute));

            string name = theAssembly.FullName;

            AddToMessage("Assembly: " + name);
            if (supportsAttribute == null)
            {
                AddToMessage(
                    "This assembly does not support WhatsNew attributes");
                return;
            }
            else
            {
                AddToMessage("Defined Types:");
            }

            Type[] types = theAssembly.GetTypes();
            foreach (Type definedType in types)
                DisplayTypeInfo(definedType);

            MessageBox.Show(outputText.ToString(),
                            "What\'s New since " + backDateTo.ToLongDateString());
            Console.ReadLine();
        }

        private static void DisplayTypeInfo(Type type)
        {
            // make sure we only pick out classes
            if (!(type.IsClass))
                return;
            AddToMessage("\nclass " + type.Name);
            Attribute[] attribs = Attribute.GetCustomAttributes(type);
            if (attribs.Length == 0)
                AddToMessage("No changes to this class");
            else
                foreach (Attribute attrib in attribs)
                    WriteAttributeInfo(attrib);

            MethodInfo[] methods = type.GetMethods();
            AddToMessage("CHANGES TO METHODS OF THIS CLASS:");
            foreach (MethodInfo nextMethod in methods)
            {
                object[] attribs2 =
                    nextMethod.GetCustomAttributes(
                        typeof(LastModifiedAttribute), false);
                if (attribs2 != null)
                {
                    AddToMessage(
                        nextMethod.ReturnType + " " + nextMethod.Name + "()");
                    foreach (Attribute nextAttrib in attribs2)
                        WriteAttributeInfo(nextAttrib);
                }
            }
        }

        private static void WriteAttributeInfo(Attribute attrib)
        {
            LastModifiedAttribute lastModifiedAttrib =
                attrib as LastModifiedAttribute;
            if (lastModifiedAttrib == null)
                return;

            // check that date is in range
            DateTime modifiedDate = lastModifiedAttrib.DateModified;
            if (modifiedDate < backDateTo)
                return;

            AddToMessage("  MODIFIED: " +
                         modifiedDate.ToLongDateString() + ":");
            AddToMessage("    " + lastModifiedAttrib.Changes);
            if (lastModifiedAttrib.Issues != null)
                AddToMessage("    Outstanding issues:" +
                             lastModifiedAttrib.Issues);
        }

        private static void AddToMessage(string message)
        {
            outputText.Append("\n" + message);
        }
    }

}

using ClassLibrary;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

[assembly: SupportsWhatsNew]

namespace VectorLibrary
{
    [LastModified("14 Feb 2010", "IEnumerable interface implemented " +
                                "So Vector can now be treated as a collection")]
    [LastModified("10 Feb 2010", "IFormattable interface implemented " +
                                 "So Vector now responds to format specifiers N and VE")]
    public class Vector:IFormattable,IEnumerable
    {
         public double x, y, z;

        public Vector(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

       [LastModified("10 Feb 2010", "Method added in order to provide formatting support")]
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format == null)
                return ToString();
            string formatUpper = format.ToUpper();
            switch (formatUpper)
            {
                case "N":
                    return "|| " + Norm().ToString() + " ||";
                case "VE":
                    return String.Format("( {0:E}, {1:E}, {2:E} )", x, y, z);
                case "IJK":
                    StringBuilder sb = new StringBuilder(x.ToString(), 30);
                    sb.Append(" i + ");
                    sb.Append(y.ToString());
                    sb.Append(" j + ");
                    sb.Append(z.ToString());
                    sb.Append(" k");
                    return sb.ToString();
                default:
                    return ToString();
            }
        }

       public Vector(Vector rhs)
       {
           x = rhs.x;
           y = rhs.y;
           z = rhs.z;
       }

       [LastModified("14 Feb 2010", "Method added in order to provide collection support")]
       public IEnumerator GetEnumerator()
       {
           return new VectorEnumerator(this);
       }

       public override string ToString()
       {
           return "( " + x + " , " + y + " , " + z + " )";
       }

       public double this[uint i]
       {
           get
           {
               switch (i)
               {
                   case 0:
                       return x;
                   case 1:
                       return y;
                   case 2:
                       return z;
                   default:
                       throw new IndexOutOfRangeException(
                           "Attempt to retrieve Vector element" + i);
               }
           }
           set
           {
               switch (i)
               {
                   case 0:
                       x = value;
                       break;
                   case 1:
                       y = value;
                       break;
                   case 2:
                       z = value;
                       break;
                   default:
                       throw new IndexOutOfRangeException(
                           "Attempt to set Vector element" + i);
               }
           }
       }

       public static bool operator ==(Vector lhs, Vector rhs)
       {
           if (System.Math.Abs(lhs.x - rhs.x) < double.Epsilon &&
               System.Math.Abs(lhs.y - rhs.y) < double.Epsilon &&
               System.Math.Abs(lhs.z - rhs.z) < double.Epsilon)
               return true;
           else
               return false;
       }

       public static bool operator !=(Vector lhs, Vector rhs)
       {
           return !(lhs == rhs);
       }

       public static Vector operator +(Vector lhs, Vector rhs)
       {
           Vector result = new Vector(lhs);
           result.x += rhs.x;
           result.y += rhs.y;
           result.z += rhs.z;
           return result;
       }

       public static Vector operator *(double lhs, Vector rhs)
       {
           return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
       }

       public static Vector operator *(Vector lhs, double rhs)
       {
           return rhs * lhs;
       }

       public static double operator *(Vector lhs, Vector rhs)
       {
           return lhs.x * rhs.x + lhs.y + rhs.y + lhs.z * rhs.z;
       }

       public double Norm()
       {
           return x * x + y * y + z * z;
       }

       [LastModified("14 Feb 2010", "Class created as part of collection support for Vector")]
       private class VectorEnumerator : IEnumerator
       {
           readonly Vector _theVector;      // Vector object that this enumerato refers to 
           int _location;   // which element of _theVector the enumerator is currently referring to 

           public VectorEnumerator(Vector theVector)
           {
               _theVector = theVector;
               _location = -1;
           }

           public bool MoveNext()
           {
               ++_location;
               return (_location > 2) ? false : true;
           }

           public object Current
           {
               get
               {
                   if (_location < 0 || _location > 2)
                       throw new InvalidOperationException(
                           "The enumerator is either before the first element or " +
                           "after the last element of the Vector");
                   return _theVector[(uint)_location];
               }
           }

           public void Reset()
           {
               _location = -1;
           }

       }
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,
        AllowMultiple=true,
        Inherited=false)]
    public class LastModifiedAttribute:Attribute
    {
        private readonly DateTime _dateModified;

        public DateTime DateModified
        {
            get { return _dateModified; }
        }

        private readonly string _changes;

        public string Changes
        {
            get { return _changes; }
        }

        public string Issues { get; set; }

        public LastModifiedAttribute(string dateModified, string changes)
        {
            _dateModified = DateTime.Parse(dateModified);
            _changes = changes;
        }

    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    [AttributeUsage(AttributeTargets.Assembly)]
    public class SupportsWhatsNewAttribute:Attribute
    {
    }
}

C# 特性的使用的更多相关文章

  1. Fis3的前端工程化之路[三大特性篇之声明依赖]

    Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...

  2. Fis3的前端工程化之路[三大特性篇之资源定位]

    Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...

  3. Fis3的前端工程化之路[三大特性篇之内容嵌入]

    Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...

  4. .NET 4.6.2正式发布带来众多特性

    虽然大多数人的注意力都集中在.NET Core上,但与原来的.NET Framework相关的工作还在继续..NET Framework 4.6.2正式版已于近日发布,其重点是安全和WinForms/ ...

  5. SQL Server 2014 新特性——内存数据库

    SQL Server 2014 新特性——内存数据库 目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 ...

  6. 探索ASP.NET MVC5系列之~~~4.模型篇---包含模型常用特性和过度提交防御

    其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...

  7. InnoDB关键特性学习笔记

    插入缓存 Insert Buffer Insert Buffer是InnoDB存储引擎关键特性中最令人激动与兴奋的一个功能.不过这个名字可能会让人认为插入缓冲是缓冲池中的一个组成部分.其实不然,Inn ...

  8. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  9. HTML5新特性有哪些,你都知道吗

    一.画布(Canvas) 画布是网页中的一块区域,可所以用JavaScript在上面绘图.下面我们来创建一个画布并在上面绘制一个坦克(后面将用HTML5做一个坦克大战游戏),代码如下: <!DO ...

  10. C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)

    #include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...

随机推荐

  1. 解决win7系统不支持16位实模式汇编程序DOS执行的问题

    这学期学习了汇编,在自己电脑上发现,win7的dos不支持16位实模式. 对编程来说,不能执行程序是致命的. 在经过网上搜集资料后,得到一种解决的方法--使用dosbox软件执行 dosbox简单说, ...

  2. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  3. B/S系统的前台和后台数据转递机制探究

    作者:朱金灿 来源:http://blog.csdn.net/clever101 说实话写这篇文章超出了我的能力范围之外(因为我并没有多少Web开发经验),我所期待的是能起一个抛砖引玉的作用--希望高 ...

  4. Method and apparatus for loading a segment register in a microprocessor capable of operating in multiple modes

    A microprocessor contains an address generation unit, including a segment block, for loading descrip ...

  5. Linux中vim编辑器莫名下方出现H的问题

    在使用vim编辑文件的时候,不知道自己是按了哪个快捷键,导致了,每次编辑文件时,下方命令行出现数字+H的格式命令,使得整个文件没法编辑,强制退出后进入不能解决问题,各种文件的编辑都不行,找不出原因,最 ...

  6. 51nod1673 树有几多愁 - 贪心策略 + 虚树 + 状压dp

    传送门 题目大意: 给一颗重新编号,叶子节点的值定义为他到根节点编号的最小值,求所有叶子节点值的乘积的最大值. 题目分析: 为什么我觉得这道题最难的是贪心啊..首先要想到 在一条链上,深度大的编号要小 ...

  7. 在线算法交互、可视化与演示及应用(caffe 网络配置文件 .prototxt 的可视化)

    0. 全集 Explained Visually 1. 图像与视觉 Image Kernels 2. 数学操作 Convolution arithmetic:卷积: 3. 神经网络与深度学习 A Ne ...

  8. Eclipse Che安装依赖

    java Java 用于运行Che的服务器和用于创建Plug-in包的SDK工具,所以需要安装Java Jdk 1.8 如果只是运行Che的话下载JRE就足够了,但是加入你需要从源代码编译的话你还需要 ...

  9. droid开发中监听器的三种实现方法(OnClickListener)

    Android开发中监听器的实现有三种方法,对于初学者来说,能够很好地理解这三种方法,将能更好地增进自己对android中监听器的理解. 一.什么是监听器. 监听器是一个存在于View类下的接口,一般 ...

  10. IOS开源项目指标

    https://github.com/edagarli/open-ios-projects/blob/master/README.md 版权声明:本文博主原创文章,博客,未经同意不得转载.