-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.cs
403 lines (382 loc) · 29.1 KB
/
Script.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
namespace WebServiceStudio
{
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;
internal class Script
{
private CodeTypeDeclaration codeClass;
private CodeNamespace codeNamespace;
private CodeCompileUnit compileUnit;
private static string dumpClassCS = "\r\npublic class Dumper {\r\n Hashtable objects = new Hashtable();\r\n TextWriter writer;\r\n int indent = 0;\r\n\r\n public Dumper():this(Console.Out) {\r\n }\r\n public Dumper(TextWriter writer) {\r\n this.writer = writer;\r\n }\r\n public static void Dump(string name, object o){\r\n Dumper d = new Dumper();\r\n d.DumpInternal(name, o);\r\n }\r\n\r\n private void DumpInternal(string name, object o) {\r\n for (int i1 = 0; i1 < indent; i1++)\r\n writer.Write(\"--- \");\r\n\r\n if (name == null) name = string.Empty;\r\n\r\n if (o == null) {\r\n writer.WriteLine(name + \" = null\");\r\n return;\r\n }\r\n\r\n Type type = o.GetType();\r\n\r\n writer.Write(type.Name + \" \" + name);\r\n\r\n if (objects[o] != null) {\r\n writer.WriteLine(\" = ...\");\r\n return;\r\n }\r\n\r\n if (!type.IsValueType && !type.Equals(typeof(string)))\r\n objects.Add(o, o);\r\n\r\n if (type.IsArray) {\r\n Array a = (Array)o;\r\n writer.WriteLine();\r\n indent++;\r\n for (int j = 0; j < a.Length; j++)\r\n DumpInternal(\"[\" + j + \"]\", a.GetValue(j));\r\n indent--;\r\n return;\r\n }\r\n if (o is XmlQualifiedName) {\r\n DumpInternal(\"Name\", ((XmlQualifiedName) o).Name);\r\n DumpInternal(\"Namespace\", ((XmlQualifiedName) o).Namespace);\r\n return;\r\n }\r\n if (o is XmlNode) {\r\n string xml = ((XmlNode)o).OuterXml;\r\n writer.WriteLine(\" = \" + xml);\r\n return;\r\n }\r\n if (type.IsEnum) {\r\n writer.WriteLine(\" = \" + ((Enum)o).ToString());\r\n return;\r\n }\r\n if (type.IsPrimitive) {\r\n writer.WriteLine(\" = \" + o.ToString());\r\n return;\r\n }\r\n if (typeof(Exception).IsAssignableFrom(type)) { \r\n writer.WriteLine(\" = \" + ((Exception)o).Message);\r\n return;\r\n }\r\n if (o is DataSet) {\r\n writer.WriteLine();\r\n indent++;\r\n DumpInternal(\"Tables\", ((DataSet)o).Tables);\r\n indent--;\r\n return;\r\n }\r\n if (o is DateTime) {\r\n writer.WriteLine(\" = \" + o.ToString());\r\n return;\r\n }\r\n if (o is DataTable) {\r\n writer.WriteLine();\r\n indent++;\r\n DataTable table = (DataTable)o;\r\n DumpInternal(\"TableName\", table.TableName);\r\n DumpInternal(\"Rows\", table.Rows);\r\n indent--;\r\n return;\r\n }\r\n if (o is DataRow) {\r\n writer.WriteLine();\r\n indent++;\r\n DataRow row = (DataRow)o;\r\n DumpInternal(\"Values\", row.ItemArray);\r\n indent--;\r\n return;\r\n }\r\n if (o is string) {\r\n string s = (string)o;\r\n if (s.Length > 40) {\r\n writer.WriteLine(\" = \");\r\n writer.WriteLine(\"\\\"\" + s + \"\\\"\");\r\n }\r\n else {\r\n writer.WriteLine(\" = \\\"\" + s + \"\\\"\");\r\n }\r\n return;\r\n }\r\n if (o is IEnumerable) {\r\n IEnumerator e = ((IEnumerable)o).GetEnumerator();\r\n if (e == null) {\r\n writer.WriteLine(\" GetEnumerator() == null\");\r\n return;\r\n }\r\n writer.WriteLine();\r\n int c = 0;\r\n indent++;\r\n while (e.MoveNext()) {\r\n DumpInternal(\"[\" + c + \"]\", e.Current);\r\n c++;\r\n }\r\n indent--;\r\n return;\r\n }\r\n writer.WriteLine();\r\n indent++;\r\n FieldInfo[] fields = type.GetFields();\r\n for (int i2 = 0; i2 < fields.Length; i2++) {\r\n FieldInfo f = fields[i2];\r\n if (!f.IsStatic)\r\n DumpInternal(f.Name, f.GetValue(o));\r\n }\r\n PropertyInfo[] props = type.GetProperties();\r\n for (int i3 = 0; i3 < props.Length; i3++) {\r\n PropertyInfo p = props[i3];\r\n if (p.CanRead &&\r\n (typeof(IEnumerable).IsAssignableFrom(p.PropertyType) || p.CanWrite) &&\r\n !p.PropertyType.Equals(type)){\r\n object v;\r\n try {\r\n v = p.GetValue(o, null);\r\n }\r\n catch (Exception e) {\r\n v = e;\r\n }\r\n DumpInternal(p.Name, v);\r\n }\r\n }\r\n indent--;\r\n }\r\n}\r\n";
private static string dumpClassVB = "Public Class Dumper\r\n Private objects As New Hashtable()\r\n Private writer As TextWriter\r\n Private indent As Integer = 0\r\n \r\n \r\n Public Sub New()\r\n MyClass.New(Console.Out)\r\n End Sub 'New\r\n \r\n Public Sub New(writer As TextWriter)\r\n Me.writer = writer\r\n End Sub 'New\r\n \r\n Public Shared Sub Dump(name As String, o As Object)\r\n Dim d As New Dumper()\r\n d.DumpInternal(name, o)\r\n End Sub 'Dump\r\n \r\n \r\n Private Sub DumpInternal(name As String, o As Object)\r\n Dim i1 As Integer\r\n For i1 = 0 To indent - 1\r\n writer.Write(\"--- \")\r\n Next i1 \r\n If name Is Nothing Then\r\n name = String.Empty\r\n End If \r\n If o Is Nothing Then\r\n writer.WriteLine((name + \" = null\"))\r\n Return\r\n End If\r\n \r\n Dim type As Type = o.GetType()\r\n \r\n writer.Write((type.Name + \" \" + name))\r\n \r\n If Not (objects(o) Is Nothing) Then\r\n writer.WriteLine(\" = ...\")\r\n Return\r\n End If\r\n \r\n If Not type.IsValueType And Not type.Equals(GetType(String)) Then\r\n objects.Add(o, o)\r\n End If \r\n If type.IsArray Then\r\n Dim a As Array = CType(o, Array)\r\n writer.WriteLine()\r\n indent += 1\r\n Dim j As Integer\r\n For j = 0 To a.Length - 1\r\n DumpInternal(\"[\" + j + \"]\", a.GetValue(j))\r\n Next j\r\n indent -= 1\r\n Return\r\n End If\r\n If TypeOf o Is XmlQualifiedName Then\r\n DumpInternal(\"Name\", CType(o, XmlQualifiedName).Name)\r\n DumpInternal(\"Namespace\", CType(o, XmlQualifiedName).Namespace)\r\n Return\r\n End If\r\n If TypeOf o Is XmlNode Then\r\n Dim xml As String = CType(o, XmlNode).OuterXml\r\n writer.WriteLine((\" = \" + xml))\r\n Return\r\n End If\r\n If type.IsEnum Then\r\n writer.WriteLine((\" = \" + CType(o, [Enum]).ToString()))\r\n Return\r\n End If\r\n If type.IsPrimitive Then\r\n writer.WriteLine((\" = \" + o.ToString()))\r\n Return\r\n End If\r\n If GetType(Exception).IsAssignableFrom(type) Then\r\n writer.WriteLine((\" = \" + CType(o, Exception).Message))\r\n Return\r\n End If\r\n If TypeOf o Is DataSet Then\r\n writer.WriteLine()\r\n indent += 1\r\n DumpInternal(\"Tables\", CType(o, DataSet).Tables)\r\n indent -= 1\r\n Return\r\n End If\r\n If TypeOf o Is DateTime Then\r\n writer.WriteLine((\" = \" + o.ToString()))\r\n Return\r\n End If\r\n If TypeOf o Is DataTable Then\r\n writer.WriteLine()\r\n indent += 1\r\n Dim table As DataTable = CType(o, DataTable)\r\n DumpInternal(\"TableName\", table.TableName)\r\n DumpInternal(\"Rows\", table.Rows)\r\n indent -= 1\r\n Return\r\n End If\r\n If TypeOf o Is DataRow Then\r\n writer.WriteLine()\r\n indent += 1\r\n Dim row As DataRow = CType(o, DataRow)\r\n DumpInternal(\"Values\", row.ItemArray)\r\n indent -= 1\r\n Return\r\n End If\r\n If TypeOf o Is String Then\r\n Dim s As String = CStr(o)\r\n If s.Length > 40 Then\r\n writer.WriteLine(\" = \")\r\n writer.WriteLine((\"\"\"\" + s + \"\"\"\"))\r\n Else\r\n writer.WriteLine((\" = \"\"\" + s + \"\"\"\"))\r\n End If\r\n Return\r\n End If\r\n If TypeOf o Is IEnumerable Then\r\n Dim e As IEnumerator = CType(o, IEnumerable).GetEnumerator()\r\n If e Is Nothing Then\r\n writer.WriteLine(\" GetEnumerator() == null\")\r\n Return\r\n End If\r\n writer.WriteLine()\r\n Dim c As Integer = 0\r\n indent += 1\r\n While e.MoveNext()\r\n DumpInternal(\"[\" + c + \"]\", e.Current)\r\n c += 1\r\n End While\r\n indent -= 1\r\n Return\r\n End If\r\n writer.WriteLine()\r\n indent += 1\r\n Dim fields As FieldInfo() = type.GetFields()\r\n Dim i2 As Integer\r\n For i2 = 0 To fields.Length - 1\r\n Dim f As FieldInfo = fields(i2)\r\n If Not f.IsStatic Then\r\n DumpInternal(f.Name, f.GetValue(o))\r\n End If\r\n Next i2\r\n Dim props As PropertyInfo() = type.GetProperties()\r\n Dim i3 As Integer\r\n For i3 = 0 To props.Length - 1\r\n Dim p As PropertyInfo = props(i3)\r\n If p.CanRead And(GetType(IEnumerable).IsAssignableFrom(p.PropertyType) Or p.CanWrite) And Not p.PropertyType.Equals(type) Then\r\n Dim v As Object\r\n Try\r\n v = p.GetValue(o, Nothing)\r\n Catch e As Exception\r\n v = e\r\n End Try\r\n DumpInternal(p.Name, v)\r\n End If\r\n Next i3\r\n indent -= 1\r\n End Sub 'DumpInternal\r\nEnd Class 'Dumper\r\n";
private CodeMethodReferenceExpression dumpMethodRef;
private CodeEntryPointMethod mainMethod;
private HttpWebClientProtocol proxy;
private ProxySettings proxySetting;
private static string usingStatementsCS = "\r\nusing System.CodeDom.Compiler;\r\nusing System.CodeDom;\r\nusing System.Collections;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Globalization;\r\nusing System.IO;\r\nusing System.Reflection;\r\nusing System.Web.Services.Protocols;\r\nusing System.Xml.Serialization;\r\nusing System.Xml;\r\nusing System;\r\n";
private static string usingStatementsVB = "\r\nImports System.CodeDom.Compiler\r\nImports System.CodeDom\r\nImports System.Collections\r\nImports System.ComponentModel\r\nImports System.Data\r\nImports System.Globalization\r\nImports System.IO\r\nImports System.Reflection\r\nImports System.Web.Services.Protocols\r\nImports System.Xml.Serialization\r\nImports System.Xml\r\nImports System\r\n";
public Script() : this("WebServiceStudio", "MainClass")
{
}
public Script(string namespaceToGen, string className)
{
this.compileUnit = new CodeCompileUnit();
this.codeNamespace = new CodeNamespace(namespaceToGen);
this.compileUnit.Namespaces.Add(this.codeNamespace);
this.codeClass = new CodeTypeDeclaration(className);
this.codeNamespace.Types.Add(this.codeClass);
this.proxySetting = ProxySettings.RequiredHeaders;
this.mainMethod = new CodeEntryPointMethod();
this.mainMethod.Name = "Main";
this.mainMethod.Attributes = MemberAttributes.Public | MemberAttributes.Static;
this.codeClass.Members.Add(this.mainMethod);
this.dumpMethodRef = this.BuildDumper();
}
public void AddMethod(MethodInfo method, object[] parameters)
{
this.BuildMethod(this.codeClass.Members, method, parameters);
}
private CodeExpression BuildArray(CodeStatementCollection statements, string name, object value)
{
Array array = (Array) value;
Type type = value.GetType();
string uniqueVariableName = GetUniqueVariableName(name, statements);
CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(type.FullName, uniqueVariableName);
statement.InitExpression = new CodeArrayCreateExpression(type.GetElementType(), array.Length);
statements.Add(statement);
CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression(uniqueVariableName);
string str2 = name + "_";
for (int i = 0; i < array.Length; i++)
{
CodeArrayIndexerExpression left = new CodeArrayIndexerExpression(targetObject, new CodeExpression[0]);
left.Indices.Add(new CodePrimitiveExpression(i));
CodeExpression right = this.BuildObject(statements, str2 + i.ToString(), array.GetValue(i));
statements.Add(new CodeAssignStatement(left, right));
}
return targetObject;
}
private CodeExpression BuildClass(CodeStatementCollection statements, string name, object value)
{
Type type = value.GetType();
string uniqueVariableName = GetUniqueVariableName(name, statements);
CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(type.FullName, uniqueVariableName);
statement.InitExpression = new CodeObjectCreateExpression(type.FullName, new CodeExpression[0]);
statements.Add(statement);
CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression(uniqueVariableName);
foreach (MemberInfo info in type.GetMembers(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
{
object obj2 = null;
Type fieldType = typeof(object);
CodeExpression left = null;
if (info is FieldInfo)
{
FieldInfo info2 = (FieldInfo) info;
if (info2.IsStatic || info2.IsInitOnly)
{
goto Label_014B;
}
fieldType = info2.FieldType;
obj2 = info2.GetValue(value);
left = new CodeFieldReferenceExpression(targetObject, info2.Name);
}
else if (info is PropertyInfo)
{
PropertyInfo info3 = (PropertyInfo) info;
if (!info3.CanWrite)
{
goto Label_014B;
}
MethodInfo getMethod = info3.GetGetMethod();
if ((getMethod.GetParameters().Length > 0) || getMethod.IsStatic)
{
goto Label_014B;
}
fieldType = info3.PropertyType;
obj2 = info3.GetValue(value, null);
left = new CodePropertyReferenceExpression(targetObject, info3.Name);
}
if (left != null)
{
CodeExpression right = this.BuildObject(statements, info.Name, obj2);
statements.Add(new CodeAssignStatement(left, right));
}
Label_014B:;
}
return targetObject;
}
public CodeMethodReferenceExpression BuildDumper()
{
return new CodeMethodReferenceExpression(new CodeTypeReferenceExpression("Dumper"), "Dump");
}
private void BuildDumpInvoke(CodeStatementCollection statements, string name, CodeExpression obj)
{
CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(this.dumpMethodRef, new CodeExpression[0]);
expression.Parameters.Add(new CodePrimitiveExpression(name));
expression.Parameters.Add(obj);
statements.Add(expression);
}
private void BuildMethod(CodeTypeMemberCollection members, MethodInfo method, object[] parameters)
{
CodeMemberMethod method2 = new CodeMemberMethod();
method2.Name = "Invoke" + method.Name;
this.mainMethod.Statements.Add(new CodeMethodInvokeExpression(null, method2.Name, new CodeExpression[0]));
method2.Attributes = MemberAttributes.Public | MemberAttributes.Static;
members.Add(method2);
CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(this.BuildProxy(method2.Statements, method), method.Name, new CodeExpression[0]);
this.BuildParameters(method2.Statements, method, parameters, expression.Parameters);
if (method.ReturnType == typeof(void))
{
method2.Statements.Add(new CodeExpressionStatement(expression));
}
else
{
string uniqueVariableName = GetUniqueVariableName(method.Name + "Result", method2.Statements);
method2.Statements.Add(new CodeVariableDeclarationStatement(method.ReturnType.FullName, uniqueVariableName, expression));
this.BuildDumpInvoke(method2.Statements, "result", new CodeVariableReferenceExpression(uniqueVariableName));
}
ParameterInfo[] infoArray = method.GetParameters();
for (int i = 0; i < infoArray.Length; i++)
{
ParameterInfo info = infoArray[i];
if (info.IsOut || info.ParameterType.IsByRef)
{
this.BuildDumpInvoke(method2.Statements, info.Name, ((CodeDirectionExpression) expression.Parameters[i]).Expression);
}
}
}
private CodeExpression BuildObject(CodeStatementCollection statements, string name, object value)
{
if (value == null)
{
return new CodePrimitiveExpression(null);
}
Type c = value.GetType();
if (c.IsPrimitive || (c == typeof(string)))
{
return new CodePrimitiveExpression(value);
}
if (c.IsEnum)
{
string[] strArray = value.ToString().Split(new char[] { ',' });
if (strArray.Length > 1)
{
CodeExpression left = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(c.FullName), strArray[0]);
for (int i = 1; i < strArray.Length; i++)
{
left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(c.FullName), strArray[i]));
}
return left;
}
return new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(c.FullName), value.ToString());
}
if (c == typeof(DateTime))
{
DateTime time = (DateTime) value;
string str = ((DateTime) value).ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz", DateTimeFormatInfo.InvariantInfo);
long ticks = time.Ticks;
statements.Add(new CodeCommentStatement("Init DateTime object value = " + str));
statements.Add(new CodeCommentStatement("We going to use DateTime ctor that takes Ticks"));
return new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] { new CodePrimitiveExpression(ticks) });
}
if (typeof(XmlNode).IsAssignableFrom(c))
{
return this.BuildXmlNode(statements, name, value);
}
if (c.IsArray)
{
return this.BuildArray(statements, name, value);
}
if (c.IsAbstract || (c.GetConstructor(new Type[0]) == null))
{
statements.Add(new CodeCommentStatement("Can not create object of type " + c.FullName + " because it does not have a default ctor"));
return new CodePrimitiveExpression(null);
}
return this.BuildClass(statements, name, value);
}
private void BuildParameters(CodeStatementCollection statements, MethodInfo method, object[] paramValues, CodeExpressionCollection parameters)
{
ParameterInfo[] infoArray = method.GetParameters();
for (int i = 0; i < infoArray.Length; i++)
{
ParameterInfo info = infoArray[i];
Type parameterType = infoArray[i].ParameterType;
FieldDirection @in = FieldDirection.In;
if (parameterType.IsByRef)
{
@in = FieldDirection.Ref;
parameterType = parameterType.GetElementType();
}
CodeExpression expression = null;
if (!info.IsOut)
{
expression = this.BuildObject(statements, info.Name, paramValues[i]);
}
else
{
@in = FieldDirection.Out;
}
if (@in != FieldDirection.In)
{
if ((expression == null) || !(expression is CodeVariableReferenceExpression))
{
CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(parameterType.FullName, info.Name);
if (expression != null)
{
statement.InitExpression = expression;
}
statements.Add(statement);
expression = new CodeVariableReferenceExpression(statement.Name);
}
expression = new CodeDirectionExpression(@in, expression);
}
parameters.Add(expression);
}
}
private CodeExpression BuildProxy(CodeStatementCollection statements, MethodInfo method)
{
Type type = this.proxy.GetType();
string name = CodeIdentifier.MakeCamel(type.Name);
if (this.proxySetting == ProxySettings.AllProperties)
{
return this.BuildClass(statements, name, this.proxy);
}
CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(type.Name, name);
statement.InitExpression = new CodeObjectCreateExpression(type.FullName, new CodeExpression[0]);
statements.Add(statement);
CodeExpression targetObject = new CodeVariableReferenceExpression(name);
FieldInfo[] soapHeaders = null;
if (this.proxySetting == ProxySettings.RequiredHeaders)
{
soapHeaders = MethodProperty.GetSoapHeaders(method, true);
}
else
{
soapHeaders = type.GetFields();
}
for (int i = 0; i < soapHeaders.Length; i++)
{
FieldInfo info = soapHeaders[i];
if (typeof(SoapHeader).IsAssignableFrom(info.FieldType))
{
CodeExpression left = new CodeFieldReferenceExpression(targetObject, info.Name);
CodeExpression right = this.BuildObject(statements, info.Name, info.GetValue(this.proxy));
statements.Add(new CodeAssignStatement(left, right));
}
}
return targetObject;
}
private CodeExpression BuildXmlNode(CodeStatementCollection statements, string name, object value)
{
Type type = value.GetType();
if (type == typeof(XmlElement))
{
XmlElement element = (XmlElement) value;
string str = GetUniqueVariableName(name + "Doc", statements);
CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(typeof(XmlDocument), str);
statement.InitExpression = new CodeObjectCreateExpression(typeof(XmlDocument), new CodeExpression[0]);
statements.Add(statement);
CodeVariableReferenceExpression expression = new CodeVariableReferenceExpression(str);
CodeMethodInvokeExpression expression2 = new CodeMethodInvokeExpression(expression, "LoadXml", new CodeExpression[0]);
expression2.Parameters.Add(new CodePrimitiveExpression(element.OuterXml));
statements.Add(expression2);
return new CodeFieldReferenceExpression(expression, "DocumentElement");
}
if (type != typeof(XmlAttribute))
{
throw new Exception("Unsupported XmlNode type");
}
XmlAttribute attribute = (XmlAttribute) value;
string uniqueVariableName = GetUniqueVariableName(name + "Doc", statements);
CodeVariableDeclarationStatement statement2 = new CodeVariableDeclarationStatement(typeof(XmlDocument), uniqueVariableName);
statement2.InitExpression = new CodeObjectCreateExpression(typeof(XmlDocument), new CodeExpression[0]);
statements.Add(statement2);
CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression(uniqueVariableName);
CodeMethodInvokeExpression expression4 = new CodeMethodInvokeExpression(targetObject, "CreateAttribute", new CodeExpression[0]);
expression4.Parameters.Add(new CodePrimitiveExpression(attribute.Name));
expression4.Parameters.Add(new CodePrimitiveExpression(attribute.NamespaceURI));
string str3 = GetUniqueVariableName(name + "Attr", statements);
CodeVariableDeclarationStatement statement3 = new CodeVariableDeclarationStatement(typeof(XmlAttribute), str3);
statement3.InitExpression = expression4;
statements.Add(statement3);
CodeVariableReferenceExpression expression5 = new CodeVariableReferenceExpression(str3);
statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(expression5, "Value"), new CodePrimitiveExpression(attribute.Value)));
return expression5;
}
public string Generate(CodeDomProvider codeGen)
{
StringWriter w = new StringWriter();
codeGen.GenerateCodeFromCompileUnit(this.compileUnit, w, null);
return w.ToString();
}
public static string GetDumpCode(Language language)
{
if (language == Language.CS)
{
return dumpClassCS;
}
if (language == Language.VB)
{
return dumpClassVB;
}
return ("*** Dump classes is not generated for " + language + " ***");
}
private static string GetUniqueVariableName(string name, CodeStatementCollection statements)
{
name = CodeIdentifier.MakeCamel(name);
foreach (CodeStatement statement in statements)
{
CodeVariableDeclarationStatement statement2 = statement as CodeVariableDeclarationStatement;
if ((statement2 != null) && (statement2.Name == name))
{
return (name + "_" + statements.Count);
}
}
return name;
}
public static string GetUsingCode(Language language)
{
if (language == Language.CS)
{
return usingStatementsCS;
}
if (language == Language.VB)
{
return usingStatementsVB;
}
return "";
}
private bool IsCLSCompliant(Type type)
{
CLSCompliantAttribute[] customAttributes = type.GetCustomAttributes(typeof(CLSCompliantAttribute), true) as CLSCompliantAttribute[];
if (customAttributes.Length != 1)
{
return false;
}
return customAttributes[0].IsCompliant;
}
public HttpWebClientProtocol Proxy
{
get
{
return this.proxy;
}
set
{
this.proxy = value;
}
}
private enum ProxySettings
{
RequiredHeaders,
AllHeaders,
AllProperties
}
}
}