Mercurial > pub > bltoolkit
diff Source/Aspects/Builders/NotNullAspectBuilder.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Source/Aspects/Builders/NotNullAspectBuilder.cs Thu Mar 27 21:46:09 2014 +0400 @@ -0,0 +1,72 @@ +using System; +using System.Reflection; +using System.Reflection.Emit; + +using BLToolkit.Reflection.Emit; +using BLToolkit.TypeBuilder.Builders; + +namespace BLToolkit.Aspects.Builders +{ + public class NotNullAspectBuilder : AbstractTypeBuilderBase + { + public NotNullAspectBuilder(string message) + { + _message = message; + } + + private readonly string _message; + + public override int GetPriority(BuildContext context) + { + return TypeBuilderConsts.Priority.NotNullAspect; + } + + public override bool IsApplied(BuildContext context, AbstractTypeBuilderList builders) + { + if (context == null) throw new ArgumentNullException("context"); + + return context.IsBeforeStep && context.BuildElement != BuildElement.Type; + } + + public override void Build(BuildContext context) + { + if (context == null) throw new ArgumentNullException("context"); + + ParameterInfo pi = (ParameterInfo)TargetElement; + + if (pi.ParameterType.IsValueType == false) + { + EmitHelper emit = context.MethodBuilder.Emitter; + Label label = emit.DefineLabel(); + + string message = _message != null? string.Format(_message, pi.Name): string.Empty; + + emit + .ldarg (pi) + .brtrue_s (label) + ; + + if (string.IsNullOrEmpty(message)) + { + emit + .ldstr (pi.Name) + .newobj (typeof(ArgumentNullException), typeof(string)) + ; + } + else + { + emit + .ldnull + .ldstr (message) + .newobj (typeof(ArgumentNullException), typeof(string), typeof(string)) + ; + } + + emit + .@throw + .MarkLabel (label) + ; + } + } + } +}