查看PostSharp。它是一个 AOP 框架,用于典型的问题“这个代码模式我一天做几百次,我怎样才能自动化它?”。您可以使用 PostSharp 进行简化(例如):
public Class1 DoSomething( Class2 first, string text, decimal number ) {
if ( null == first ) { throw new ArgumentNullException( "first" ); }
if ( string.IsNullOrEmpty( text ) ) { throw new ArgumentException( "Must be not null and longer than 0.", "text" ) ; }
if ( number < 15.7m || number > 76.57m ) { throw new OutOfRangeArgumentException( "Minimum is 15.7 and maximum 76.57.", "number"); }
return new Class1( first.GetSomething( text ), number + text.Lenght );
}
到
public Class1 DoSomething( [NotNull]Class2 first, [NotNullOrEmpty]string text, [InRange( 15.7, 76.57 )]decimal number ) {
return new Class1( first.GetSomething( text ), number + text.Lenght );
}
但这并不是全部!:)