comparison Source/TypeBuilder/Builders/BuildContext.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using System.Reflection.Emit;
5 using System.Diagnostics;
6
7 using BLToolkit.Reflection;
8 using BLToolkit.Reflection.Emit;
9
10 namespace BLToolkit.TypeBuilder.Builders
11 {
12 [DebuggerStepThrough]
13 public class BuildContext
14 {
15 public BuildContext(Type type)
16 {
17 Type = type;
18 Items = new Dictionary<object,object>(10);
19 }
20
21 public TypeHelper Type { get; private set; }
22 public AssemblyBuilderHelper AssemblyBuilder { get; set; }
23 public TypeBuilderHelper TypeBuilder { get; set; }
24 public Dictionary<object,object> Items { get; private set; }
25
26 public T GetItem<T>(string key)
27 {
28 object value;
29 return Items.TryGetValue(key, out value) ? (T)value : default(T);
30 }
31
32 private Dictionary<PropertyInfo,FieldBuilder> _fields;
33 public Dictionary<PropertyInfo,FieldBuilder> Fields
34 {
35 get { return _fields ?? (_fields = new Dictionary<PropertyInfo, FieldBuilder>(10)); }
36 }
37
38 private IDictionary<TypeHelper,IAbstractTypeBuilder> _interfaceMap;
39 public IDictionary<TypeHelper,IAbstractTypeBuilder> InterfaceMap
40 {
41 get { return _interfaceMap ?? (_interfaceMap = new Dictionary<TypeHelper,IAbstractTypeBuilder>()); }
42 }
43
44 public TypeHelper CurrentInterface { get; set; }
45 public MethodBuilderHelper MethodBuilder { get; set; }
46 public LocalBuilder ReturnValue { get; set; }
47 public LocalBuilder Exception { get; set; }
48 public Label ReturnLabel { get; set; }
49
50 #region BuildElement
51
52 public BuildElement BuildElement { get; set; }
53
54 public bool IsAbstractGetter { get { return BuildElement == BuildElement.AbstractGetter; } }
55 public bool IsAbstractSetter { get { return BuildElement == BuildElement.AbstractSetter; } }
56 public bool IsAbstractProperty { get { return IsAbstractGetter || IsAbstractSetter; } }
57 public bool IsAbstractMethod { get { return BuildElement == BuildElement.AbstractMethod; } }
58 public bool IsVirtualGetter { get { return BuildElement == BuildElement.VirtualGetter; } }
59 public bool IsVirtualSetter { get { return BuildElement == BuildElement.VirtualSetter; } }
60 public bool IsVirtualProperty { get { return IsVirtualGetter || IsVirtualSetter; } }
61 public bool IsVirtualMethod { get { return BuildElement == BuildElement.VirtualMethod; } }
62 public bool IsGetter { get { return IsAbstractGetter || IsVirtualGetter; } }
63 public bool IsSetter { get { return IsAbstractSetter || IsVirtualSetter; } }
64 public bool IsProperty { get { return IsGetter || IsSetter; } }
65 public bool IsMethod { get { return IsAbstractMethod || IsVirtualMethod; } }
66 public bool IsMethodOrProperty { get { return IsMethod || IsProperty; } }
67
68 #endregion
69
70 #region BuildStep
71
72 public BuildStep Step { get; set; }
73
74 public bool IsBeginStep { get { return Step == BuildStep.Begin; } }
75 public bool IsBeforeStep { get { return Step == BuildStep.Before; } }
76 public bool IsBuildStep { get { return Step == BuildStep.Build; } }
77 public bool IsAfterStep { get { return Step == BuildStep.After; } }
78 public bool IsCatchStep { get { return Step == BuildStep.Catch; } }
79 public bool IsFinallyStep { get { return Step == BuildStep.Finally; } }
80 public bool IsEndStep { get { return Step == BuildStep.End; } }
81
82 public bool IsBeforeOrBuildStep
83 {
84 get { return Step == BuildStep.Before || Step == BuildStep.Build; }
85 }
86
87 #endregion
88
89 public AbstractTypeBuilderList TypeBuilders { get; set; }
90 public PropertyInfo CurrentProperty { get; set; }
91 public MethodInfo CurrentMethod { get; set; }
92
93 #region Internal Methods
94
95 public FieldBuilder GetField(string fieldName)
96 {
97 return GetItem<FieldBuilder>("$BLToolkit.Field." + fieldName);
98 }
99
100 public FieldBuilder CreateField(string fieldName, Type type, FieldAttributes attributes)
101 {
102 var field = TypeBuilder.DefineField(fieldName, type, attributes);
103
104 field.SetCustomAttribute(MethodBuilder.Type.Assembly.BLToolkitAttribute);
105
106 Items.Add("$BLToolkit.Field." + fieldName, field);
107
108 return field;
109 }
110
111 public FieldBuilder CreatePrivateField(string fieldName, Type type)
112 {
113 return CreateField(fieldName, type, FieldAttributes.Private);
114 }
115
116 public FieldBuilder CreatePrivateField(PropertyInfo propertyInfo, string fieldName, Type type)
117 {
118 var field = CreateField(fieldName, type, FieldAttributes.Private);
119
120 if (propertyInfo != null)
121 Fields.Add(propertyInfo, field);
122
123 return field;
124 }
125
126 public FieldBuilder CreatePrivateStaticField(string fieldName, Type type)
127 {
128 return CreateField(fieldName, type, FieldAttributes.Private | FieldAttributes.Static);
129 }
130
131 public MethodBuilderHelper GetFieldInstanceEnsurer(string fieldName)
132 {
133 return GetItem<MethodBuilderHelper>("$BLToolkit.FieldInstanceEnsurer." + fieldName);
134 }
135
136 #endregion
137 }
138 }