- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection ;
- using Com.Zhao.Des.VectorClass ;
- using Com.Zhao.Attr.WathsNewAttributes ;
- namespace Com.Zhao.App.LookUpWhatsNew
- {
- class WhatsNewChecker
- {
- /// <summary>
- /// 要输出的文本
- /// </summary>
- static StringBuilder outputText = new StringBuilder(1024) ;
- /// <summary>
- /// 查询修改日志的起始日期
- /// </summary>
- static DateTime backDateTo = new DateTime( 2008 , 2 , 15 ) ;
- static void Main( string[] args )
- {
- Assembly theAssembly = Assembly.Load("VectorClass") ;
- //验证程序集是不是加了我们的特性标记
- Attribute supportsAttr = Attribute.GetCustomAttribute( theAssembly , typeof(SupportWhatsNewAttribute) ) ;
- string name = theAssembly.FullName ;
- //程序集全名
- Console.WriteLine( "程序集全名:" + name ) ;
- if( supportsAttr == null )
- {
- Console.WriteLine( "对不起,访问的程序集并没有例用标记标识" ) ;
- }
- else
- {
- Type[] types = theAssembly.GetTypes() ;
- foreach( Type t in types )
- {
- DispalyTypeInfo( t ) ;
- }
- Console.WriteLine( outputText ) ;
- }
- Console.ReadLine() ;
- }
- static void DispalyTypeInfo( Type type )
- {
- //如果不是类,我们不考虑,比如说结构
- if( ! type.IsClass )
- {
- return ;
- }
- AddToMessages( "/n 类名: " + type.Name ) ;
- //得到类的所有特性
- Attribute[] atts = Attribute.GetCustomAttributes( type ) ;
- if( atts.Length == 0 )
- {
- AddToMessages( "对不起,没有任何特性!" ) ;
- }
- else
- {
- foreach( Attribute attrib in atts )
- {
- WriteAttributeInfo( attrib ) ;
- }
- }
- MethodInfo[] methods = type.GetMethods() ;
- AddToMessages( "------------类中修改过的方法-----------" ) ;
- foreach( MethodInfo info in methods )
- {
- object[] attr2 = info.GetCustomAttributes( typeof(LastModifiedAttribute) , false ) ;
- if( attr2 != null )
- {
- AddToMessages( info.ReturnType + " " + info.Name + "()" ) ;
- foreach( Attribute a in attr2 )
- {
- WriteAttributeInfo( a ) ;
- }
- }
- }
- }
- /// <summary>
- /// 输出特性信息
- /// </summary>
- /// <param name="attrib">要输出的特性</param>
- static void WriteAttributeInfo( Attribute attrib )
- {
- LastModifiedAttribute lastattr = attrib as LastModifiedAttribute ;
- if( lastattr == null )
- {
- return ;
- }
- else
- {
- DateTime date = lastattr.DeteModified ;
- if( date < backDateTo )
- {
- //如果特性的修改日期早于我们的查询起始日,不考虑输出
- return ;
- }
- else
- {
- AddToMessages( "修改日期: " + date.ToString() + ":" ) ;
- AddToMessages( " " + lastattr.Changes ) ;
- if( lastattr.Issues != null )
- {
- AddToMessages( " 备注: " + lastattr.Issues ) ;
- }
- }
- }
- }
- static void AddToMessages( string message )
- {
- outputText.Append( "/n" + message ) ;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Com.Zhao.Attr.WathsNewAttributes ;
- [assembly: SupportWhatsNew]
- namespace Com.Zhao.Des.VectorClass
- {
- [LastModified("14 Feb 2008", "这次修改我们改了类的结构,增加了1号功能", Issues = "重要备注。。。下次修改前请联系我")]
- [LastModified("15 Feb 2008", "这次修改我们改了类的结构,增加了2号功能", Issues = "重要备注。。。下次修改前请联系我")]
- [LastModified("16 Feb 2008", "这次修改我们改了类的结构,增加了3号功能", Issues = "重要备注。。。下次修改前请联系我")]
- [LastModified("17 Feb 2008", "这次修改我们改了类的结构,增加了4号功能", Issues = "重要备注。。。下次修改前请联系我")]
- public class Vector
- {
- public double x , y , z ;
- public Vector( double x , double y , double z )
- {
- this.x = x ;
- this.y = y ;
- this.z = z ;
- }
- [LastModified("14 Feb 2008" ,"这次修改我们改了字符串的显示,加了1号功能." , Issues = "重要备注。。。下次修改前请联系我" ) ]
- [LastModified("15 Feb 2008", "这次修改我们改了字符串的显示,加了2号功能.", Issues = "重要备注。。。下次修改前请联系我")]
- [LastModified("16 Feb 2008", "这次修改我们改了字符串的显示,加了3号功能.", Issues = "重要备注。。。下次修改前请联系我")]
- public override string ToString()
- {
- return base.ToString();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Com.Zhao.Attr.WathsNewAttributes
- {
- /// <summary>
- /// 本特性只能用于类或者方法,可以多次用到一个元素上,但是不可以继承特性
- /// </summary>
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method , AllowMultiple = true , Inherited = false )]
- public class LastModifiedAttribute :Attribute
- {
- /// <summary>
- /// 最后一次修改时间
- /// </summary>
- private DateTime deteModified;
- public DateTime DeteModified
- {
- get { return deteModified; }
- }
- /// <summary>
- /// 修改了什么内容,标记出来
- /// </summary>
- private string changes;
- public string Changes
- {
- get { return changes; }
- }
- /// <summary>
- /// 如果有什么重要备注,请指出
- /// </summary>
- private string issues;
- public string Issues
- {
- get { return issues; }
- set { issues = value; }
- }
- public LastModifiedAttribute( string date , string changes )
- {
- this.deteModified = DateTime.Parse( date ) ;
- this.changes = changes ;
- }
- }
- /// <summary>
- /// 只要以用来标记在程序集中
- /// </summary>
- [AttributeUsage(AttributeTargets.Assembly) ]
- public class SupportWhatsNewAttribute :Attribute
- {
- }
- }