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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
//------------------------------------------------------------------------------
// <copyright file="Compilation.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Serialization {
using System.Configuration;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;
using System.IO;
using System;
using System.Text;
using System.Xml;
using System.Threading;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Xml.Serialization.Configuration;
using System.Diagnostics;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Runtime.Versioning;
using System.Diagnostics.CodeAnalysis;
internal class TempAssembly {
internal const string GeneratedAssemblyNamespace = "Microsoft.Xml.Serialization.GeneratedAssembly";
Assembly assembly;
bool pregeneratedAssmbly = false;
XmlSerializerImplementation contract = null;
Hashtable writerMethods;
Hashtable readerMethods;
TempMethodDictionary methods;
static object[] emptyObjectArray = new object[0];
Hashtable assemblies = new Hashtable();
static volatile FileIOPermission fileIOPermission;
internal class TempMethod {
internal MethodInfo writeMethod;
internal MethodInfo readMethod;
internal string name;
internal string ns;
internal bool isSoap;
internal string methodKey;
}
private TempAssembly() {
}
internal TempAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, string location, Evidence evidence) {
bool containsSoapMapping = false;
for (int i = 0; i < xmlMappings.Length; i++) {
xmlMappings[i].CheckShallow();
if (xmlMappings[i].IsSoap) {
containsSoapMapping = true;
}
}
// We will make best effort to use RefEmit for assembly generation
bool fallbackToCSharpAssemblyGeneration = false;
if (!containsSoapMapping && !TempAssembly.UseLegacySerializerGeneration) {
try {
assembly = GenerateRefEmitAssembly(xmlMappings, types, defaultNamespace, evidence);
}
// Only catch and handle known failures with RefEmit
catch (CodeGeneratorConversionException) {
fallbackToCSharpAssemblyGeneration = true;
}
// Add other known exceptions here...
//
}
else {
fallbackToCSharpAssemblyGeneration = true;
}
if (fallbackToCSharpAssemblyGeneration) {
assembly = GenerateAssembly(xmlMappings, types, defaultNamespace, evidence, XmlSerializerCompilerParameters.Create(location), null, assemblies);
}
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (assembly == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Failed to generate XmlSerializer assembly, but did not throw"));
#endif
InitAssemblyMethods(xmlMappings);
}
internal TempAssembly(XmlMapping[] xmlMappings, Assembly assembly, XmlSerializerImplementation contract) {
this.assembly = assembly;
InitAssemblyMethods(xmlMappings);
this.contract = contract;
pregeneratedAssmbly = true;
}
internal static bool UseLegacySerializerGeneration {
get {
if (AppSettings.UseLegacySerializerGeneration.HasValue) {
// AppSetting will always win if specified
return (bool) AppSettings.UseLegacySerializerGeneration;
}
else {
#if CONFIGURATION_DEP
XmlSerializerSection configSection = ConfigurationManager.GetSection(ConfigurationStrings.XmlSerializerSectionPath) as XmlSerializerSection;
return configSection == null ? false : configSection.UseLegacySerializerGeneration;
#else
return false;
#endif
}
}
}
internal TempAssembly(XmlSerializerImplementation contract) {
this.contract = contract;
pregeneratedAssmbly = true;
}
internal XmlSerializerImplementation Contract {
get {
if (contract == null) {
contract = (XmlSerializerImplementation)Activator.CreateInstance(GetTypeFromAssembly(this.assembly, "XmlSerializerContract"));
}
return contract;
}
}
internal void InitAssemblyMethods(XmlMapping[] xmlMappings) {
methods = new TempMethodDictionary();
for (int i = 0; i < xmlMappings.Length; i++) {
TempMethod method = new TempMethod();
method.isSoap = xmlMappings[i].IsSoap;
method.methodKey = xmlMappings[i].Key;
XmlTypeMapping xmlTypeMapping = xmlMappings[i] as XmlTypeMapping;
if (xmlTypeMapping != null) {
method.name = xmlTypeMapping.ElementName;
method.ns = xmlTypeMapping.Namespace;
}
methods.Add(xmlMappings[i].Key, method);
}
}
/// <devdoc>
/// <para>
/// Attempts to load pre-generated serialization assembly.
/// First check for the [XmlSerializerAssembly] attribute
/// </para>
/// </devdoc>
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract) {
Assembly serializer = null;
contract = null;
string serializerName = null;
// Packaged apps do not support loading generated serializers.
if (Microsoft.Win32.UnsafeNativeMethods.IsPackagedProcess.Value) {
return null;
}
bool logEnabled = DiagnosticsSwitches.PregenEventLog.Enabled;
// check to see if we loading explicit pre-generated assembly
object[] attrs = type.GetCustomAttributes(typeof(XmlSerializerAssemblyAttribute), false);
if (attrs.Length == 0) {
// Guess serializer name: if parent assembly signed use strong name
AssemblyName name = GetName(type.Assembly, true);
serializerName = Compiler.GetTempAssemblyName(name, defaultNamespace);
// use strong name
name.Name = serializerName;
name.CodeBase = null;
name.CultureInfo = CultureInfo.InvariantCulture;
try {
serializer = Assembly.Load(name);
}
catch (Exception e) {
if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
throw;
}
if (logEnabled) {
Log(e.Message, EventLogEntryType.Information);
}
byte[] token = name.GetPublicKeyToken();
if (token != null && token.Length > 0) {
// the parent assembly was signed, so do not try to LoadWithPartialName
return null;
}
#pragma warning disable 618
serializer = Assembly.LoadWithPartialName(serializerName, null);
#pragma warning restore 618
}
if (serializer == null) {
#if !FEATURE_PAL // EventLog
if (logEnabled) {
Log(Res.GetString(Res.XmlPregenCannotLoad, serializerName), EventLogEntryType.Information);
}
#endif //!FEATURE_PAL // EventLog
return null;
}
if (!IsSerializerVersionMatch(serializer, type, defaultNamespace, null)) {
#if !FEATURE_PAL // EventLog
if (logEnabled)
Log(Res.GetString(Res.XmlSerializerExpiredDetails, serializerName, type.FullName), EventLogEntryType.Error);
#endif //!FEATURE_PAL // EventLog
return null;
}
}
else {
XmlSerializerAssemblyAttribute assemblyAttribute = (XmlSerializerAssemblyAttribute)attrs[0];
if (assemblyAttribute.AssemblyName != null && assemblyAttribute.CodeBase != null)
throw new InvalidOperationException(Res.GetString(Res.XmlPregenInvalidXmlSerializerAssemblyAttribute, "AssemblyName", "CodeBase"));
// found XmlSerializerAssemblyAttribute attribute, it should have all needed information to load the pre-generated serializer
if (assemblyAttribute.AssemblyName != null) {
serializerName = assemblyAttribute.AssemblyName;
#pragma warning disable 618
serializer = Assembly.LoadWithPartialName(serializerName, null);
#pragma warning restore 618
}
else if (assemblyAttribute.CodeBase != null && assemblyAttribute.CodeBase.Length > 0) {
serializerName = assemblyAttribute.CodeBase;
serializer = Assembly.LoadFrom(serializerName);
}
else {
serializerName = type.Assembly.FullName;
serializer = type.Assembly;
}
if (serializer == null) {
throw new FileNotFoundException(null, serializerName);
}
}
Type contractType = GetTypeFromAssembly(serializer, "XmlSerializerContract");
contract = (XmlSerializerImplementation)Activator.CreateInstance(contractType);
if (contract.CanSerialize(type))
return serializer;
#if !FEATURE_PAL // EventLog
if (logEnabled)
Log(Res.GetString(Res.XmlSerializerExpiredDetails, serializerName, type.FullName), EventLogEntryType.Error);
#endif //!FEATURE_PAL // EventLog
return null;
}
#if !FEATURE_PAL // EventLog
static void Log(string message, EventLogEntryType type) {
new EventLogPermission(PermissionState.Unrestricted).Assert();
EventLog.WriteEntry("XmlSerializer", message, type);
}
#endif //!FEATURE_PAL // EventLog
static AssemblyName GetName(Assembly assembly, bool copyName) {
PermissionSet perms = new PermissionSet(PermissionState.None);
perms.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
perms.Assert();
return assembly.GetName(copyName);
}
static bool IsSerializerVersionMatch(Assembly serializer, Type type, string defaultNamespace, string location) {
if (serializer == null)
return false;
object[] attrs = serializer.GetCustomAttributes(typeof(XmlSerializerVersionAttribute), false);
if (attrs.Length != 1)
return false;
XmlSerializerVersionAttribute assemblyInfo = (XmlSerializerVersionAttribute)attrs[0];
// we found out dated pre-generate assembly
//
if (assemblyInfo.ParentAssemblyId == GenerateAssemblyId(type) && assemblyInfo.Namespace == defaultNamespace)
return true;
return false;
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
static string GenerateAssemblyId(Type type) {
Module[] modules = type.Assembly.GetModules();
ArrayList list = new ArrayList();
for (int i = 0; i < modules.Length; i++) {
list.Add(modules[i].ModuleVersionId.ToString());
}
list.Sort();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++) {
sb.Append(list[i].ToString());
sb.Append(",");
}
return sb.ToString();
}
internal static Assembly GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies) {
FileIOPermission.Assert();
Compiler compiler = new Compiler();
try {
Hashtable scopeTable = new Hashtable();
foreach (XmlMapping mapping in xmlMappings)
scopeTable[mapping.Scope] = mapping;
TypeScope[] scopes = new TypeScope[scopeTable.Keys.Count];
scopeTable.Keys.CopyTo(scopes, 0);
assemblies.Clear();
Hashtable importedTypes = new Hashtable();
foreach (TypeScope scope in scopes) {
foreach (Type t in scope.Types) {
compiler.AddImport(t, importedTypes);
Assembly a = t.Assembly;
string name = a.FullName;
if (assemblies[name] != null)
continue;
if (!a.GlobalAssemblyCache) {
assemblies[name] = a;
}
}
}
for (int i = 0; i < types.Length; i++) {
compiler.AddImport(types[i], importedTypes);
}
compiler.AddImport(typeof(object).Assembly);
compiler.AddImport(typeof(XmlSerializer).Assembly);
IndentedWriter writer = new IndentedWriter(compiler.Source, false);
writer.WriteLine("#if _DYNAMIC_XMLSERIALIZER_COMPILATION");
writer.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]");
writer.WriteLine("[assembly:System.Security.SecurityTransparent()]");
writer.WriteLine("[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]");
writer.WriteLine("#endif");
// Add AssemblyVersion attribute to match parent accembly version
if (types != null && types.Length > 0 && types[0] != null) {
writer.WriteLine("[assembly:System.Reflection.AssemblyVersionAttribute(\"" + types[0].Assembly.GetName().Version.ToString() + "\")]");
}
if (assembly != null && types.Length > 0) {
for (int i = 0; i < types.Length; i++) {
Type type = types[i];
if (type == null)
continue;
if (DynamicAssemblies.IsTypeDynamic(type)) {
throw new InvalidOperationException(Res.GetString(Res.XmlPregenTypeDynamic, types[i].FullName));
}
}
writer.Write("[assembly:");
writer.Write(typeof(XmlSerializerVersionAttribute).FullName);
writer.Write("(");
writer.Write("ParentAssemblyId=");
ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, GenerateAssemblyId(types[0]));
writer.Write(", Version=");
ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, ThisAssembly.Version);
if (defaultNamespace != null) {
writer.Write(", Namespace=");
ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, defaultNamespace);
}
writer.WriteLine(")]");
}
CodeIdentifiers classes = new CodeIdentifiers();
classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
classes.AddUnique("XmlSerializationReader", "XmlSerializationReader");
string suffix = null;
if (types != null && types.Length == 1 && types[0] != null) {
suffix = CodeIdentifier.MakeValid(types[0].Name);
if (types[0].IsArray) {
suffix += "Array";
}
}
writer.WriteLine("namespace " + GeneratedAssemblyNamespace + " {");
writer.Indent++;
writer.WriteLine();
string writerClass = "XmlSerializationWriter" + suffix;
writerClass = classes.AddUnique(writerClass, writerClass);
XmlSerializationWriterCodeGen writerCodeGen = new XmlSerializationWriterCodeGen(writer, scopes, "public", writerClass);
writerCodeGen.GenerateBegin();
string[] writeMethodNames = new string[xmlMappings.Length];
for (int i = 0; i < xmlMappings.Length; i++) {
writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]);
}
writerCodeGen.GenerateEnd();
writer.WriteLine();
string readerClass = "XmlSerializationReader" + suffix;
readerClass = classes.AddUnique(readerClass, readerClass);
XmlSerializationReaderCodeGen readerCodeGen = new XmlSerializationReaderCodeGen(writer, scopes, "public", readerClass);
readerCodeGen.GenerateBegin();
string[] readMethodNames = new string[xmlMappings.Length];
for (int i = 0; i < xmlMappings.Length; i++) {
readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]);
}
readerCodeGen.GenerateEnd(readMethodNames, xmlMappings, types);
string baseSerializer = readerCodeGen.GenerateBaseSerializer("XmlSerializer1", readerClass, writerClass, classes);
Hashtable serializers = new Hashtable();
for (int i = 0; i < xmlMappings.Length; i++) {
if (serializers[xmlMappings[i].Key] == null) {
serializers[xmlMappings[i].Key] = readerCodeGen.GenerateTypedSerializer(readMethodNames[i], writeMethodNames[i], xmlMappings[i], classes, baseSerializer, readerClass, writerClass);
}
}
readerCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, readerClass, readMethodNames, writerClass, writeMethodNames, serializers);
writer.Indent--;
writer.WriteLine("}");
return compiler.Compile(assembly, defaultNamespace, parameters, evidence);
}
finally {
compiler.Close();
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts", Justification="It is safe because the serialization assembly is generated by the framework code, not by the user.")]
internal static Assembly GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Evidence evidence) {
Hashtable scopeTable = new Hashtable();
foreach (XmlMapping mapping in xmlMappings)
scopeTable[mapping.Scope] = mapping;
TypeScope[] scopes = new TypeScope[scopeTable.Keys.Count];
scopeTable.Keys.CopyTo(scopes, 0);
string assemblyName = "Microsoft.GeneratedCode";
AssemblyBuilder assemblyBuilder = CodeGenerator.CreateAssemblyBuilder(AppDomain.CurrentDomain, assemblyName);
ConstructorInfo SecurityTransparentAttribute_ctor = typeof(SecurityTransparentAttribute).GetConstructor(
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(SecurityTransparentAttribute_ctor, new Object[0]));
ConstructorInfo AllowPartiallyTrustedCallersAttribute_ctor = typeof(AllowPartiallyTrustedCallersAttribute).GetConstructor(
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(AllowPartiallyTrustedCallersAttribute_ctor, new Object[0]));
ConstructorInfo SecurityRulesAttribute_ctor = typeof(SecurityRulesAttribute).GetConstructor(
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { typeof(SecurityRuleSet) },
null
);
assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(SecurityRulesAttribute_ctor, new Object[] { SecurityRuleSet.Level1 }));
// Add AssemblyVersion attribute to match parent accembly version
if (types != null && types.Length > 0 && types[0] != null) {
ConstructorInfo AssemblyVersionAttribute_ctor = typeof(AssemblyVersionAttribute).GetConstructor(
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { typeof(String) },
null
);
FileIOPermission.Assert();
string assemblyVersion = types[0].Assembly.GetName().Version.ToString();
FileIOPermission.RevertAssert();
assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(AssemblyVersionAttribute_ctor, new Object[] { assemblyVersion }));
}
CodeIdentifiers classes = new CodeIdentifiers();
classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
classes.AddUnique("XmlSerializationReader", "XmlSerializationReader");
string suffix = null;
if (types != null && types.Length == 1 && types[0] != null) {
suffix = CodeIdentifier.MakeValid(types[0].Name);
if (types[0].IsArray) {
suffix += "Array";
}
}
ModuleBuilder moduleBuilder = CodeGenerator.CreateModuleBuilder(assemblyBuilder, assemblyName);
string writerClass = "XmlSerializationWriter" + suffix;
writerClass = classes.AddUnique(writerClass, writerClass);
XmlSerializationWriterILGen writerCodeGen = new XmlSerializationWriterILGen(scopes, "public", writerClass);
writerCodeGen.ModuleBuilder = moduleBuilder;
writerCodeGen.GenerateBegin();
string[] writeMethodNames = new string[xmlMappings.Length];
for (int i = 0; i < xmlMappings.Length; i++) {
writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]);
}
Type writerType = writerCodeGen.GenerateEnd();
string readerClass = "XmlSerializationReader" + suffix;
readerClass = classes.AddUnique(readerClass, readerClass);
XmlSerializationReaderILGen readerCodeGen = new XmlSerializationReaderILGen(scopes, "public", readerClass);
readerCodeGen.ModuleBuilder = moduleBuilder;
readerCodeGen.CreatedTypes.Add(writerType.Name, writerType);
readerCodeGen.GenerateBegin();
string[] readMethodNames = new string[xmlMappings.Length];
for (int i = 0; i < xmlMappings.Length; i++) {
readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]);
}
readerCodeGen.GenerateEnd(readMethodNames, xmlMappings, types);
string baseSerializer = readerCodeGen.GenerateBaseSerializer("XmlSerializer1", readerClass, writerClass, classes);
Hashtable serializers = new Hashtable();
for (int i = 0; i < xmlMappings.Length; i++) {
if (serializers[xmlMappings[i].Key] == null) {
serializers[xmlMappings[i].Key] = readerCodeGen.GenerateTypedSerializer(readMethodNames[i], writeMethodNames[i], xmlMappings[i], classes, baseSerializer, readerClass, writerClass);
}
}
readerCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, readerClass, readMethodNames, writerClass, writeMethodNames, serializers);
if (DiagnosticsSwitches.KeepTempFiles.Enabled) {
FileIOPermission.Assert();
assemblyBuilder.Save(assemblyName + ".dll");
}
return writerType.Assembly;
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
static MethodInfo GetMethodFromType(Type type, string methodName, Assembly assembly) {
MethodInfo method = type.GetMethod(methodName);
if (method != null)
return method;
MissingMethodException missingMethod = new MissingMethodException(type.FullName, methodName);
if (assembly != null) {
throw new InvalidOperationException(Res.GetString(Res.XmlSerializerExpired, assembly.FullName, assembly.CodeBase), missingMethod);
}
throw missingMethod;
}
internal static Type GetTypeFromAssembly(Assembly assembly, string typeName) {
typeName = GeneratedAssemblyNamespace + "." + typeName;
Type type = assembly.GetType(typeName);
if (type == null) throw new InvalidOperationException(Res.GetString(Res.XmlMissingType, typeName, assembly.FullName));
return type;
}
internal bool CanRead(XmlMapping mapping, XmlReader xmlReader) {
if (mapping == null)
return false;
if (mapping.Accessor.Any) {
return true;
}
TempMethod method = methods[mapping.Key];
return xmlReader.IsStartElement(method.name, method.ns);
}
string ValidateEncodingStyle(string encodingStyle, string methodKey) {
if (encodingStyle != null && encodingStyle.Length > 0) {
if (methods[methodKey].isSoap) {
if (encodingStyle != Soap.Encoding && encodingStyle != Soap12.Encoding) {
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidEncoding3, encodingStyle, Soap.Encoding, Soap12.Encoding));
}
}
else {
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidEncodingNotEncoded1, encodingStyle));
}
}
else {
if (methods[methodKey].isSoap) {
encodingStyle = Soap.Encoding;
}
}
return encodingStyle;
}
internal static FileIOPermission FileIOPermission {
get {
if (fileIOPermission == null)
fileIOPermission = new FileIOPermission(PermissionState.Unrestricted);
return fileIOPermission;
}
}
internal object InvokeReader(XmlMapping mapping, XmlReader xmlReader, XmlDeserializationEvents events, string encodingStyle) {
XmlSerializationReader reader = null;
try {
encodingStyle = ValidateEncodingStyle(encodingStyle, mapping.Key);
reader = Contract.Reader;
reader.Init(xmlReader, events, encodingStyle, this);
if (methods[mapping.Key].readMethod == null) {
if (readerMethods == null) {
readerMethods = Contract.ReadMethods;
}
string methodName = (string)readerMethods[mapping.Key];
if (methodName == null) {
throw new InvalidOperationException(Res.GetString(Res.XmlNotSerializable, mapping.Accessor.Name));
}
methods[mapping.Key].readMethod = GetMethodFromType(reader.GetType(), methodName, pregeneratedAssmbly ? this.assembly : null);
}
return methods[mapping.Key].readMethod.Invoke(reader, emptyObjectArray);
}
catch (SecurityException e) {
throw new InvalidOperationException(Res.GetString(Res.XmlNoPartialTrust), e);
}
finally {
if (reader != null)
reader.Dispose();
}
}
internal void InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle, string id) {
XmlSerializationWriter writer = null;
try {
encodingStyle = ValidateEncodingStyle(encodingStyle, mapping.Key);
writer = Contract.Writer;
writer.Init(xmlWriter, namespaces, encodingStyle, id, this);
if (methods[mapping.Key].writeMethod == null) {
if (writerMethods == null) {
writerMethods = Contract.WriteMethods;
}
string methodName = (string)writerMethods[mapping.Key];
if (methodName == null) {
throw new InvalidOperationException(Res.GetString(Res.XmlNotSerializable, mapping.Accessor.Name));
}
methods[mapping.Key].writeMethod = GetMethodFromType(writer.GetType(), methodName, pregeneratedAssmbly ? assembly : null);
}
methods[mapping.Key].writeMethod.Invoke(writer, new object[] { o });
}
catch (SecurityException e) {
throw new InvalidOperationException(Res.GetString(Res.XmlNoPartialTrust), e);
}
finally {
if (writer != null)
writer.Dispose();
}
}
internal Assembly GetReferencedAssembly(string name) {
return assemblies != null && name != null ? (Assembly)assemblies[name] : null;
}
internal bool NeedAssembyResolve {
get { return assemblies != null && assemblies.Count > 0; }
}
internal sealed class TempMethodDictionary : DictionaryBase {
internal TempMethod this[string key] {
get {
return (TempMethod)Dictionary[key];
}
}
internal void Add(string key, TempMethod value) {
Dictionary.Add(key, value);
}
}
}
sealed class XmlSerializerCompilerParameters {
bool needTempDirAccess;
CompilerParameters parameters;
XmlSerializerCompilerParameters(CompilerParameters parameters, bool needTempDirAccess) {
this.needTempDirAccess = needTempDirAccess;
this.parameters = parameters;
}
internal bool IsNeedTempDirAccess { get { return this.needTempDirAccess; } }
internal CompilerParameters CodeDomParameters { get { return this.parameters; } }
internal static XmlSerializerCompilerParameters Create(string location) {
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
if (string.IsNullOrEmpty(location)) {
#if CONFIGURATION_DEP
XmlSerializerSection configSection = ConfigurationManager.GetSection(ConfigurationStrings.XmlSerializerSectionPath) as XmlSerializerSection;
location = configSection == null ? location : configSection.TempFilesLocation;
#endif
// Trim leading and trailing white spaces (VSWhidbey 229873)
if (!string.IsNullOrEmpty(location)) {
location = location.Trim();
}
}
parameters.TempFiles = new TempFileCollection(location);
return new XmlSerializerCompilerParameters(parameters, string.IsNullOrEmpty(location));
}
internal static XmlSerializerCompilerParameters Create(CompilerParameters parameters, bool needTempDirAccess) {
return new XmlSerializerCompilerParameters(parameters, needTempDirAccess);
}
}
class TempAssemblyCacheKey {
string ns;
object type;
internal TempAssemblyCacheKey(string ns, object type) {
this.type = type;
this.ns = ns;
}
public override bool Equals(object o) {
TempAssemblyCacheKey key = o as TempAssemblyCacheKey;
if (key == null) return false;
return (key.type == this.type && key.ns == this.ns);
}
public override int GetHashCode() {
return ((ns != null ? ns.GetHashCode() : 0) ^ (type != null ? type.GetHashCode() : 0));
}
}
internal class TempAssemblyCache {
Hashtable cache = new Hashtable();
internal TempAssembly this[string ns, object o] {
get { return (TempAssembly)cache[new TempAssemblyCacheKey(ns, o)]; }
}
internal void Add(string ns, object o, TempAssembly assembly) {
TempAssemblyCacheKey key = new TempAssemblyCacheKey(ns, o);
lock (this) {
if (cache[key] == assembly) return;
Hashtable clone = new Hashtable();
foreach (object k in cache.Keys) {
clone.Add(k, cache[k]);
}
cache = clone;
cache[key] = assembly;
}
}
}
}
|