comparison Source/TypeBuilder/InstanceTypeAttribute.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.Diagnostics.CodeAnalysis;
3
4 using BLToolkit.Reflection;
5
6 namespace BLToolkit.TypeBuilder
7 {
8 ///<summary>
9 /// Specifies a value holder type.
10 ///</summary>
11 [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes")]
12 [AttributeUsage(AttributeTargets.Property)]
13 public class InstanceTypeAttribute : Builders.AbstractTypeBuilderAttribute
14 {
15 ///<summary>
16 /// Initializes a new instance of the InstanceTypeAttribute class.
17 ///</summary>
18 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
19 public InstanceTypeAttribute(Type instanceType)
20 {
21 _instanceType = instanceType;
22 }
23
24 ///<summary>
25 /// Initializes a new instance of the InstanceTypeAttribute class.
26 ///</summary>
27 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
28 ///<param name="parameter1">An additional parameter.</param>
29 ///<seealso cref="Parameters"/>
30 public InstanceTypeAttribute(Type instanceType, object parameter1)
31 {
32 _instanceType = instanceType;
33 SetParameters(parameter1);
34 }
35
36 ///<summary>
37 /// Initializes a new instance of the InstanceTypeAttribute class.
38 ///</summary>
39 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
40 ///<param name="parameter1">An additional parameter.</param>
41 ///<param name="parameter2">An additional parameter.</param>
42 ///<seealso cref="Parameters"/>
43 public InstanceTypeAttribute(Type instanceType,
44 object parameter1,
45 object parameter2)
46 {
47 _instanceType = instanceType;
48 SetParameters(parameter1, parameter2);
49 }
50
51 ///<summary>
52 /// Initializes a new instance of the InstanceTypeAttribute class.
53 ///</summary>
54 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
55 ///<param name="parameter1">An additional parameter.</param>
56 ///<param name="parameter2">An additional parameter.</param>
57 ///<param name="parameter3">An additional parameter.</param>
58 ///<seealso cref="Parameters"/>
59 public InstanceTypeAttribute(Type instanceType,
60 object parameter1,
61 object parameter2,
62 object parameter3)
63 {
64 _instanceType = instanceType;
65 SetParameters(parameter1, parameter2, parameter3);
66 }
67
68 ///<summary>
69 /// Initializes a new instance of the InstanceTypeAttribute class.
70 ///</summary>
71 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
72 ///<param name="parameter1">An additional parameter.</param>
73 ///<param name="parameter2">An additional parameter.</param>
74 ///<param name="parameter3">An additional parameter.</param>
75 ///<param name="parameter4">An additional parameter.</param>
76 ///<seealso cref="Parameters"/>
77 public InstanceTypeAttribute(Type instanceType,
78 object parameter1,
79 object parameter2,
80 object parameter3,
81 object parameter4)
82 {
83 _instanceType = instanceType;
84 SetParameters(parameter1, parameter2, parameter3, parameter4);
85 }
86
87 ///<summary>
88 /// Initializes a new instance of the InstanceTypeAttribute class.
89 ///</summary>
90 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
91 ///<param name="parameter1">An additional parameter.</param>
92 ///<param name="parameter2">An additional parameter.</param>
93 ///<param name="parameter3">An additional parameter.</param>
94 ///<param name="parameter4">An additional parameter.</param>
95 ///<param name="parameter5">An additional parameter.</param>
96 ///<seealso cref="Parameters"/>
97 public InstanceTypeAttribute(Type instanceType,
98 object parameter1,
99 object parameter2,
100 object parameter3,
101 object parameter4,
102 object parameter5)
103 {
104 _instanceType = instanceType;
105 SetParameters(parameter1, parameter2, parameter3, parameter4, parameter5);
106 }
107
108 ///<summary>
109 /// Initializes a new instance of the InstanceTypeAttribute class.
110 ///</summary>
111 ///<param name="instanceType">The <see cref="System.Type"/> of an instance.</param>
112 ///<param name="parameter1">An additional parameter.</param>
113 ///<param name="parameters">More additional parameters.</param>
114 ///<seealso cref="Parameters"/>
115 public InstanceTypeAttribute(Type instanceType, object parameter1, params object[] parameters)
116 {
117 _instanceType = instanceType;
118
119 // Note: we can not use something like
120 // public InstanceTypeAttribute(Type instanceType, params object[] parameters)
121 // because [InstanceType(typeof(Foo), new object[] {1,2,3})] will be treated as
122 // [InstanceType(typeof(Foo), 1, 2, 3)] so it will be not possible to specify
123 // an instance type with array as the type of the one and only parameter.
124 // An extra parameter of type object made it successul.
125
126 int len = parameters.Length;
127 Array.Resize(ref parameters, len + 1);
128 Array.ConstrainedCopy(parameters, 0, parameters, 1, len);
129 parameters[0] = parameter1;
130
131 SetParameters(parameters);
132 }
133
134 protected void SetParameters(params object[] parameters)
135 {
136 _parameters = parameters;
137 }
138
139 private object[] _parameters;
140 ///<summary>
141 /// Any additional parameters passed to a value holder constructor
142 /// with <see cref="InitContext"/> parameter.
143 ///</summary>
144 public object[] Parameters
145 {
146 get { return _parameters; }
147 }
148
149 private readonly Type _instanceType;
150 protected Type InstanceType
151 {
152 get { return _instanceType; }
153 }
154
155 private bool _isObjectHolder;
156 ///<summary>
157 /// False (default value) for holders for scalar types,
158 /// true for holders for complex objects.
159 ///</summary>
160 public bool IsObjectHolder
161 {
162 get { return _isObjectHolder; }
163 set { _isObjectHolder = value; }
164 }
165
166 private Builders.IAbstractTypeBuilder _typeBuilder;
167 ///<summary>
168 /// An <see cref="Builders.IAbstractTypeBuilder"/> required for this attribute
169 /// to build an abstract type inheritor.
170 ///</summary>
171 public override Builders.IAbstractTypeBuilder TypeBuilder
172 {
173 get
174 {
175 return _typeBuilder ?? (_typeBuilder =
176 new Builders.InstanceTypeBuilder(_instanceType, _isObjectHolder));
177 }
178 }
179 }
180 }