0
|
1 <#@ template language="C#v3.5" debug="True" #>
|
|
2 <#@ output extension="generated.cs" #>
|
|
3 <#@ assembly name="System.Core" #>
|
|
4 <#@ import namespace="System.Collections.Generic" #>
|
|
5 <#@ import namespace="System.Linq" #>
|
|
6 //---------------------------------------------------------------------------------------------------
|
|
7 // <auto-generated>
|
|
8 // This code was generated by BLToolkit template for T4.
|
|
9 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
|
|
10 // </auto-generated>
|
|
11 //---------------------------------------------------------------------------------------------------
|
|
12 using System;
|
|
13 using System.Data.Linq;
|
|
14 using System.Data.SqlTypes;
|
|
15 using System.Globalization;
|
|
16 using System.IO;
|
|
17 using System.Xml;
|
|
18 #if !SILVERLIGHT
|
|
19 using System.Xml.Linq;
|
|
20 #endif
|
|
21
|
|
22 namespace BLToolkit.Common
|
|
23 {
|
|
24 using Properties;
|
|
25
|
|
26 public partial class Convert
|
|
27 {
|
|
28 <#
|
|
29 GenerationEnvironment.Remove(GenerationEnvironment.Length - 2, 2);
|
|
30
|
|
31 _types =
|
|
32 Set(_ => { _.Group = "Simple"; _.Method = "To" + _.Name; }, new[]
|
|
33 {
|
|
34 new ToType { Name = "Boolean", NullValue = "Configuration.NullableValues.Boolean", Froms = { { "Char", null } } },
|
|
35 new ToType { Name = "Byte", NullValue = "Configuration.NullableValues.Byte" },
|
|
36 new ToType { Name = "Char", NullValue = "Configuration.NullableValues.Char" },
|
|
37 new ToType { Name = "DateTime", NullValue = "Configuration.NullableValues.DateTime" },
|
|
38 new ToType { Name = "DateTimeOffset", NullValue = "DateTimeOffset.MinValue" },
|
|
39 new ToType { Name = "Decimal", NullValue = "Configuration.NullableValues.Decimal", Froms = { { "Byte[]", null }, { "Binary", null } } },
|
|
40 new ToType { Name = "Double", NullValue = "Configuration.NullableValues.Double" },
|
|
41 new ToType { Name = "Guid", NullValue = "Configuration.NullableValues.Guid" },
|
|
42 new ToType { Name = "Int16", NullValue = "Configuration.NullableValues.Int16" },
|
|
43 new ToType { Name = "Int32", NullValue = "Configuration.NullableValues.Int32" },
|
|
44 new ToType { Name = "Int64", NullValue = "Configuration.NullableValues.Int64" },
|
|
45 new ToType { Name = "Single", NullValue = "Configuration.NullableValues.Single" },
|
|
46 new ToType { Name = "String", NullValue = "Configuration.NullableValues.String", FromObjectReturn = "return p.ToString();" },
|
|
47 new ToType { Name = "TimeSpan", NullValue = "Configuration.NullableValues.TimeSpan" },
|
|
48 }.Concat(Set(_ => _.NonCompliant = true, new[]
|
|
49 {
|
|
50 new ToType { Name = "SByte", NullValue = "Configuration.NullableValues.SByte" },
|
|
51 new ToType { Name = "UInt16", NullValue = "Configuration.NullableValues.UInt16" },
|
|
52 new ToType { Name = "UInt32", NullValue = "Configuration.NullableValues.UInt32" },
|
|
53 new ToType { Name = "UInt64", NullValue = "Configuration.NullableValues.UInt64" },
|
|
54 })))
|
|
55 .Concat(Set(_ => { _.Group = "Nullable"; _.Method = "ToNullable" + _.Name; _.Name += "?"; }, new[]
|
|
56 {
|
|
57 new ToType { Name = "Boolean" },
|
|
58 new ToType { Name = "Byte" },
|
|
59 new ToType { Name = "Char" },
|
|
60 new ToType { Name = "DateTime" },
|
|
61 new ToType { Name = "DateTimeOffset" },
|
|
62 new ToType { Name = "Decimal" },
|
|
63 new ToType { Name = "Double" },
|
|
64 new ToType { Name = "Guid" },
|
|
65 new ToType { Name = "Int16" },
|
|
66 new ToType { Name = "Int32" },
|
|
67 new ToType { Name = "Int64" },
|
|
68 new ToType { Name = "Single" },
|
|
69 new ToType { Name = "TimeSpan" },
|
|
70 }.Concat(Set(_ => _.NonCompliant = true, new[]
|
|
71 {
|
|
72 new ToType { Name = "SByte" },
|
|
73 new ToType { Name = "UInt16" },
|
|
74 new ToType { Name = "UInt32" },
|
|
75 new ToType { Name = "UInt64" },
|
|
76 }))))
|
|
77 .Concat(Set(_ => { _.Group = "Sql"; _.Method = "To" + _.Name; _.NonSilverlightable = true; _.NullValue = _.Name + ".Null"; }, new[]
|
|
78 {
|
|
79 new ToType { Name = "SqlString" },
|
|
80 new ToType { Name = "SqlByte" },
|
|
81 new ToType { Name = "SqlInt16" },
|
|
82 new ToType { Name = "SqlInt32" },
|
|
83 new ToType { Name = "SqlInt64" },
|
|
84 new ToType { Name = "SqlSingle" },
|
|
85 new ToType { Name = "SqlDouble" },
|
|
86 new ToType { Name = "SqlBoolean" },
|
|
87 new ToType { Name = "SqlDecimal" },
|
|
88 new ToType { Name = "SqlMoney" },
|
|
89 new ToType { Name = "SqlDateTime" },
|
|
90 new ToType { Name = "SqlGuid" },
|
|
91 new ToType { Name = "SqlBinary" },
|
|
92 new ToType { Name = "SqlBytes" },
|
|
93 new ToType { Name = "SqlChars" },
|
|
94 new ToType { Name = "SqlXml" },
|
|
95 }))
|
|
96 .Concat(Set(_ => { _.Group = "Other"; }, new[]
|
|
97 {
|
|
98 new ToType { Name = "Type", Method = "ToType", NonSLMethods = { "Guid", "Guid?", "Binary", "Byte[]" } },
|
|
99 new ToType { Name = "Stream", Method = "ToStream", NullValue = "Stream.Null" },
|
|
100 new ToType { Name = "Byte[]", Method = "ToByteArray", NonSLMethods = { "DateTime", "DateTime?", "DateTimeOffset", "DateTimeOffset?" }, Froms = { { "Decimal", null }, { "Stream", null }, { "Char[]", null } } },
|
|
101 new ToType { Name = "Char[]", Method = "ToCharArray", },
|
|
102 new ToType { Name = "Binary", Method = "ToLinqBinary", },
|
|
103 new ToType { Name = "XmlReader", Method = "ToXmlReader", NonSilverlightable = true },
|
|
104 new ToType { Name = "XmlDocument", Method = "ToXmlDocument", NonSilverlightable = true },
|
|
105 new ToType { Name = "XElement", Method = "ToXElement", NonSilverlightable = true },
|
|
106 }))
|
|
107 .ToDictionary(_ => _.Name);
|
|
108
|
|
109
|
|
110 // To Boolean
|
|
111 //
|
|
112 ConvertFrom("String", "p == null? {2} : p.Length == 1 ? ToBoolean(p[0]) : Boolean.Parse(p)", "Boolean", "Boolean?", "SqlBoolean");
|
|
113 ConvertFrom("Byte[]", "p == null || p.Length == 0 ? {2} : BitConverter.ToBoolean(p, 0)", "Boolean", "Boolean?", "SqlBoolean");
|
|
114 ConvertFrom("Binary", "p == null || p.Length == 0 ? {2} : BitConverter.ToBoolean(p.ToArray(), 0)", "Boolean", "Boolean?", "SqlBoolean");
|
|
115
|
|
116 ConvertTo ("Boolean", "p.HasValue && p.Value", "Boolean?");
|
|
117 ConvertToNull("Boolean", "p != 0", "p.HasValue && p.Value != 0", "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal");
|
|
118 ConvertTo ("Boolean", "p.HasValue && ToBoolean(p.Value)", "Char?");
|
|
119 ConvertTo ("Boolean", "!p.IsNull && p.Value", "SqlBoolean");
|
|
120 ConvertTo ("Boolean", "!p.IsNull && ToBoolean(p.Value)", Sql("String", "Byte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money"));
|
|
121
|
|
122 ConvertFrom("Boolean", "p", "Boolean?", "SqlBoolean");
|
|
123 ConvertFrom("Char", "ToBoolean(p)", "Boolean?", "SqlBoolean");
|
|
124 ConvertFrom("Char?", "p.HasValue && ToBoolean(p.Value)", "Boolean?", "SqlBoolean");
|
|
125
|
|
126 // To Boolean?
|
|
127 //
|
|
128 ConvertToNull("Boolean?", "p != 0", "p.HasValue ? p.Value != 0 : {2}", "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal");
|
|
129 ConvertTo ("Boolean?", "p.IsNull ? {2} : p.Value", "SqlBoolean");
|
|
130 ConvertTo ("Boolean?", "p.IsNull ? {2} : ToBoolean(p.Value)", Sql("String", "Byte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money"));
|
|
131
|
|
132 // SqlBoolean
|
|
133 //
|
|
134 ConvertTo ("SqlBoolean", "p.HasValue ? p.Value : {2}", "Boolean?");
|
|
135 ConvertToNull("SqlBoolean", "p != 0", "p.HasValue ? p.Value != 0 : {2}", "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal");
|
|
136 ConvertTo ("SqlBoolean", "p.IsNull ? {2} : ToBoolean(p.Value)", Sql("String", "Byte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money"));
|
|
137
|
|
138 // To Byte
|
|
139 //
|
|
140 ConvertNumber("Byte",
|
|
141 new string[] { "Byte" },
|
|
142 new string[] { "SByte", "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal", "Char" },
|
|
143 new string[] { "Byte" },
|
|
144 new string[] { "String", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
145
|
|
146 ConvertFrom("Byte[]", "p == null || p.Length == 0 ? {2} : p[0]", "Byte", "Byte?", "SqlByte");
|
|
147 ConvertFrom("Binary", "p == null || p.Length == 0 ? {2} : p.ToArray()[0]", "Byte", "Byte?", "SqlByte");
|
|
148
|
|
149 // To Char
|
|
150 //
|
|
151 ConvertNumber("Char",
|
|
152 new string[] { "Char" },
|
|
153 new string[] { "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal" },
|
|
154 new string[] { },
|
|
155 new string[] { "String", "Byte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
156
|
|
157 ConvertFrom("String", "string.IsNullOrEmpty(p)? ({0})0 : p[0]", "Char", "Char?");
|
|
158
|
|
159 // To DateTime
|
|
160 //
|
|
161 ConvertTo(new[] { "DateTime", "SqlDateTime" }, "p.HasValue ? p.Value : {2}", "DateTime?");
|
|
162 ConvertTo(new[] { "DateTime?", "SqlDateTime" }, "p", "DateTime");
|
|
163 ConvertTo(new[] { "DateTime", "DateTime?" }, "p.IsNull ? {2} : p.Value", "SqlDateTime");
|
|
164
|
|
165 ConvertFrom("String", "p == null ? {2} : {3}.Parse(p, null, DateTimeStyles.NoCurrentDateDefault)", "DateTime", "DateTime?", "SqlDateTime");
|
|
166 ConvertFrom("TimeSpan", "{3}.MinValue + p", "DateTime", "DateTime?", "SqlDateTime");
|
|
167 ConvertFrom("Int64", "{3}.MinValue + TimeSpan.FromTicks(p)", "DateTime", "DateTime?", "SqlDateTime");
|
|
168 ConvertFrom("Double", "{3}.MinValue + TimeSpan.FromDays (p)", "DateTime", "DateTime?", "SqlDateTime");
|
|
169 ConvertFrom("DateTimeOffset", "p.LocalDateTime", "DateTime", "DateTime?", "SqlDateTime");
|
|
170 ConvertFrom("DateTimeOffset?", "p.HasValue ? p.Value.LocalDateTime : {2}", "DateTime", "DateTime?", "SqlDateTime");
|
|
171 ConvertFrom("TimeSpan?", "p.HasValue ? {3}.MinValue + p.Value : {2}", "DateTime", "DateTime?", "SqlDateTime");
|
|
172 ConvertFrom("Int64?", "p.HasValue ? {3}.MinValue + TimeSpan.FromTicks(p.Value): {2}", "DateTime", "DateTime?", "SqlDateTime");
|
|
173 ConvertFrom("Double?", "p.HasValue ? {3}.MinValue + TimeSpan.FromDays (p.Value): {2}", "DateTime", "DateTime?", "SqlDateTime");
|
|
174 ConvertFrom("SqlDateTime", "p.IsNull ? {2} : p.Value", "DateTime", "DateTime?", "SqlDateTime");
|
|
175 ConvertFrom("SqlString", "p.IsNull ? {2} : To{3}(p.Value)", "DateTime", "DateTime?", "SqlDateTime");
|
|
176 ConvertFrom("SqlInt64", "p.IsNull ? {2} : {3}.MinValue + TimeSpan.FromTicks(p.Value)", "DateTime", "DateTime?", "SqlDateTime");
|
|
177 ConvertFrom("SqlDouble", "p.IsNull ? {2} : {3}.MinValue + TimeSpan.FromDays (p.Value)", "DateTime", "DateTime?", "SqlDateTime");
|
|
178 ConvertFrom("Byte[]", "p == null || p.Length == 0 ? {2} : {3}.FromBinary(ToInt64(p))", "DateTime", "DateTime?", "SqlDateTime");
|
|
179 ConvertFrom("Binary", "p == null || p.Length == 0 ? {2} : {3}.FromBinary(ToInt64(p.ToArray()))", "DateTime", "DateTime?", "SqlDateTime");
|
|
180
|
|
181 // To DateTimeOffset
|
|
182 //
|
|
183 ConvertTo("DateTimeOffset", "p.HasValue ? p.Value : {2}", "DateTimeOffset?");
|
|
184 ConvertTo("DateTimeOffset?", "p", "DateTimeOffset");
|
|
185
|
|
186 ConvertFrom("TimeSpan", "{3}.MinValue + p", "DateTimeOffset", "DateTimeOffset?");
|
|
187 ConvertFrom("Int64", "{3}.MinValue + TimeSpan.FromTicks(p)", "DateTimeOffset", "DateTimeOffset?");
|
|
188 ConvertFrom("Double", "{3}.MinValue + TimeSpan.FromDays (p)", "DateTimeOffset", "DateTimeOffset?");
|
|
189 ConvertFrom("DateTime", "p", "DateTimeOffset", "DateTimeOffset?");
|
|
190 ConvertFrom("DateTime?", "p.HasValue ? p.Value : {2}", "DateTimeOffset", "DateTimeOffset?");
|
|
191 ConvertFrom("TimeSpan?", "p.HasValue ? {3}.MinValue + p.Value : {2}", "DateTimeOffset", "DateTimeOffset?");
|
|
192 ConvertFrom("Int64?", "p.HasValue ? {3}.MinValue + TimeSpan.FromTicks(p.Value): {2}", "DateTimeOffset", "DateTimeOffset?");
|
|
193 ConvertFrom("Double?", "p.HasValue ? {3}.MinValue + TimeSpan.FromDays (p.Value): {2}", "DateTimeOffset", "DateTimeOffset?");
|
|
194 ConvertFrom("SqlDateTime", "p.IsNull ? {2} : p.Value", "DateTimeOffset", "DateTimeOffset?");
|
|
195 ConvertFrom("SqlString", "p.IsNull ? {2} : ToDateTimeOffset(p.Value)", "DateTimeOffset", "DateTimeOffset?");
|
|
196 ConvertFrom("SqlInt64", "p.IsNull ? {2} : {3}.MinValue + TimeSpan.FromTicks(p.Value)", "DateTimeOffset", "DateTimeOffset?");
|
|
197 ConvertFrom("SqlDouble", "p.IsNull ? {2} : {3}.MinValue + TimeSpan.FromDays (p.Value)", "DateTimeOffset", "DateTimeOffset?");
|
|
198 ConvertFrom("Byte[]", "p == null || p.Length == 0 ? {2} : new {0}(ToDateTime(p))", "DateTimeOffset", "DateTimeOffset?");
|
|
199 ConvertFrom("Binary", "p == null || p.Length == 0 ? {2} : new {0}(ToDateTime(p.ToArray()))", "DateTimeOffset", "DateTimeOffset?");
|
|
200
|
|
201 // To Decimal
|
|
202 //
|
|
203 ConvertNumber("Decimal",
|
|
204 new string[] { "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "Char", "UInt64", "Decimal" },
|
|
205 new string[] { "Single", "Double" },
|
|
206 new string[] { "Byte", "Int16", "Int32", "Int64", "Decimal", "Money" },
|
|
207 new string[] { "String", "Single", "Double", "Boolean" });
|
|
208
|
|
209 ConvertNumber("Money",
|
|
210 new string[] { "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "Char", "UInt64", "Decimal" },
|
|
211 new string[] { "Single", "Double" },
|
|
212 new string[] { "Byte", "Int16", "Int32", "Int64", "Decimal", "Money" },
|
|
213 new string[] { "String", "Single", "Double", "Boolean" });
|
|
214
|
|
215 ConvertTo(new[] { "Decimal?", "SqlDecimal", "SqlMoney" }, "p == null || p.Length == 0 ? {2} : ToDecimal(p)", "Byte[]", "Binary");
|
|
216
|
|
217 // To Double
|
|
218 //
|
|
219 ConvertTo(new[] { "Double", "Double?", "SqlDouble" }, "(p - {1}.MinValue).TotalDays", "DateTime", "DateTimeOffset");
|
|
220 ConvertTo("SqlDouble", "p.IsNull? {0}.Null: ToDouble(p.Value)", "SqlDateTime");
|
|
221
|
|
222 ConvertNumber("Double",
|
|
223 new string[] { "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "Char", "UInt64", "Single", "Double" },
|
|
224 new string[] { "Decimal" },
|
|
225 new string[] { "Byte", "Int16", "Int32", "Int64", "Single", "Double" },
|
|
226 new string[] { "String", "Decimal", "Money", "Boolean", "DateTime" });
|
|
227
|
|
228 ConvertTo(new[] { "Double", "Double?", "SqlDouble" }, "p.HasValue ? (p.Value - {4}.MinValue).TotalDays : {2}", "DateTime?", "DateTimeOffset?");
|
|
229
|
|
230 ConvertFrom("TimeSpan", "p.TotalDays", "Double", "Double?", "SqlDouble");
|
|
231 ConvertFrom("TimeSpan?", "p.HasValue ? p.Value.TotalDays : {2}", "Double", "Double?", "SqlDouble");
|
|
232
|
|
233 // To Guid
|
|
234 //
|
|
235 ConvertTo(new[] { "Guid", "SqlGuid" }, "p.HasValue ? p.Value : {2}", "Guid?");
|
|
236 ConvertTo(new[] { "Guid?", "SqlGuid" }, "p", "Guid");
|
|
237 ConvertTo(new[] { "Guid", "Guid?" }, "p.IsNull ? {2} : p.Value", "SqlGuid");
|
|
238
|
|
239 ConvertTo(new[] { "Guid", "Guid?", "SqlGuid" }, "p == null ? {2} : new Guid(p)", "String", "Byte[]");
|
|
240
|
|
241 ConvertFrom("SqlGuid", "p.IsNull ? {2} : p.Value", "Guid", "Guid?", "SqlGuid");
|
|
242 ConvertFrom("SqlString", "p.IsNull ? {2} : new Guid(p.Value)", "Guid", "Guid?", "SqlGuid");
|
|
243 ConvertFrom("SqlBinary", "p.IsNull ? {2} : p.ToSqlGuid().Value", "Guid", "Guid?", "SqlGuid");
|
|
244 ConvertFrom("Binary", "p == null ? {2} : new Guid(p.ToArray())", "Guid", "Guid?", "SqlGuid");
|
|
245 ConvertFrom("Type", "p == null ? {2} : p.GUID", "Guid", "Guid?", "SqlGuid");
|
|
246
|
|
247 // To Int16
|
|
248 //
|
|
249 ConvertNumber("Int16",
|
|
250 new string[] { "SByte", "Int16", "Byte" },
|
|
251 new string[] { "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal", "Char" },
|
|
252 new string[] { "Byte", "Int16" },
|
|
253 new string[] { "String", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
254
|
|
255 // To Int32
|
|
256 //
|
|
257 ConvertNumber("Int32",
|
|
258 new string[] { "SByte", "Int16", "Int32", "Byte", "UInt16", "Char" },
|
|
259 new string[] { "Int64", "UInt32", "UInt64", "Single", "Double", "Decimal" },
|
|
260 new string[] { "Byte", "Int16", "Int32" },
|
|
261 new string[] { "String", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
262
|
|
263 // To Int64
|
|
264 //
|
|
265 ConvertTo("Int64", "(p - DateTime.MinValue).Ticks", "DateTime", "DateTimeOffset");
|
|
266 ConvertTo("Int64?", "(p - DateTime.MinValue).Ticks", "DateTime", "DateTimeOffset");
|
|
267 ConvertTo("SqlInt64", "(p - DateTime.MinValue).Ticks", "DateTime", "DateTimeOffset");
|
|
268 ConvertTo("SqlInt64", "p.IsNull? {0}.Null: ToInt64(p.Value)", "SqlDateTime");
|
|
269
|
|
270 ConvertNumber("Int64",
|
|
271 new string[] { "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "Char" },
|
|
272 new string[] { "UInt64", "Single", "Double", "Decimal" },
|
|
273 new string[] { "Byte", "Int16", "Int32", "Int64" },
|
|
274 new string[] { "String", "Single", "Double", "Decimal", "Money", "Boolean", "DateTime" });
|
|
275
|
|
276 ConvertTo("Int64", " p.HasValue ? (p.Value - DateTime.MinValue).Ticks : 0", "DateTime?", "DateTimeOffset?");
|
|
277 ConvertTo("Int64?", "p.HasValue ? (p.Value - DateTime.MinValue).Ticks : 0", "DateTime?", "DateTimeOffset?");
|
|
278 ConvertTo("SqlInt64", "p.HasValue ? (p.Value - DateTime.MinValue).Ticks : 0", "DateTime?", "DateTimeOffset?");
|
|
279
|
|
280 ConvertFrom("TimeSpan", "p.Ticks", "Int64", "Int64?", "SqlInt64");
|
|
281 ConvertFrom("TimeSpan?", "p.HasValue ? p.Value.Ticks : 0", "Int64", "Int64?", "SqlInt64");
|
|
282
|
|
283 // To SByte
|
|
284 //
|
|
285 ConvertNumber("SByte",
|
|
286 new string[] { "SByte" },
|
|
287 new string[] { "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64", "Single", "Double", "Decimal", "Char" },
|
|
288 new string[] { },
|
|
289 new string[] { "String", "Byte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
290
|
|
291 ConvertFrom("Byte[]", "p == null || p.Length == 0 ? {2} : checked(({0})p[0])", "SByte", "SByte?");
|
|
292 ConvertFrom("Binary", "p == null || p.Length == 0 ? {2} : checked(({0})p.ToArray()[0])", "SByte", "SByte?");
|
|
293
|
|
294 // To Single
|
|
295 //
|
|
296 ConvertNumber("Single",
|
|
297 new string[] { "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "Char", "UInt64", "Single" },
|
|
298 new string[] { "Double", "Decimal" },
|
|
299 new string[] { "Byte", "Int16", "Int32", "Int64", "Single" },
|
|
300 new string[] { "String", "Double", "Decimal", "Money", "Boolean" });
|
|
301
|
|
302 // To String
|
|
303 //
|
|
304 ConvertToNull(new[] { "String", "SqlString" },
|
|
305 "p.To{3}()",
|
|
306 "p.HasValue ? p.Value.To{3}() : {2}",
|
|
307 "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64",
|
|
308 "Single", "Double", "Boolean", "Decimal", "Char", "TimeSpan", "DateTime", "DateTimeOffset", "Guid");
|
|
309
|
|
310 ConvertTo(new[] { "String", "SqlString" }, "p == null ? {2} : p.FullName", "Type");
|
|
311 ConvertTo(new[] { "String", "SqlString" }, "p == null ? {2} : System.Text.Encoding.UTF8.GetString(p, 0, p.Length)", "Byte[]");
|
|
312 ConvertTo(new[] { "String", "SqlString" }, "To{0}(p.ToArray())", "Binary");
|
|
313
|
|
314 ConvertTo(new[] { "String", "SqlString" }, "p.To{0}()",
|
|
315 Sql("String", "Byte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean", "Guid"));
|
|
316
|
|
317 ConvertTo( "String", "p.IsNull ? {2} : p.ToSqlString().Value", "SqlChars");
|
|
318 ConvertTo(new[] { "String", "SqlString" }, "p.IsNull ? {2} : p.Value", "SqlXml");
|
|
319 ConvertTo(new[] { "String", "SqlString" }, "p == null ? {2} : p.InnerXml", "XmlDocument");
|
|
320 ConvertTo(new[] { "String", "SqlString" }, "p == null ? {2} : p.ToString()", "XElement");
|
|
321
|
|
322 ConvertTo("SqlString", "p ?? {2}", "String");
|
|
323 ConvertTo("SqlString", "p.To{0}()", "SqlChars");
|
|
324
|
|
325 // To TimeSpan
|
|
326 //
|
|
327 ConvertTo("TimeSpan", "p.HasValue ? p.Value : {2}", "TimeSpan?");
|
|
328 ConvertTo("TimeSpan?", "p", "TimeSpan");
|
|
329
|
|
330 ConvertFrom("DateTime", "p - DateTime.MinValue", "TimeSpan", "TimeSpan?");
|
|
331 ConvertFrom("DateTimeOffset", "p - DateTimeOffset.MinValue", "TimeSpan", "TimeSpan?");
|
|
332 ConvertFrom("Int64", "TimeSpan.FromTicks(p)", "TimeSpan", "TimeSpan?");
|
|
333 ConvertFrom("Double", "TimeSpan.FromDays (p)", "TimeSpan", "TimeSpan?");
|
|
334 ConvertFrom("DateTime?", "p.HasValue ? p.Value - DateTime.MinValue : {2}", "TimeSpan", "TimeSpan?");
|
|
335 ConvertFrom("DateTimeOffset?", "p.HasValue ? p.Value - DateTimeOffset.MinValue : {2}", "TimeSpan", "TimeSpan?");
|
|
336 ConvertFrom("Int64?", "p.HasValue ? {3}.FromTicks(p.Value) : {2}", "TimeSpan", "TimeSpan?");
|
|
337 ConvertFrom("Double?", "p.HasValue ? {3}.FromDays (p.Value) : {2}", "TimeSpan", "TimeSpan?");
|
|
338 ConvertFrom("SqlString", "p.IsNull ? {2} : {3}.Parse(p.Value)", "TimeSpan", "TimeSpan?");
|
|
339 ConvertFrom("SqlDateTime", "p.IsNull ? {2} : p.Value - DateTime.MinValue", "TimeSpan", "TimeSpan?");
|
|
340 ConvertFrom("SqlInt64", "p.IsNull ? {2} : {3}.FromTicks(p.Value)", "TimeSpan", "TimeSpan?");
|
|
341 ConvertFrom("SqlDouble", "p.IsNull ? {2} : {3}.FromDays(p.Value)", "TimeSpan", "TimeSpan?");
|
|
342 ConvertFrom("Byte[]", "p == null || p.Length == 0? {2} : {3}.FromTicks(ToInt64(p))", "TimeSpan", "TimeSpan?");
|
|
343 ConvertFrom("Binary", "p == null || p.Length == 0? {2} : {3}.FromTicks(ToInt64(p.ToArray()))", "TimeSpan", "TimeSpan?");
|
|
344
|
|
345 // To UInt16
|
|
346 //
|
|
347 ConvertNumber("UInt16",
|
|
348 new string[] { "UInt16", "Byte" },
|
|
349 new string[] { "SByte", "Int16", "Int32", "Int64", "UInt32", "UInt64", "Single", "Double", "Decimal", "Char" },
|
|
350 new string[] { "Byte" },
|
|
351 new string[] { "String", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
352
|
|
353 // To UInt32
|
|
354 //
|
|
355 ConvertNumber("UInt32",
|
|
356 new string[] { "Byte", "UInt16", "UInt32" },
|
|
357 new string[] { "SByte", "Int16", "Int32", "Int64", "UInt64", "Single", "Double", "Decimal", "Char" },
|
|
358 new string[] { "Byte" },
|
|
359 new string[] { "String", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
360
|
|
361 // To UInt64
|
|
362 //
|
|
363 ConvertNumber("UInt64",
|
|
364 new string[] { "Byte", "UInt16", "UInt32", "UInt64" },
|
|
365 new string[] { "SByte", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Char" },
|
|
366 new string[] { "Byte" },
|
|
367 new string[] { "String", "Int16", "Int32", "Int64", "Single", "Double", "Decimal", "Money", "Boolean" });
|
|
368
|
|
369 // To SqlBinary
|
|
370 //
|
|
371 ConvertTo("SqlBinary", "p", "Byte[]");
|
|
372 ConvertTo("SqlBinary", "p == null ? {2} : p.ToArray()", "Binary");
|
|
373 ConvertTo("SqlBinary", "p == Guid.Empty? {2} : new SqlGuid(p).To{0}()", "Guid");
|
|
374 ConvertTo("SqlBinary", "p.HasValue ? new SqlGuid(p.Value).ToSqlBinary(): {2}", "Guid?");
|
|
375 ConvertTo("SqlBinary", "p.To{0}()", "SqlBytes" , "SqlGuid");
|
|
376
|
|
377 // To SqlXml
|
|
378 //
|
|
379 ConvertTo("SqlXml", "p == null ? {2} : new {0}(new XmlTextReader(new StringReader(p)))", "String");
|
|
380 ConvertTo("SqlXml", "p == null ? {2} : new {0}(p)", "Stream", "XmlReader");
|
|
381 ConvertTo("SqlXml", "p == null ? {2} : new {0}(new XmlTextReader(new StringReader(p.InnerXml)))", "XmlDocument");
|
|
382 ConvertTo("SqlXml", "p == null ? {2} : new {0}(new XmlTextReader(new StringReader(p.ToString())))", "XElement");
|
|
383 ConvertTo("SqlXml", "p == null ? {2} : new {0}(new XmlTextReader(new StringReader(new string(p))))", "Char[]");
|
|
384 ConvertTo("SqlXml", "p == null ? {2} : new {0}(new MemoryStream(p))", "Byte[]");
|
|
385 ConvertTo("SqlXml", "p == null ? {2} : new {0}(new MemoryStream(p.ToArray()))", "Binary");
|
|
386 ConvertTo("SqlXml", "p.IsNull ? {2} : new {0}(new XmlTextReader(new StringReader(p.Value)))", "SqlString");
|
|
387 ConvertTo("SqlXml", "p.IsNull ? {2} : new {0}(new XmlTextReader(new StringReader(p.ToSqlString().Value)))", "SqlChars");
|
|
388 ConvertTo("SqlXml", "p.IsNull ? {2} : new {0}(new MemoryStream(p.Value))", "SqlBinary");
|
|
389 ConvertTo("SqlXml", "p.IsNull ? {2} : new {0}(p.Stream)", "SqlBytes");
|
|
390
|
|
391 // To Stream
|
|
392 //
|
|
393 ConvertTo("Stream", "p == Guid.Empty ? {2} : new MemoryStream(p.ToByteArray())", "Guid");
|
|
394 ConvertTo("Stream", "p == null ? {2} : new MemoryStream(p)", "Byte[]");
|
|
395 ConvertTo("Stream", "p == null ? {2} : new MemoryStream(p.ToArray())", "Binary");
|
|
396 ConvertTo("Stream", "p.HasValue ? new MemoryStream(p.Value.ToByteArray()) : {2}", "Guid?");
|
|
397 ConvertTo("Stream", "p.IsNull ? {2} : p.Stream", "SqlBytes");
|
|
398 ConvertTo("Stream", "p.IsNull ? {2} : new MemoryStream(p.Value)", "SqlBinary");
|
|
399 ConvertTo("Stream", "p.IsNull ? {2} : new MemoryStream(p.Value.ToByteArray())", "SqlGuid");
|
|
400
|
|
401 // To Type
|
|
402 //
|
|
403 ConvertTo("Type", "p == null ? {2} : Type.GetType(p)", "String");
|
|
404 ConvertTo("Type", "p == null ? {2} : Type.GetType(new string(p))", "Char[]");
|
|
405 ConvertTo("Type", "p == null ? {2} : Type.GetTypeFromCLSID(ToGuid(p))", "Byte[]");
|
|
406 ConvertTo("Type", "p == null ? {2} : Type.GetTypeFromCLSID(ToGuid(p.ToArray()))", "Binary");
|
|
407 ConvertTo("Type", "p == Guid.Empty ? {2} : Type.GetTypeFromCLSID(p)", "Guid");
|
|
408 ConvertTo("Type", "p.HasValue ? Type.GetTypeFromCLSID(p.Value) : {2}", "Guid?");
|
|
409 ConvertTo("Type", "p.IsNull ? {2} : Type.GetType(p.Value)", "SqlString");
|
|
410 ConvertTo("Type", "p.IsNull ? {2} : Type.GetType(new string(p.Value))", "SqlChars");
|
|
411 ConvertTo("Type", "p.IsNull ? {2} : Type.GetTypeFromCLSID(p.Value)", "SqlGuid");
|
|
412
|
|
413 // To Byte[]
|
|
414 //
|
|
415 ConvertTo("Byte[]", "p == null ? {2} : System.Text.Encoding.UTF8.GetBytes(p)", "String");
|
|
416 ConvertTo("Byte[]", "new[] {{ p }}", "Byte");
|
|
417 ConvertTo("Byte[]", "new[] {{ checked((Byte)p) }}", "SByte");
|
|
418 ConvertTo("Byte[]", "ToByteArray(p.ToBinary())", "DateTime");
|
|
419 ConvertTo("Byte[]", "ToByteArray(p.LocalDateTime.ToBinary())", "DateTimeOffset");
|
|
420 ConvertTo("Byte[]", "p == null ? {2} : p.ToArray()", "Binary");
|
|
421 ConvertTo("Byte[]", "ToByteArray(p.Ticks)", "TimeSpan");
|
|
422 ConvertTo("Byte[]", "p == Guid.Empty ? {2} : p.ToByteArray()", "Guid");
|
|
423
|
|
424 ConvertTo("Byte[]", "BitConverter.GetBytes(p)",
|
|
425 "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Single", "Double", "Boolean", "Char");
|
|
426 ConvertTo("Byte[]", "p.HasValue ? ToByteArray(p.Value) : {2}",
|
|
427 "SByte?", "Int16?", "Int32?", "Int64?", "Byte?", "UInt16?", "UInt32?", "UInt64?", "Single?", "Double?", "Boolean?", "Decimal?", "Char?", "DateTime?", "DateTimeOffset?", "TimeSpan?", "Guid?");
|
|
428
|
|
429 ConvertTo("Byte[]", "p.IsNull ? {2} : p.Value", "SqlBinary", "SqlBytes");
|
|
430 ConvertTo("Byte[]", "p.IsNull ? {2} : p.ToByteArray()", "SqlGuid");
|
|
431 ConvertTo("Byte[]", "p.IsNull ? {2} : ToByteArray(p.Value)",
|
|
432 "SqlString", "SqlByte", "SqlInt16", "SqlInt32", "SqlInt64", "SqlSingle", "SqlDouble", "SqlDecimal", "SqlMoney", "SqlBoolean");
|
|
433
|
|
434 // To SqlBytes
|
|
435 //
|
|
436 ConvertTo("SqlBytes", "p == null ? {2} : new {0}(p)", "Byte[]", "Stream");
|
|
437 ConvertTo("SqlBytes", "p == null ? {2} : new {0}(p.ToArray())", "Binary");
|
|
438 ConvertTo("SqlBytes", "p == Guid.Empty ? {2}: new {0}(p.ToByteArray())", "Guid");
|
|
439 ConvertTo("SqlBytes", "p.HasValue ? new {0}(p.Value.ToByteArray()) : {2}", "Guid?");
|
|
440 ConvertTo("SqlBytes", "p.IsNull ? {2} : new {0}(p)", "SqlBinary");
|
|
441 ConvertTo("SqlBytes", "p.IsNull ? {2} : new {0}(p.ToByteArray())", "SqlGuid");
|
|
442
|
|
443 ConvertTo("SqlBytes", "p == null ? {2} : new {0}(ToByteArray(p))", "String");
|
|
444
|
|
445 ConvertTo("SqlBytes", "new {0}(ToByteArray(p))",
|
|
446 "Byte", "SByte", "DateTime", "DateTimeOffset", "TimeSpan", "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Single", "Double", "Boolean", "Char");
|
|
447 ConvertTo("SqlBytes", "p.HasValue ? ToSqlBytes(p.Value) : {2}",
|
|
448 "SByte?", "Int16?", "Int32?", "Int64?", "Byte?", "UInt16?", "UInt32?", "UInt64?", "Single?", "Double?", "Boolean?", "Decimal?", "Char?", "DateTime?", "DateTimeOffset?", "TimeSpan?", "Guid?");
|
|
449
|
|
450 ConvertTo("Byte[]", "p.IsNull ? {2} : ToSqlBytes(p.Value)",
|
|
451 "SqlString", "SqlByte", "SqlInt16", "SqlInt32", "SqlInt64", "SqlSingle", "SqlDouble", "SqlDecimal", "SqlMoney", "SqlBoolean");
|
|
452
|
|
453
|
|
454
|
|
455 string[] numberTypes1 = new string[] { "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Char", "Single", "Double", "SqlByte", "SqlInt16", "SqlInt32", "SqlInt64", "SqlSingle", "SqlDouble", "SqlDecimal", "SqlMoney" };
|
|
456 string[] numberTypes2 = Add(numberTypes1, "SByte", "Byte");
|
|
457 string[] numberTypes1n = numberTypes1.Where(_ => !_.StartsWith("Sql")).Select(_ => _ + "?").ToArray();
|
|
458 string[] numberTypes2n = numberTypes2.Where(_ => !_.StartsWith("Sql")).Select(_ => _ + "?").ToArray();
|
|
459
|
|
460 // From String
|
|
461 //
|
|
462 ConvertFrom("String", "p == null? {2} : {3}.Parse(p)",
|
|
463 Add(Add(numberTypes2, numberTypes2n), "Decimal", "DateTimeOffset", "TimeSpan", "Decimal?", "DateTimeOffset?", "TimeSpan?"));
|
|
464
|
|
465 // From Boolean
|
|
466 //
|
|
467 ConvertFrom("Boolean", "p ? ({0})1 : ({0})0", Add(Add(numberTypes2, numberTypes2n), "Decimal", "Decimal?"));
|
|
468
|
|
469 // From Boolean?
|
|
470 //
|
|
471 ConvertFrom("Boolean?", "p.HasValue && p.Value ? ({0})1: ({0})0", Add(Add(numberTypes2, numberTypes2n), "Decimal", "Decimal?"));
|
|
472
|
|
473 // From Byte[]
|
|
474 //
|
|
475 ConvertFrom("Byte[]", "p == null || p.Length == 0 ? {2} : BitConverter.To{3}(p, 0)", Add(numberTypes1, numberTypes1n));
|
|
476
|
|
477 // From Binary
|
|
478 //
|
|
479 ConvertFrom("Binary", "p == null || p.Length == 0 ? {2} : BitConverter.To{3}(p.ToArray(), 0)", Add(numberTypes1, numberTypes1n));
|
|
480
|
|
481 Generate();
|
|
482 #>
|
|
483 }
|
|
484 }
|
|
485 <#+
|
|
486 class ToType
|
|
487 {
|
|
488 public string Group;
|
|
489 public string Name;
|
|
490 public string Method;
|
|
491 public bool NonCompliant;
|
|
492 public bool NonSilverlightable; // :)
|
|
493 public string NullValue;
|
|
494 public string FromObjectReturn;
|
|
495
|
|
496 public Dictionary<string,string> Froms = new Dictionary<string,string>();
|
|
497 public HashSet<string> NonSLMethods = new HashSet<string>();
|
|
498 }
|
|
499
|
|
500 Dictionary<string,ToType> _types;
|
|
501
|
|
502 void ConvertTo(string toType, string convertText, params string[] fromTypes)
|
|
503 {
|
|
504 foreach (string fromType in fromTypes)
|
|
505 {
|
|
506 if (toType == fromType)
|
|
507 continue;
|
|
508
|
|
509 if (!_types.ContainsKey(fromType))
|
|
510 throw new InvalidOperationException(fromType);
|
|
511
|
|
512 if (!_types[toType].Froms.ContainsKey(fromType))
|
|
513 _types[toType].Froms.Add(fromType, string.Format(
|
|
514 convertText,
|
|
515 toType,
|
|
516 fromType,
|
|
517 _types[toType].NullValue ?? "(" + toType + ")null",
|
|
518 toType. TrimEnd('?').Replace("Sql", "").Replace("Money", "Decimal"),
|
|
519 fromType.TrimEnd('?').Replace("Sql", "").Replace("Money", "Decimal")));
|
|
520 }
|
|
521 }
|
|
522
|
|
523 void ConvertTo(string[] toTypes, string convertText, params string[] fromTypes)
|
|
524 {
|
|
525 foreach (var t in toTypes)
|
|
526 ConvertTo(t, convertText, fromTypes);
|
|
527 }
|
|
528
|
|
529 void ConvertFrom(string fromType, string convertText, params string[] toTypes)
|
|
530 {
|
|
531 foreach (string toType in toTypes)
|
|
532 ConvertTo(toType, convertText, fromType);
|
|
533 }
|
|
534
|
|
535 void ConvertToNull(string toType, string convertText, string convertTextNull, params string[] fromTypes)
|
|
536 {
|
|
537 ConvertTo(toType, convertText, fromTypes);
|
|
538 ConvertTo(toType, convertTextNull, fromTypes.Select(_ => _ + "?").ToArray());
|
|
539 }
|
|
540
|
|
541 void ConvertToNull(string[] toTypes, string convertText, string convertTextNull, params string[] fromTypes)
|
|
542 {
|
|
543 foreach (var t in toTypes)
|
|
544 ConvertToNull(t, convertText, convertTextNull, fromTypes);
|
|
545 }
|
|
546
|
|
547 void ConvertNumber(string toType, string[] types1, string[] types2, string[] types3, string[] types4)
|
|
548 {
|
|
549 if (toType != "Money")
|
|
550 {
|
|
551 ConvertToNull(toType,
|
|
552 "p",
|
|
553 "p.HasValue ? p.Value : {2}",
|
|
554 types1);
|
|
555 ConvertToNull(toType,
|
|
556 "checked(({0})p)",
|
|
557 "p.HasValue ? checked(({0})p.Value) : {2}",
|
|
558 types2);
|
|
559 ConvertTo(toType, "p.IsNull ? {2} : p.Value", Sql(types3));
|
|
560 ConvertTo(toType, "p.IsNull ? {2} : To{0}(p.Value)", Sql(types4));
|
|
561
|
|
562 ConvertToNull(toType + "?",
|
|
563 "p",
|
|
564 "p.HasValue ? p.Value : {2}",
|
|
565 types1);
|
|
566 ConvertToNull(toType + "?",
|
|
567 "checked(({0})p)",
|
|
568 "p.HasValue ? checked(({0})p.Value) : {2}",
|
|
569 types2);
|
|
570 ConvertTo(toType + "?", "p.IsNull ? {2} : p.Value", Sql(types3));
|
|
571 ConvertTo(toType + "?", "p.IsNull ? {2} : To{3}(p.Value)", Sql(types4));
|
|
572 }
|
|
573
|
|
574 if (_types.ContainsKey("Sql" + toType))
|
|
575 {
|
|
576 ConvertToNull("Sql" + toType,
|
|
577 "p",
|
|
578 "p.HasValue ? p.Value : {2}",
|
|
579 types1);
|
|
580 ConvertToNull("Sql" + toType,
|
|
581 "checked(({3})p)",
|
|
582 "p.HasValue ? checked(({3})p.Value) : {2}",
|
|
583 types2);
|
|
584 ConvertTo("Sql" + toType, "p.To{0}()", Sql(types3));
|
|
585 ConvertTo("Sql" + toType, "p.To{0}()", Sql(types4));
|
|
586 }
|
|
587 }
|
|
588
|
|
589 string[] Sql(params string[] types)
|
|
590 {
|
|
591 return types.Select(_ => "Sql" + _).ToArray();
|
|
592 }
|
|
593
|
|
594 string[] Add(string[] str1, params string[] str2)
|
|
595 {
|
|
596 return str1.Concat(str2).ToArray();
|
|
597 }
|
|
598
|
|
599 IEnumerable<ToType> Set(Action<ToType> action, IEnumerable<ToType> types)
|
|
600 {
|
|
601 foreach (var t in types)
|
|
602 {
|
|
603 action(t);
|
|
604 yield return t;
|
|
605 }
|
|
606 }
|
|
607
|
|
608 string LenDiff(int max, string str)
|
|
609 {
|
|
610 var s = "";
|
|
611
|
|
612 while (max-- > str.Length)
|
|
613 s += " ";
|
|
614
|
|
615 return s;
|
|
616 }
|
|
617
|
|
618 Dictionary<string,int> _groupOrder = new Dictionary<string,int>
|
|
619 {
|
|
620 { "Simple", 1 },
|
|
621 { "Nullable", 2 },
|
|
622 { "Sql", 3 },
|
|
623 { "Other", 4 },
|
|
624 };
|
|
625
|
|
626 HashSet<string> _codedTypes = new HashSet<string>
|
|
627 {
|
|
628 "Boolean", "Char", "SByte", "Byte", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", "Single", "Double", "Decimal", "DateTime", "String",
|
|
629 };
|
|
630
|
|
631 void Generate()
|
|
632 {
|
|
633 PushIndent("\t\t");
|
|
634
|
|
635 foreach (var gr in _types.Values.Select(_ => _.Group).Distinct())
|
|
636 {
|
|
637 WriteLine("");
|
|
638 WriteLine("#region {0} Types", gr);
|
|
639
|
|
640 foreach (var to in
|
|
641 from t in _types.Values
|
|
642 where t.Group == gr
|
|
643 orderby t.Name
|
|
644 select t)
|
|
645 {
|
|
646 WriteLine("");
|
|
647 WriteLine("#region {0}", to.Name);
|
|
648
|
|
649 var froms = from f in to.Froms.Keys select f;
|
|
650
|
|
651 if (to.NonSilverlightable)
|
|
652 {
|
|
653 ClearIndent();
|
|
654 WriteLine("");
|
|
655 WriteLine("#if !SILVERLIGHT");
|
|
656 PushIndent("\t\t");
|
|
657
|
|
658 froms =
|
|
659 from f in froms
|
|
660 orderby _groupOrder[_types[f].Group], _types[f].Name
|
|
661 select f;
|
|
662 }
|
|
663 else
|
|
664 {
|
|
665 froms =
|
|
666 from f in froms
|
|
667 orderby _types[f].NonSilverlightable, _groupOrder[_types[f].Group], _types[f].Name
|
|
668 select f;
|
|
669 }
|
|
670
|
|
671 var group = "";
|
|
672 var sl = false;
|
|
673 var len = froms.Concat(new[] { "" }).Select(_ => _.Length).Max();
|
|
674 var lcode = to.Froms.Values.Concat(new[] { "" }).Select(_ => (_ ?? "").Length).Max();
|
|
675
|
|
676 foreach (var f in froms)
|
|
677 {
|
|
678 var code = to.Froms[f];
|
|
679
|
|
680 if (code == null)
|
|
681 continue;
|
|
682
|
|
683 var fr = _types[f];
|
|
684
|
|
685 if (!to.NonSilverlightable && sl != fr.NonSilverlightable)
|
|
686 {
|
|
687 sl = fr.NonSilverlightable;
|
|
688
|
|
689 ClearIndent();
|
|
690 WriteLine("");
|
|
691 WriteLine("#if !SILVERLIGHT");
|
|
692 PushIndent("\t\t");
|
|
693 }
|
|
694
|
|
695 if (fr.Group != group)
|
|
696 {
|
|
697 group = fr.Group;
|
|
698
|
|
699 WriteLine("");
|
|
700 WriteLine("// {0} Types", group);
|
|
701 WriteLine("//");
|
|
702 }
|
|
703
|
|
704 if (to.NonSLMethods.Contains(fr.Name))
|
|
705 {
|
|
706 ClearIndent();
|
|
707 WriteLine("#if !SILVERLIGHT");
|
|
708 PushIndent("\t\t");
|
|
709 }
|
|
710
|
|
711 WriteLine("/// <summary>Converts the value from <c>{0}</c> to an equivalent <c>{1}</c> value.</summary>", fr.Name, to.Name);
|
|
712
|
|
713 if (to.NonCompliant || fr.NonCompliant)
|
|
714 WriteLine("[CLSCompliant(false)]");
|
|
715
|
|
716 WriteLine("public static {0} {1}({2}{3} p) {{ return {4};{5} }}",
|
|
717 to.Name, to.Method, fr.Name, LenDiff(len, fr.Name), code, LenDiff(lcode, code));
|
|
718
|
|
719 if (to.NonSLMethods.Contains(fr.Name))
|
|
720 {
|
|
721 ClearIndent();
|
|
722 WriteLine("#endif");
|
|
723 PushIndent("\t\t");
|
|
724 }
|
|
725 }
|
|
726
|
|
727 if (sl)
|
|
728 {
|
|
729 ClearIndent();
|
|
730 WriteLine("");
|
|
731 WriteLine("#endif");
|
|
732 PushIndent("\t\t");
|
|
733 }
|
|
734
|
|
735 // From Object
|
|
736 //
|
|
737 if (to.Froms.Count > 0)
|
|
738 {
|
|
739 WriteLine("");
|
|
740 WriteLine("// From Object");
|
|
741 WriteLine("//");
|
|
742
|
|
743 WriteLine("/// <summary>Converts the value from <c>Object</c> to an equivalent <c>{0}</c> value.</summary>", to.Name);
|
|
744
|
|
745 if (to.NonCompliant)
|
|
746 WriteLine("[CLSCompliant(false)]");
|
|
747
|
|
748 WriteLine("public static {0} {1}(object p)", to.Name, to.Method);
|
|
749 WriteLine("{");
|
|
750
|
|
751 PushIndent("\t");
|
|
752
|
|
753 var defType = false;
|
|
754
|
|
755 var tc =
|
|
756 from f in to.Froms.Keys
|
|
757 where _codedTypes.Contains(f)
|
|
758 select f;
|
|
759
|
|
760 if (tc.Any())
|
|
761 {
|
|
762 WriteLine("if (p == null) return {0};", to.NullValue ?? "null");
|
|
763 WriteLine("");
|
|
764
|
|
765 if (!_codedTypes.Contains(to.Name))
|
|
766 {
|
|
767 WriteLine("if (p is {0}) return ({0})p;", to.Name);
|
|
768 WriteLine("");
|
|
769 }
|
|
770
|
|
771 defType = true;
|
|
772
|
|
773 WriteLine("var type = p.GetType();");
|
|
774 WriteLine("");
|
|
775 WriteLine("// Primitive types");
|
|
776 WriteLine("//");
|
|
777 WriteLine("switch (Type.GetTypeCode(type))");
|
|
778 WriteLine("{");
|
|
779 PushIndent("\t");
|
|
780
|
|
781 len = new[] { to.Name, "DBNull" }.Concat(tc).Select(_ => _.Length).Max();
|
|
782
|
|
783 WriteLine("case TypeCode.DBNull{0} : return {1};", LenDiff(len, "DBNull"), to.NullValue ?? "null");
|
|
784
|
|
785 if (_codedTypes.Contains(to.Name))
|
|
786 WriteLine("case TypeCode.{0}{1} : return ({0})p;", to.Name, LenDiff(len, to.Name));
|
|
787
|
|
788 foreach (var t in tc)
|
|
789 {
|
|
790 if (to.NonSLMethods.Contains(t))
|
|
791 {
|
|
792 ClearIndent();
|
|
793 WriteLine("#if !SILVERLIGHT");
|
|
794 PushIndent("\t\t");
|
|
795 PushIndent("\t");
|
|
796 PushIndent("\t");
|
|
797 }
|
|
798
|
|
799 WriteLine("case TypeCode.{0}{1} : return {2}(({0}){1}p);", t, LenDiff(len, t), to.Method);
|
|
800
|
|
801 if (to.NonSLMethods.Contains(t))
|
|
802 {
|
|
803 ClearIndent();
|
|
804 WriteLine("#endif");
|
|
805 PushIndent("\t\t");
|
|
806 PushIndent("\t");
|
|
807 PushIndent("\t");
|
|
808 }
|
|
809 }
|
|
810
|
|
811 PopIndent();
|
|
812 WriteLine("}");
|
|
813 }
|
|
814 else
|
|
815 {
|
|
816 WriteLine("if (p == null || p is DBNull) return {0};", to.NullValue ?? "null");
|
|
817 WriteLine("");
|
|
818 WriteLine("if (p is {0}) return ({0})p;", to.Name);
|
|
819 }
|
|
820
|
|
821 tc =
|
|
822 from f in to.Froms.Keys
|
|
823 where !_codedTypes.Contains(f)
|
|
824 select f;
|
|
825
|
|
826 if (to.NonSilverlightable)
|
|
827 {
|
|
828 tc =
|
|
829 from f in tc
|
|
830 orderby _groupOrder[_types[f].Group], _types[f].Name
|
|
831 select f;
|
|
832 }
|
|
833 else
|
|
834 {
|
|
835 tc =
|
|
836 from f in tc
|
|
837 orderby _types[f].NonSilverlightable, _groupOrder[_types[f].Group], _types[f].Name
|
|
838 select f;
|
|
839 }
|
|
840
|
|
841 var openGroupBlock = false;
|
|
842
|
|
843 group = "";
|
|
844 sl = false;
|
|
845 len = new[] { "" }.Concat(tc).Select(_ => _.Length).Max();
|
|
846
|
|
847 foreach (var f in tc)
|
|
848 {
|
|
849 var fr = _types[f];
|
|
850
|
|
851 if (!to.NonSilverlightable && sl != fr.NonSilverlightable)
|
|
852 {
|
|
853 sl = fr.NonSilverlightable;
|
|
854
|
|
855 ClearIndent();
|
|
856 WriteLine("");
|
|
857 WriteLine("#if !SILVERLIGHT");
|
|
858 PushIndent("\t\t");
|
|
859 PushIndent("\t");
|
|
860 }
|
|
861
|
|
862 if (fr.Group != group)
|
|
863 {
|
|
864 group = fr.Group;
|
|
865
|
|
866 if (openGroupBlock)
|
|
867 {
|
|
868 openGroupBlock = false;
|
|
869 PopIndent();
|
|
870 WriteLine("}");
|
|
871 }
|
|
872
|
|
873 WriteLine("");
|
|
874 WriteLine("// {0} Types", group);
|
|
875 WriteLine("//");
|
|
876
|
|
877 if (group == "Nullable")
|
|
878 {
|
|
879 openGroupBlock = true;
|
|
880
|
|
881 if (!defType)
|
|
882 {
|
|
883 defType = true;
|
|
884 WriteLine("var type = p.GetType();");
|
|
885 WriteLine("");
|
|
886 }
|
|
887
|
|
888 WriteLine("if (type.IsGenericType)");
|
|
889 WriteLine("{");
|
|
890
|
|
891 PushIndent("\t");
|
|
892 }
|
|
893 else if (group == "Sql")
|
|
894 {
|
|
895 openGroupBlock = true;
|
|
896
|
|
897 WriteLine("if (p is INullable)");
|
|
898 WriteLine("{");
|
|
899
|
|
900 PushIndent("\t");
|
|
901 }
|
|
902 }
|
|
903
|
|
904 if (to.NonSLMethods.Contains(fr.Name))
|
|
905 {
|
|
906 ClearIndent();
|
|
907 WriteLine("#if !SILVERLIGHT");
|
|
908 PushIndent("\t\t");
|
|
909 PushIndent("\t");
|
|
910 if (openGroupBlock)
|
|
911 PushIndent("\t");
|
|
912 }
|
|
913
|
|
914 WriteLine("if (p is {0}){1} return {2}(({0}){1}p);", fr.Name, LenDiff(len, fr.Name), to.Method);
|
|
915
|
|
916 if (to.NonSLMethods.Contains(fr.Name))
|
|
917 {
|
|
918 ClearIndent();
|
|
919 WriteLine("#endif");
|
|
920 PushIndent("\t\t");
|
|
921 PushIndent("\t");
|
|
922 if (openGroupBlock)
|
|
923 PushIndent("\t");
|
|
924 }
|
|
925 }
|
|
926
|
|
927 if (openGroupBlock)
|
|
928 {
|
|
929 PopIndent();
|
|
930 WriteLine("}");
|
|
931 }
|
|
932
|
|
933 if (sl)
|
|
934 {
|
|
935 ClearIndent();
|
|
936 WriteLine("");
|
|
937 WriteLine("#endif");
|
|
938 PushIndent("\t\t");
|
|
939 PushIndent("\t");
|
|
940 }
|
|
941
|
|
942 if (_codedTypes.Contains(to.Name))
|
|
943 {
|
|
944 WriteLine("");
|
|
945 WriteLine("if (p is IConvertible) return ((IConvertible)p).To{0}(null);", to.Name);
|
|
946 }
|
|
947
|
|
948 WriteLine("");
|
|
949
|
|
950 if (to.FromObjectReturn != null)
|
|
951 WriteLine(to.FromObjectReturn);
|
|
952 else
|
|
953 WriteLine("throw CreateInvalidCastException(p.GetType(), typeof({0}));", to.Name);
|
|
954
|
|
955 PopIndent();
|
|
956
|
|
957 WriteLine("}");
|
|
958 }
|
|
959
|
|
960 if (to.NonSilverlightable)
|
|
961 {
|
|
962 ClearIndent();
|
|
963 WriteLine("");
|
|
964 WriteLine("#endif");
|
|
965 PushIndent("\t\t");
|
|
966 }
|
|
967
|
|
968 WriteLine("");
|
|
969 WriteLine("#endregion");
|
|
970 }
|
|
971
|
|
972 WriteLine("");
|
|
973 WriteLine("#endregion");
|
|
974 }
|
|
975
|
|
976 ClearIndent();
|
|
977 }
|
|
978 #>
|