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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
//------------------------------------------------------------------------------
// <copyright file="WebCodeGenerator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Services.Description {
using System;
using System.Globalization;
using System.Collections;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Threading;
using System.Web.Services.Protocols;
internal enum CodeFlags {
IsPublic = 0x1,
IsAbstract = 0x2,
IsStruct = 0x4,
IsNew = 0x8,
IsByRef = 0x10,
IsOut = 0x20,
IsInterface = 0x40
}
internal class WebCodeGenerator {
private static CodeAttributeDeclaration generatedCodeAttribute;
private WebCodeGenerator() { }
internal static CodeAttributeDeclaration GeneratedCodeAttribute {
get {
if (generatedCodeAttribute == null) {
CodeAttributeDeclaration decl = new CodeAttributeDeclaration(typeof(GeneratedCodeAttribute).FullName);
Assembly a = Assembly.GetEntryAssembly();
if (a == null) {
a = Assembly.GetExecutingAssembly();
if (a == null) {
a = typeof(WebCodeGenerator).Assembly;
}
}
AssemblyName assemblyName = a.GetName();
decl.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Name)));
string version = GetProductVersion(a);
decl.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(version == null ? assemblyName.Version.ToString() : version)));
generatedCodeAttribute = decl;
}
return generatedCodeAttribute;
}
}
private static string GetProductVersion(Assembly assembly) {
object[] attributes = assembly.GetCustomAttributes(true);
for ( int i = 0; i < attributes.Length; i++ ) {
if (attributes[i] is AssemblyInformationalVersionAttribute) {
AssemblyInformationalVersionAttribute version = (AssemblyInformationalVersionAttribute)attributes[i];
return version.InformationalVersion;
}
}
return null;
}
internal static string[] GetNamespacesForTypes(Type[] types) {
Hashtable names = new Hashtable();
for (int i = 0; i < types.Length; i++) {
string name = types[i].FullName;
int dot = name.LastIndexOf('.');
if (dot > 0)
names[name.Substring(0, dot)] = types[i];
}
string[] ns = new string[names.Keys.Count];
names.Keys.CopyTo(ns, 0);
return ns;
}
internal static void AddImports(CodeNamespace codeNamespace, string[] namespaces) {
Debug.Assert(codeNamespace != null, "Inalid (null) CodeNamespace");
foreach (string ns in namespaces)
codeNamespace.Imports.Add(new CodeNamespaceImport(ns));
}
static CodeMemberProperty CreatePropertyDeclaration(CodeMemberField field, string name, string typeName) {
CodeMemberProperty prop = new CodeMemberProperty();
prop.Type = new CodeTypeReference(typeName);
prop.Name = name;
//add get
CodeMethodReturnStatement ret = new CodeMethodReturnStatement();
ret.Expression = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name);
prop.GetStatements.Add(ret);
CodeExpression left = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name);
CodeExpression right = new CodeArgumentReferenceExpression("value");
prop.SetStatements.Add(new CodeAssignStatement(left, right));
return prop;
}
internal static CodeTypeMember AddMember(CodeTypeDeclaration codeClass, string typeName, string memberName, CodeExpression initializer, CodeAttributeDeclarationCollection metadata, CodeFlags flags, CodeGenerationOptions options) {
CodeTypeMember member;
bool generateProperty = (options & CodeGenerationOptions.GenerateProperties) != 0;
string fieldName = generateProperty ? MakeFieldName(memberName) : memberName;
CodeMemberField field = new CodeMemberField(typeName, fieldName);
field.InitExpression = initializer;
if (generateProperty) {
codeClass.Members.Add(field);
member = CreatePropertyDeclaration(field, memberName, typeName);
}
else {
member = field;
}
member.CustomAttributes = metadata;
if ((flags & CodeFlags.IsPublic) != 0)
member.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
codeClass.Members.Add(member);
return member;
}
internal static string FullTypeName(XmlMemberMapping mapping, CodeDomProvider codeProvider) {
return mapping.GenerateTypeName(codeProvider);
}
static string MakeFieldName(string name) {
return CodeIdentifier.MakeCamel(name) + "Field";
}
internal static CodeConstructor AddConstructor(CodeTypeDeclaration codeClass, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags) {
CodeConstructor ctor = new CodeConstructor();
if ((flags & CodeFlags.IsPublic) != 0)
ctor.Attributes = (ctor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
if ((flags & CodeFlags.IsAbstract) != 0)
ctor.Attributes |= MemberAttributes.Abstract;
ctor.CustomAttributes = metadata;
Debug.Assert(parameterTypeNames.Length == parameterNames.Length, "invalid set of parameters");
for (int i = 0; i < parameterTypeNames.Length; i++) {
CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
ctor.Parameters.Add(param);
}
codeClass.Members.Add(ctor);
return ctor;
}
internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName,
CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames,
string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags) {
return AddMethod(codeClass, methodName, parameterFlags,
parameterTypeNames, parameterNames, new CodeAttributeDeclarationCollection[0],
returnTypeName, metadata, flags);
}
internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName,
CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames,
CodeAttributeDeclarationCollection[] parameterAttributes, string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags) {
CodeMemberMethod method = new CodeMemberMethod();
method.Name = methodName;
method.ReturnType = new CodeTypeReference(returnTypeName);
method.CustomAttributes = metadata;
if ((flags & CodeFlags.IsPublic) != 0)
method.Attributes = (method.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
if ((flags & CodeFlags.IsAbstract) != 0)
method.Attributes = (method.Attributes & ~MemberAttributes.ScopeMask) | MemberAttributes.Abstract;
if ((flags & CodeFlags.IsNew) != 0)
method.Attributes = (method.Attributes & ~MemberAttributes.VTableMask) | MemberAttributes.New;
Debug.Assert(parameterFlags.Length == parameterTypeNames.Length && parameterTypeNames.Length == parameterNames.Length, "invalid set of parameters");
for (int i = 0; i < parameterNames.Length; i++) {
CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
if ((parameterFlags[i] & CodeFlags.IsByRef) != 0)
param.Direction = FieldDirection.Ref;
else if ((parameterFlags[i] & CodeFlags.IsOut) != 0)
param.Direction = FieldDirection.Out;
if (i < parameterAttributes.Length) {
param.CustomAttributes = parameterAttributes[i];
}
method.Parameters.Add(param);
}
codeClass.Members.Add(method);
return method;
}
internal static CodeTypeDeclaration AddClass(CodeNamespace codeNamespace, string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags, bool isPartial) {
CodeTypeDeclaration codeClass = CreateClass(className, baseClassName, implementedInterfaceNames, metadata, flags, isPartial);
codeNamespace.Types.Add(codeClass);
return codeClass;
}
internal static CodeTypeDeclaration CreateClass(string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags, bool isPartial) {
CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);
if (baseClassName != null && baseClassName.Length > 0)
codeClass.BaseTypes.Add(baseClassName);
foreach (string interfaceName in implementedInterfaceNames)
codeClass.BaseTypes.Add(interfaceName);
codeClass.IsStruct = (flags & CodeFlags.IsStruct) != 0;
if ((flags & CodeFlags.IsPublic) != 0)
codeClass.TypeAttributes |= TypeAttributes.Public;
else
codeClass.TypeAttributes &= ~TypeAttributes.Public;
if ((flags & CodeFlags.IsAbstract) != 0)
codeClass.TypeAttributes |= TypeAttributes.Abstract;
else
codeClass.TypeAttributes &= ~TypeAttributes.Abstract;
if ((flags & CodeFlags.IsInterface) != 0)
codeClass.IsInterface = true;
else
codeClass.IsPartial = isPartial;
codeClass.CustomAttributes = metadata;
codeClass.CustomAttributes.Add(GeneratedCodeAttribute);
return codeClass;
}
internal static CodeAttributeDeclarationCollection AddCustomAttribute(CodeAttributeDeclarationCollection metadata, Type type, CodeAttributeArgument[] arguments) {
if (metadata == null) metadata = new CodeAttributeDeclarationCollection();
CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(type.FullName, arguments);
metadata.Add(attribute);
return metadata;
}
internal static CodeAttributeDeclarationCollection AddCustomAttribute(CodeAttributeDeclarationCollection metadata, Type type, CodeExpression[] arguments) {
return AddCustomAttribute(metadata, type, arguments, new string[0], new CodeExpression[0]);
}
internal static CodeAttributeDeclarationCollection AddCustomAttribute(CodeAttributeDeclarationCollection metadata, Type type, CodeExpression[] parameters, string[] propNames, CodeExpression[] propValues) {
Debug.Assert(propNames.Length == propValues.Length, "propNames.Length != propValues.Length");
int count = (parameters == null ? 0 : parameters.Length) + (propNames == null ? 0 : propNames.Length);
CodeAttributeArgument[] arguments = new CodeAttributeArgument[count];
for (int i = 0; i < parameters.Length; i++)
arguments[i] = new CodeAttributeArgument(null, parameters[i]);
for (int i = 0; i < propNames.Length; i++)
arguments[parameters.Length + i] = new CodeAttributeArgument(propNames[i], propValues[i]);
return AddCustomAttribute(metadata, type, arguments);
}
// public event xxxCompletedEventHandler xxxCompleted;
internal static void AddEvent(CodeTypeMemberCollection members, string handlerType, string handlerName) {
CodeMemberEvent eventCompleted = new CodeMemberEvent();
eventCompleted.Type = new CodeTypeReference(handlerType);
eventCompleted.Name = handlerName;
eventCompleted.Attributes = (eventCompleted.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
eventCompleted.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));
members.Add(eventCompleted);
}
// public delegate void xxxCompletedEventHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs args);
internal static void AddDelegate(CodeTypeDeclarationCollection codeClasses, string handlerType, string handlerArgs) {
CodeTypeDelegate handler = new CodeTypeDelegate(handlerType);
handler.CustomAttributes.Add(GeneratedCodeAttribute);
handler.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "sender"));
handler.Parameters.Add(new CodeParameterDeclarationExpression(handlerArgs, "e"));
handler.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));
codeClasses.Add(handler);
}
// private SendOrPostCallback xxxOperationCompleted;
internal static void AddCallbackDeclaration(CodeTypeMemberCollection members, string callbackMember) {
CodeMemberField callback = new CodeMemberField();
callback.Type = new CodeTypeReference(typeof(SendOrPostCallback));
callback.Name = callbackMember;
members.Add(callback);
}
// private void On_xxx_OperationCompleted(object arg) {..}
internal static void AddCallbackImplementation(CodeTypeDeclaration codeClass, string callbackName, string handlerName, string handlerArgs, bool methodHasOutParameters) {
CodeMemberMethod asyncCompleted = WebCodeGenerator.AddMethod(codeClass, callbackName,
new CodeFlags[1] { 0 }, new string[] { typeof(object).FullName }, new string[] { "arg" },
typeof(void).FullName, null, 0);
CodeEventReferenceExpression member = new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName);
CodeBinaryOperatorExpression checkIfNull = new CodeBinaryOperatorExpression(member, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
CodeStatement[] trueStatements = new CodeStatement[2];
trueStatements[0] = new CodeVariableDeclarationStatement(typeof(InvokeCompletedEventArgs), "invokeArgs", new CodeCastExpression(typeof(InvokeCompletedEventArgs), new CodeArgumentReferenceExpression("arg")));
CodeVariableReferenceExpression invokeArgs = new CodeVariableReferenceExpression("invokeArgs");
CodeObjectCreateExpression create = new CodeObjectCreateExpression();
if (methodHasOutParameters) {
create.CreateType = new CodeTypeReference(handlerArgs);
create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "Results"));
}
else {
create.CreateType = new CodeTypeReference(typeof(AsyncCompletedEventArgs));
}
create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "Error"));
create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "Cancelled"));
create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "UserState"));
trueStatements[1] = new CodeExpressionStatement(new CodeDelegateInvokeExpression(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName), new CodeExpression[] { new CodeThisReferenceExpression(), create }));
asyncCompleted.Statements.Add(new CodeConditionStatement(checkIfNull, trueStatements, new CodeStatement[0]));
}
internal static CodeMemberMethod AddAsyncMethod(CodeTypeDeclaration codeClass, string methodName,
string[] parameterTypeNames, string[] parameterNames, string callbackMember, string callbackName, string userState) {
CodeMemberMethod asyncCodeMethod = WebCodeGenerator.AddMethod(codeClass, methodName,
new CodeFlags[parameterNames.Length], parameterTypeNames, parameterNames, typeof(void).FullName, null, CodeFlags.IsPublic);
asyncCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));
CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), methodName);
for (int i = 0; i < parameterNames.Length; i++) {
invoke.Parameters.Add(new CodeArgumentReferenceExpression(parameterNames[i]));
}
invoke.Parameters.Add(new CodePrimitiveExpression(null));
asyncCodeMethod.Statements.Add(invoke);
asyncCodeMethod = WebCodeGenerator.AddMethod(codeClass, methodName,
new CodeFlags[parameterNames.Length], parameterTypeNames, parameterNames, typeof(void).FullName, null, CodeFlags.IsPublic);
asyncCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));
asyncCodeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), userState));
CodeFieldReferenceExpression member = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), callbackMember);
CodeBinaryOperatorExpression checkIfNull = new CodeBinaryOperatorExpression(member, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));
CodeDelegateCreateExpression createDelegate = new CodeDelegateCreateExpression();
createDelegate.DelegateType = new CodeTypeReference(typeof(SendOrPostCallback));
createDelegate.TargetObject = new CodeThisReferenceExpression();
createDelegate.MethodName = callbackName;
CodeStatement[] trueStatements = new CodeStatement[] { new CodeAssignStatement(member, createDelegate) };
asyncCodeMethod.Statements.Add(new CodeConditionStatement(checkIfNull, trueStatements, new CodeStatement[0]));
return asyncCodeMethod;
}
internal static CodeTypeDeclaration CreateArgsClass(string name, string[] paramTypes, string[] paramNames, bool isPartial) {
CodeTypeDeclaration codeClass = new CodeTypeDeclaration(name);
codeClass.CustomAttributes.Add(GeneratedCodeAttribute);
// Add [DebuggerStepThrough]
codeClass.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DebuggerStepThroughAttribute).FullName));
// Add [DesignerCategory("code")]
codeClass.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(DesignerCategoryAttribute).FullName, new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("code")) }));
codeClass.IsPartial = isPartial;
codeClass.BaseTypes.Add(new CodeTypeReference(typeof(AsyncCompletedEventArgs)));
CodeIdentifiers identifiers = new CodeIdentifiers();
identifiers.AddUnique("Error", "Error");
identifiers.AddUnique("Cancelled", "Cancelled");
identifiers.AddUnique("UserState", "UserState");
for (int i = 0; i < paramNames.Length; i++) {
if (paramNames[i] != null) {
identifiers.AddUnique(paramNames[i], paramNames[i]);
}
}
string results = identifiers.AddUnique("results", "results");
CodeMemberField data = new CodeMemberField(typeof(object[]), results);
codeClass.Members.Add(data);
CodeConstructor ctor = new CodeConstructor();
ctor.Attributes = (ctor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Assembly;
CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(object[]), results);
ctor.Parameters.Add(param);
ctor.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Exception), "exception"));
ctor.Parameters.Add(new CodeParameterDeclarationExpression(typeof(bool), "cancelled"));
ctor.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "userState"));
ctor.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("exception"));
ctor.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("cancelled"));
ctor.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("userState"));
ctor.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), data.Name), new CodeArgumentReferenceExpression(results)));
codeClass.Members.Add(ctor);
int index = 0;
for (int i = 0; i < paramNames.Length; i++) {
if (paramNames[i] != null) {
codeClass.Members.Add(CreatePropertyDeclaration(data, paramNames[i], paramTypes[i], index++));
}
}
codeClass.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));
return codeClass;
}
static CodeMemberProperty CreatePropertyDeclaration(CodeMemberField field, string name, string typeName, int index) {
CodeMemberProperty prop = new CodeMemberProperty();
prop.Type = new CodeTypeReference(typeName);
prop.Name = name;
prop.Attributes = (prop.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
//add get
prop.GetStatements.Add(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "RaiseExceptionIfNecessary", new CodeExpression[0]));
CodeArrayIndexerExpression valueRef = new CodeArrayIndexerExpression();
valueRef.TargetObject = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name);
valueRef.Indices.Add(new CodePrimitiveExpression(index));
CodeMethodReturnStatement ret = new CodeMethodReturnStatement();
ret.Expression = new CodeCastExpression(typeName, valueRef);
prop.GetStatements.Add(ret);
prop.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));
return prop;
}
}
}
|