0
|
1 using System;
|
|
2 using System.Reflection;
|
|
3 using System.Reflection.Emit;
|
|
4
|
|
5 using BLToolkit.Reflection.Emit;
|
|
6 using BLToolkit.TypeBuilder.Builders;
|
|
7
|
|
8 namespace BLToolkit.Aspects.Builders
|
|
9 {
|
|
10 public class NotNullAspectBuilder : AbstractTypeBuilderBase
|
|
11 {
|
|
12 public NotNullAspectBuilder(string message)
|
|
13 {
|
|
14 _message = message;
|
|
15 }
|
|
16
|
|
17 private readonly string _message;
|
|
18
|
|
19 public override int GetPriority(BuildContext context)
|
|
20 {
|
|
21 return TypeBuilderConsts.Priority.NotNullAspect;
|
|
22 }
|
|
23
|
|
24 public override bool IsApplied(BuildContext context, AbstractTypeBuilderList builders)
|
|
25 {
|
|
26 if (context == null) throw new ArgumentNullException("context");
|
|
27
|
|
28 return context.IsBeforeStep && context.BuildElement != BuildElement.Type;
|
|
29 }
|
|
30
|
|
31 public override void Build(BuildContext context)
|
|
32 {
|
|
33 if (context == null) throw new ArgumentNullException("context");
|
|
34
|
|
35 ParameterInfo pi = (ParameterInfo)TargetElement;
|
|
36
|
|
37 if (pi.ParameterType.IsValueType == false)
|
|
38 {
|
|
39 EmitHelper emit = context.MethodBuilder.Emitter;
|
|
40 Label label = emit.DefineLabel();
|
|
41
|
|
42 string message = _message != null? string.Format(_message, pi.Name): string.Empty;
|
|
43
|
|
44 emit
|
|
45 .ldarg (pi)
|
|
46 .brtrue_s (label)
|
|
47 ;
|
|
48
|
|
49 if (string.IsNullOrEmpty(message))
|
|
50 {
|
|
51 emit
|
|
52 .ldstr (pi.Name)
|
|
53 .newobj (typeof(ArgumentNullException), typeof(string))
|
|
54 ;
|
|
55 }
|
|
56 else
|
|
57 {
|
|
58 emit
|
|
59 .ldnull
|
|
60 .ldstr (message)
|
|
61 .newobj (typeof(ArgumentNullException), typeof(string), typeof(string))
|
|
62 ;
|
|
63 }
|
|
64
|
|
65 emit
|
|
66 .@throw
|
|
67 .MarkLabel (label)
|
|
68 ;
|
|
69 }
|
|
70 }
|
|
71 }
|
|
72 }
|