C#中通过反射查看程序集的特性信息

1.特性允许把定制的元数据与程序元素联系起来,这些元数据是在编译时创建的,并嵌入到程序中;
2.在运行期间可以使用反射的一些功能检查这些元数据。
3.实例:
  WhatsNewAttribute.dll:定义特性,  编译:csc /t:library WhatsNewAttributes.cs;
  VectorClass.dll:应用特性,编译:csc /reference:WhatsNewAttributes.dll VectorClass.cs;
  LookUpWhatsNew.exe:编译:csc /reference:WhatsNewAttributes.dll /reference:VectorClass.dll LookUpWhatsNew.cs。

WhatsNewAttribute.cs:

  1. using System;   
  2.   
  3. namespace Magci.Test.Reflection   
  4. {   
  5.     //定义LastModified特性类   
  6.     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]   
  7.     public class LastModifiedAttribute : Attribute   
  8.     {   
  9.         private DateTime dateModified;   
  10.         private string changes;   
  11.         private string issues;   
  12.   
  13.         public LastModifiedAttribute(string dateModified, string changes)   
  14.         {   
  15.             this.dateModified = DateTime.Parse(dateModified);   
  16.             this.changes = changes;   
  17.         }   
  18.   
  19.         public DateTime DateModified   
  20.         {   
  21.             get  
  22.             {   
  23.                 return dateModified;   
  24.             }   
  25.         }   
  26.   
  27.         public string Changes   
  28.         {   
  29.             get  
  30.             {   
  31.                 return changes;   
  32.             }   
  33.         }   
  34.   
  35.         public string Issues   
  36.         {   
  37.             get  
  38.             {   
  39.                 return issues;   
  40.             }   
  41.             set  
  42.             {   
  43.                 issues = value;   
  44.             }   
  45.         }   
  46.     }   
  47.   
  48.     //定义SupportsWhatsNew特性类   
  49.     [AttributeUsage(AttributeTargets.Assembly)]   
  50.     public class SupportsWhatsNewAttribute : Attribute   
  51.     {   
  52.     }   
  53. }  



VectorClass.cs:

  1. using System;   
  2. using System.Text;   
  3. using System.Collections;   
  4. using Magci.Test.Reflection;   
  5.   
  6. //使用SupportsWhatsNew特性标记   
  7. [assembly:SupportsWhatsNew]   
  8.   
  9. namespace Magci.Test.Reflection   
  10. {   
  11.     //添加LastModified特性   
  12.     [LastModified("2009-01-12""IEnumerable interface implemented. So Vector can now be treated as a collection")]   
  13.     [LastModified("2009-01-08""IFormattable interface implemented. So Vector now responds to format specifiers N and VE")]   
  14.     public class Vector : IFormattable, IEnumerable   
  15.     {   
  16.         public double x, y, z;   
  17.   
  18.         public Vector(double x, double y, double z)   
  19.         {   
  20.             this.x = x;   
  21.             this.y = y;   
  22.             this.z = z;   
  23.         }   
  24.            
  25.         [LastModified("2009-01-08""Method added in order to provide formatting support")]   
  26.         public string ToString(string format, IFormatProvider formatProvider)   
  27.         {   
  28.             if (format == null)   
  29.             {   
  30.                 return ToString();   
  31.             }   
  32.             string formatUpper = format.ToUpper();   
  33.             switch (formatUpper)   
  34.             {   
  35.                 case "N":   
  36.                     return "||" + (x*x + y*y + z*z) + "||";   
  37.                 case "VE":   
  38.                     return String.Format("{0:E},{1:E},{2:E}", x, y, z);   
  39.                 case "IJK":   
  40.                     StringBuilder sb = new StringBuilder(x.ToString(), 30);   
  41.                     sb.Append("i+");   
  42.                     sb.Append(y.ToString());   
  43.                     sb.Append("j+");   
  44.                     sb.Append(z.ToString());   
  45.                     sb.Append("k");   
  46.                     return sb.ToString();   
  47.                 default:   
  48.                     return ToString();   
  49.             }   
  50.         }   
  51.   
  52.         public IEnumerator GetEnumerator()   
  53.         {   
  54.             return new VectorEnumerator(this);   
  55.         }   
  56.   
  57.         public override string ToString()   
  58.         {   
  59.             return "(" + x + "," + y + "," + z + ")";   
  60.         }   
  61.   
  62.         public double this [uint i]   
  63.         {   
  64.             get  
  65.             {   
  66.                 switch (i)   
  67.                 {   
  68.                     case 0:   
  69.                         return x;   
  70.                     case 1:   
  71.                         return y;   
  72.                     case 2:   
  73.                         return z;   
  74.                     default:   
  75.                         throw new IndexOutOfRangeException("Vector Only have three emlement!");   
  76.                 }   
  77.             }   
  78.   
  79.             set  
  80.             {   
  81.                 switch (i)   
  82.                 {   
  83.                     case 0:   
  84.                         x = value;   
  85.                         break;   
  86.                     case 1:   
  87.                         y = value;   
  88.                         break;   
  89.                     case 2:   
  90.                         z = value;   
  91.                         break;   
  92.                     default:   
  93.                         throw new IndexOutOfRangeException("Vector Only have three emlement!");   
  94.                 }   
  95.             }   
  96.         }   
  97.   
  98.     }   
  99.   
  100.     [LastModified("2009-01-12""Class created as part of collection support for Vector")]   
  101.     public class VectorEnumerator : IEnumerator   
  102.     {   
  103.         Vector vector;   
  104.         int location;   
  105.   
  106.         public VectorEnumerator(Vector vector)   
  107.         {   
  108.             this.vector = vector;   
  109.             location = -1;   
  110.         }   
  111.            
  112.         public bool MoveNext()   
  113.         {   
  114.             ++location;   
  115.             return (location > 2) ? false : true;   
  116.         }   
  117.   
  118.         public object Current   
  119.         {   
  120.             get  
  121.             {   
  122.                 if (location < 0 || location > 2)   
  123.                 {   
  124.                     throw new InvalidOperationException("Vector only have three components!");   
  125.                 }   
  126.                 return vector[(uint)location];   
  127.             }   
  128.         }   
  129.   
  130.         public void Reset()   
  131.         {   
  132.             location = -1;   
  133.         }   
  134.     }   
  135. }  



