postsharp

C# 中参数验证方式的演变

笑着哭i 提交于 2019-12-24 12:02:31
一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空,如果是字符可能有长度限制,如果是整数可能需要判断范围,如果是一些特殊的类型比如电话号码,邮件地址等,可能需要使用正则表达式进行判断。 通常,我们一般都是在方法开始的地方进行条件判断,然后抛出合适的异常,这是最普通和通用的做法,但是在.NET中,利用一些语言特性和类库,可以使用一些其他的方式将我们从复杂繁琐的工作中解放出来。本文逐一介绍能够用来进行参数验证的方式,他们包括直接判断语句,帮助类,扩展方法,Customer Attribute,Enterprise Liberary,Debug.Assert,Code Contract等。可以看到在.NET中随着版本的演化,逐步添加了很多声明式编程( Declarative programming )的风格,这样的代码会直接表明what而不是how,从而使得代码更加清晰和易维护。 现在来看下这些参数验证的方法。 一 一般的方法 假设我们有一个方法如下,用来进行登记注册,需要传入姓名和年龄。 public bool Register(string name, int age) { //insert into db } 当然,不是传进什么东西都能调用我们的方法。一般地

CIL: “Operation could destabilize the runtime” exception

蹲街弑〆低调 提交于 2019-12-24 07:28:07
问题 I've been playing with PostSharp a bit and I ran into a nasty problem. Following IL in Silverlight assembly: .method public hidebysig specialname newslot virtual final instance void set_AccountProfileModifiedAt(valuetype [mscorlib]System.DateTime 'value') cil managed { .maxstack 2 .locals ( [0] bool ~propertyHasChanged, [1] bool CS$4$0000) L_0000: nop L_0001: nop L_0002: ldarg.0 L_0003: call instance valuetype [mscorlib]System.DateTime Accounts.AccountOwner::get_AccountProfileModifiedAt() L

How do you intercept method calls on a base class using PostSharp?

可紊 提交于 2019-12-24 01:55:27
问题 I want to provide an implementation of System.Object.ToString to various classes using PostSharp. I've created an aspect inheriting from MethodInterceptionAspect but the OnInvoke method isn't getting invoked when a call to EchoDto.ToString takes place. How can I get OnInvoke to be called when ToString is called? [DataContract] [ImplementJsonToStringAspect()] public class EchoDto { [DataMember] public string Text { get; set; } } [Serializable] [MulticastAttributeUsage(MulticastTargets.Method)]

NuGet restoring PostSharp package before the build begins

此生再无相见时 提交于 2019-12-23 09:21:03
问题 I am using PostSharp and I have the following target description in my project file: <Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''"> <Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." /> <Error

PostSharp AssemblyLoadException Autofac

狂风中的少年 提交于 2019-12-22 13:12:02
问题 I am setting up a new solution in which I want to use the latest Autofac (3.4) and PostSharp 3.1.42. After referencing the NuGet packages I get the following error and I can't figure out what is going on. I never selected Autofac 3.3.0 package. packages.config: <package id="Autofac" version="3.4.0" targetFramework="net451" /> <package id="Autofac.Extras.Multitenant" version="3.1.1" targetFramework="net451" /> <package id="Autofac.Mvc5" version="3.3.0" targetFramework="net451" /> <package id=

PostSharp AssemblyLoadException Autofac

不羁岁月 提交于 2019-12-22 13:11:48
问题 I am setting up a new solution in which I want to use the latest Autofac (3.4) and PostSharp 3.1.42. After referencing the NuGet packages I get the following error and I can't figure out what is going on. I never selected Autofac 3.3.0 package. packages.config: <package id="Autofac" version="3.4.0" targetFramework="net451" /> <package id="Autofac.Extras.Multitenant" version="3.1.1" targetFramework="net451" /> <package id="Autofac.Mvc5" version="3.3.0" targetFramework="net451" /> <package id=

How to determine if a class is decorated with a PostSharp aspect at build or run time

烈酒焚心 提交于 2019-12-22 11:23:03
问题 I have a series of WCF services of the form [WCFEndpoint] public class MyWCFEndpoint : WCFSvcBase, IMyWCFEndpoint { } where WCFEndpoint is a PostSharp OnMethodBoundaryAspect: [Serializable] public class WCFEndpointAttribute : OnMethodBoundaryAspect { } The main purpose of [WCFEndpoint] is to provide duration information on WCF calls via overrides of OnEntry and OnExit, as well as other diagnostic information. The problem is that developers occasionally forget to add the [WCFEndpoint] to new

How to determine if a class is decorated with a PostSharp aspect at build or run time

一个人想着一个人 提交于 2019-12-22 11:21:07
问题 I have a series of WCF services of the form [WCFEndpoint] public class MyWCFEndpoint : WCFSvcBase, IMyWCFEndpoint { } where WCFEndpoint is a PostSharp OnMethodBoundaryAspect: [Serializable] public class WCFEndpointAttribute : OnMethodBoundaryAspect { } The main purpose of [WCFEndpoint] is to provide duration information on WCF calls via overrides of OnEntry and OnExit, as well as other diagnostic information. The problem is that developers occasionally forget to add the [WCFEndpoint] to new

PostSharp & classes that (are supposed to) implement INotifyPropertyChanged

北慕城南 提交于 2019-12-22 09:50:04
问题 I'm new to PostSharp and am trying to figure out whether it can make my life implementing property changed events a bit easier. However, I'm facing the first issue already. I have my interfaces in one Project library, interfaces.dll: public interface IPerson : INotifyPropertyChanged { string firstName { get; set; } } and the implementation in another one, implementations.dll : [NotifyPropertyChanged] // Postsharp attribute public class Person : IPerson { public string firstName { get; set; }

Can Spring.Net function as PostSharp?

徘徊边缘 提交于 2019-12-22 08:35:23
问题 A few months back I've discovered PostSharp, and for a time, it was good. But then legal came back with an answer saying that they don't like the licence of the old versions. Then the department told me that 2.0's price was unacceptably high (for the number of seats we need)... I was extremely disapponted, but not disheartened. Can't be the only such framework, I thought. I kept looking for a replacement, but most of it was either dead, ill maintained (especially in documentation department),