扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:David Tansey 来源:论坛整理 2007年11月11日
关键字:
using System ; using MyAttributeClasses ; namespace SomeClassesToTest { [DefectTrack( "1377", "12/15/02", "David Tansey" ) ] [DefectTrack( "1363", "12/12/02", "Toni Feltman", Origin = "Coding: Unhandled Exception" ) ] public class SomeCustomPricingClass { public double GetAdjustedPrice( double tnPrice, double tnPctAdjust ) { return tnPrice + ( tnPrice * tnPctAdjust ) ; } [DefectTrack( "1351", "12/10/02", "David Tansey", Origin = "Specification: Missing Requirement", FixComment = "Added PriceIsValid( ) function" ) ] public bool PriceIsValid( double tnPrice ) { return tnPrice > 0.00 && tnPrice < 1000.00 ; } } [DefectTrack( "NEW", "12/12/02", "Mike Feltman" ) ] public class AnotherCustomClass { string cMyMessageString ; public AnotherCustomClass( ){ } [DefectTrack( "1399", "12/17/02", "David Tansey", Origin = "Analysis: Missing Requirement" ) ] public void SetMessage( string lcMessageString ) { this.cMyMessageString = lcMessageString ; } } } |
using MyAttributeClasses ; |
[DefectTrack( "1377", "12/15/02", "David Tansey" ) ] [DefectTrack( "1363", "12/12/02", "Toni Feltman", Origin = "Coding: Unhandled Exception" ) ] public class SomeCustomPricingClass {} |
图一:C#特性 |
using System ; using System.Reflection ; using MyAttributeClasses ; public class TestMyAttribute { public static void Main( ) { DisplayDefectTrack( "MyAttributes" ) ; Console.ReadLine(); } public static void DisplayDefectTrack( string lcAssembly ) { Assembly loAssembly = Assembly.Load( lcAssembly ) ; Type[ ] laTypes = loAssembly.GetTypes( ) ; foreach( Type loType in laTypes ) { Console.WriteLine("*======================*" ) ; Console.WriteLine( "TYPE:\t" + loType.ToString( ) ) ; Console.WriteLine( "*=====================*" ) ; object[ ] laAttributes = loType.GetCustomAttributes( typeof( DefectTrackAttribute ), false ) ; if( laAttributes.Length > 0 ) Console.WriteLine( "\nMod/Fix Log:" ) ; foreach( Attribute loAtt in laAttributes ) { DefectTrackAttribute loDefectTrack = (DefectTrackAttribute)loAtt ; Console.WriteLine( "----------------------" ) ; Console.WriteLine( "Defect ID:\t" + loDefectTrack.DefectID ) ; Console.WriteLine( "Date:\t\t" + loDefectTrack.ModificationDate ) ; Console.WriteLine( "Developer ID:\t" + loDefectTrack.DeveloperID ) ; Console.WriteLine( "Origin:\t\t" + loDefectTrack.Origin ) ; Console.WriteLine( "Comment:\n" + loDefectTrack.FixComment ) ; } MethodInfo[ ] laMethods = loType.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly ) ; if( laMethods.Length > 0 ) { Console.WriteLine( "\nMethods: " ) ; Console.WriteLine( "----------------------" ) ; } foreach( MethodInfo loMethod in laMethods ) { Console.WriteLine( "\n\t" + loMethod.ToString( ) ) ; object[ ] laMethodAttributes = loMethod.GetCustomAttributes( typeof( DefectTrackAttribute ), false ) ; if( laMethodAttributes.Length > 0 ) Console.WriteLine( "\n\t\tMod/Fix Log:" ) ; foreach( Attribute loAtt in laMethodAttributes ) { DefectTrackAttribute loDefectTrack = (DefectTrackAttribute)loAtt ; Console.WriteLine( "\t\t----------------" ) ; Console.WriteLine( "\t\tDefect ID:\t" + loDefectTrack.DefectID ) ; Console.WriteLine( "\t\tDeveloper ID:\t" + loDefectTrack.DeveloperID ) ; Console.WriteLine( "\t\tOrigin:\t\t" + loDefectTrack.Origin ) ; Console.WriteLine( "\t\tComment:\n\t\t" + loDefectTrack.FixComment ) ; } } Console.WriteLine( "\n\n" ) ; } } } |
Assembly loAssembly = Assembly.Load( lcAssembly ) ; Type[ ] laTypes = loAssembly.GetTypes( ) ; |
object[ ] laAttributes = loType.GetCustomAttributes( typeof( DefectTrackAttribute ), false ) ; |
DefectTrackAttribute loDefectTrack = (DefectTrackAttribute)loAtt ; |
MethodInfo[ ] laMethods = loType.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly ) ; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者