LookUpWhatsNew.cs:

  1. using System;   
  2. using System.Reflection;   
  3. using System.Text;   
  4. using System.Windows.Forms;   
  5.   
  6. namespace Magci.Test.Reflection   
  7. {   
  8.     public class WhatsNewChecker   
  9.     {   
  10.         public static StringBuilder OutputText = new StringBuilder(1000);   
  11.         //查询起始时间   
  12.         public static DateTime BackDateTo = new DateTime(2009, 1, 1);   
  13.   
  14.         public static void Main()   
  15.         {   
  16.             //加载程序集   
  17.             Assembly theAssembly = Assembly.Load("VectorClass");   
  18.             //验证是否应用了SupportsWhatsNewAttribute特性   
  19.             Attribute supportsAttribute = Attribute.GetCustomAttribute(theAssembly, typeof(SupportsWhatsNewAttribute));   
  20.   
  21.             string Name = theAssembly.FullName;   
  22.             OutputText.Append("/nAssembly:" + Name);   
  23.             if (supportsAttribute == null)   
  24.             {   
  25.                 OutputText.Append("/nThis assembly does not support WhatsNew attributes");   
  26.                 return;   
  27.             }   
  28.             else  
  29.             {   
  30.                    
  31.                 OutputText.Append("/nDefined Types:");   
  32.             }   
  33.                
  34.             //取得程序集中定义的所有类型   
  35.             Type[] types = theAssembly.GetTypes();   
  36.             //迭代显示所有类型信息   
  37.             foreach (Type type in types)   
  38.             {   
  39.                 DisplayTypeInfo(theAssembly, type);   
  40.             }   
  41.                
  42.             //通过消息框显示该程序集信息   
  43.             MessageBox.Show(OutputText.ToString(), "What/'s New since " + BackDateTo.ToLongDateString());   
  44.         }   
  45.   
  46.         //显示类型信息   
  47.         public static void DisplayTypeInfo(Assembly theAssembly, Type type)   
  48.         {   
  49.             //只考虑类的情况   
  50.             if (!(type.IsClass))   
  51.             {   
  52.                 return;   
  53.             }   
  54.             OutputText.Append("/nclass " + type.Name);   
  55.             //获取程序集的特性   
  56.             Attribute[] attributes = Attribute.GetCustomAttributes(type);   
  57.             if (attributes.Length == 0)   
  58.             {   
  59.                 OutputText.Append("/nNo changes to this class");   
  60.             }   
  61.             else  
  62.             {   
  63.                 //迭代显示类特性信息   
  64.                 foreach (Attribute attribute in attributes)   
  65.                 {   
  66.                     WriteAttributeInfo(attribute);   
  67.                 }   
  68.             }   
  69.        
  70.             //取得程序集中所有公共方法   
  71.             MethodInfo[] methods = type.GetMethods();   
  72.             OutputText.Append("/nChanges to method of this class:");   
  73.             //迭代显示所有方法信息   
  74.             foreach (MethodInfo method in methods)   
  75.             {   
  76.                 object[] attributes2 = method.GetCustomAttributes(typeof(LastModifiedAttribute), false);   
  77.                 if (attributes2 != null)   
  78.                 {   
  79.                     OutputText.Append("/n" + method.ReturnType + " " + method.Name + "()");   
  80.                     //迭代显示方法特性信息   
  81.                     foreach (Attribute attribute in attributes2)   
  82.                     {   
  83.                         WriteAttributeInfo(attribute);   
  84.                     }   
  85.                 }   
  86.             }   
  87.         }   
  88.   
  89.         public static void WriteAttributeInfo(Attribute attribute)   
  90.         {   
  91.             LastModifiedAttribute lastModifiedAttribute = attribute as LastModifiedAttribute;   
  92.             if (lastModifiedAttribute == null)   
  93.             {   
  94.                 return;   
  95.             }   
  96.             DateTime modifiedDate = lastModifiedAttribute.DateModified;   
  97.             if (modifiedDate < BackDateTo)   
  98.             {   
  99.                 return;   
  100.             }   
  101.   
  102.             OutputText.Append("/nModified: " + modifiedDate.ToLongDateString() + ":");   
  103.             OutputText.Append("/n   " + lastModifiedAttribute.Changes);   
  104.             if (lastModifiedAttribute.Issues != null)   
  105.             {   
  106.                 OutputText.Append("/n  Outstanding issues:" + lastModifiedAttribute.Issues);   
  107.             }   
  108.         }   
  109.     }   
  110. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值