Imported Upstream version 5.4.0.167

Former-commit-id: 5624ac747d633e885131e8349322922b6a59baaa
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-08-21 15:34:15 +00:00
parent e49d6f06c0
commit 536cd135cc
12856 changed files with 563812 additions and 223249 deletions

View File

@@ -0,0 +1,38 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.XmlSerializer.Generator.Tests", "tests\Microsoft.XmlSerializer.Generator.Tests.csproj", "{0D1E2954-A5C7-4B8C-932A-31EB4A96A737}"
ProjectSection(ProjectDependencies) = postProject
{80958E8B-2FEB-4F95-83F9-825CA1ED26F8} = {80958E8B-2FEB-4F95-83F9-825CA1ED26F8}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.XmlSerializer.Generator", "src\Microsoft.XmlSerializer.Generator.csproj", "{80958E8B-2FEB-4F95-83F9-825CA1ED26F8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1A2F9F4A-A032-433E-B914-ADD5992BB178}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E893-4E87-987E-04EF0DCEAEFD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0D1E2954-A5C7-4B8C-932A-31EB4A96A737}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A737}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A737}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A737}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU
{80958E8B-2FEB-4F95-83F9-825CA1ED26F8}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{80958E8B-2FEB-4F95-83F9-825CA1ED26F8}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{80958E8B-2FEB-4F95-83F9-825CA1ED26F8}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{80958E8B-2FEB-4F95-83F9-825CA1ED26F8}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0D1E2954-A5C7-4B8C-932A-31EB4A96A737} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
{80958E8B-2FEB-4F95-83F9-825CA1ED26F8} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.props" />
<PropertyGroup>
<PackageVersion>1.0.0</PackageVersion>
<SkipValidatePackage>true</SkipValidatePackage>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyKey>Open</AssemblyKey>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<ProjectReference Include="..\src\Microsoft.XmlSerializer.Generator.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,170 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{80958E8B-2FEB-4F95-83F9-825CA1ED26F8}</ProjectGuid>
<AssemblyName>Microsoft.XmlSerializer.Generator</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<StringResourcesPath>..\..\System.Private.Xml\src\Resources\Strings.resx</StringResourcesPath>
<DefineConstants>$(DefineConstants);XMLSERIALIZERGENERATOR</DefineConstants>
<SourceDir>..\..\System.Private.Xml\src</SourceDir>
</PropertyGroup>
<!-- Default configurations to help VS understand the options -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Release|AnyCPU'" />
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard'">
<Compile Include="$(CommonPath)\System\CSharpHelpers.cs" />
<Compile Include="$(CommonPath)\System\LocalAppContext.cs" />
<Compile Include="$(SourceDir)\System\Xml\BinHexDecoder.cs">
<Link>System\Xml\BinHexDecoder.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\BinHexEncoder.cs">
<Link>System\Xml\BinHexEncoder.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Core\LocalAppContextSwitches.cs">
<Link>System\Xml\Core\LocalAppContextSwitches.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Extensions\ExtensionMethods.cs">
<Link>System\Xml\Extensions\ExtensionMethods.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\CodeGenerationoptions.cs">
<Link>System\Xml\Serialization\CodeGenerationoptions.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\CodeIdentifier.cs">
<Link>System\Xml\Serialization\CodeIdentifier.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\CodeIdentifiers.cs">
<Link>System\Xml\Serialization\CodeIdentifiers.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Compilation.cs">
<Link>System\Xml\Serialization\Compilation.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Compiler.cs">
<Link>System\Xml\Serialization\Compiler.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Configuration\DateTimeSerializationSection.cs">
<Link>System\Xml\Serialization\Configuration\DateTimeSerializationSection.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Globals.cs">
<Link>System\Xml\Serialization\Globals.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\indentedWriter.cs">
<Link>System\Xml\Serialization\indentedWriter.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\IXmlTextParser.cs">
<Link>System\Xml\Serialization\IXmlTextParser.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Mappings.cs">
<Link>System\Xml\Serialization\Mappings.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Models.cs">
<Link>System\Xml\Serialization\Models.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\NameTable.cs">
<Link>System\Xml\Serialization\NameTable.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\PrimitiveXmlSerializers.cs">
<Link>System\Xml\Serialization\PrimitiveXmlSerializers.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapReflectionImporter.cs">
<Link>System\Xml\Serialization\SoapReflectionImporter.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapSchemamember.cs">
<Link>System\Xml\Serialization\SoapSchemamember.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\TypeCode.cs">
<Link>System\Xml\Serialization\TypeCode.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\TypeExtensions.cs">
<Link>System\Xml\Serialization\TypeExtensions.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Types.cs">
<Link>System\Xml\Serialization\Types.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAttributeOverrides.cs">
<Link>System\Xml\Serialization\XmlAttributeOverrides.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAttributes.cs">
<Link>System\Xml\Serialization\XmlAttributes.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlCountingReader.cs">
<Link>System\Xml\Serialization\XmlCountingReader.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\Xmlcustomformatter.cs">
<Link>System\Xml\Serialization\Xmlcustomformatter.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlMapping.cs">
<Link>System\Xml\Serialization\XmlMapping.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlMemberMapping.cs">
<Link>System\Xml\Serialization\XmlMemberMapping.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlMembersMapping.cs">
<Link>System\Xml\Serialization\XmlMembersMapping.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlReflectionImporter.cs">
<Link>System\Xml\Serialization\XmlReflectionImporter.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlReflectionMember.cs">
<Link>System\Xml\Serialization\XmlReflectionMember.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSchemaProviderAttribute.cs">
<Link>System\Xml\Serialization\XmlSchemaProviderAttribute.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSerializationGeneratedCode.cs">
<Link>System\Xml\Serialization\XmlSerializationGeneratedCode.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSerializationReader.cs">
<Link>System\Xml\Serialization\XmlSerializationReader.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSerializationWriter.cs">
<Link>System\Xml\Serialization\XmlSerializationWriter.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSerializer.cs">
<Link>System\Xml\Serialization\XmlSerializer.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSerializerFactory.cs">
<Link>System\Xml\Serialization\XmlSerializerFactory.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSerializerNamespaces.cs">
<Link>System\Xml\Serialization\XmlSerializerNamespaces.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlTextAttribute.cs">
<Link>System\Xml\Serialization\XmlTextAttribute.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlTypeAttribute.cs">
<Link>System\Xml\Serialization\XmlTypeAttribute.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlTypeMapping.cs">
<Link>System\Xml\Serialization\XmlTypeMapping.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\Serialization\_Events.cs">
<Link>System\Xml\Serialization\_Events.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\XmlCharType.cs">
<Link>System\Xml\XmlCharType.cs</Link>
</Compile>
<Compile Include="$(SourceDir)\System\Xml\XmlReservedNs.cs">
<Link>System\Xml\XmlReservedNs.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Microsoft\XmlSerializer\Generator\XmlSerializationGeneratedCode.cs" />
<Compile Include="Microsoft\XmlSerializer\Generator\XmlSerializationReader.cs" />
<Compile Include="Microsoft\XmlSerializer\Generator\XmlSerializationWriter.cs" />
<Compile Include="Sgen.cs" />
<Compile Include="System\Xml\Serialization\SoapAttributesExtensions.cs" />
<Compile Include="System\Xml\Serialization\XmlAnyElementAttributeExtensions.cs" />
<Compile Include="System\Xml\Serialization\XmlArrayItemAttributeExtensions.cs" />
<Compile Include="System\Xml\Serialization\XmlChoiceIdentifierAttributeExtensions.cs" />
<Compile Include="System\Xml\Serialization\XmlElementAttributeExtension.cs" />
<Compile Include="System\Xml\Serialization\XmlRootAttributeExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Resources.ResourceManager" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\$(AssemblyName).rd.xml" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,399 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.XmlSerializer.Generator
{
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Reflection;
using System.Security;
using System.Globalization;
using System.Xml;
using System.Xml.Serialization;
internal class XmlSerializationCodeGen
{
private IndentedWriter _writer;
private int _nextMethodNumber = 0;
private Hashtable _methodNames = new Hashtable();
private ReflectionAwareCodeGen _raCodeGen;
private TypeScope[] _scopes;
private TypeDesc _stringTypeDesc = null;
private TypeDesc _qnameTypeDesc = null;
private string _access;
private string _className;
private TypeMapping[] _referencedMethods;
private int _references = 0;
private Hashtable _generatedMethods = new Hashtable();
internal XmlSerializationCodeGen(IndentedWriter writer, TypeScope[] scopes, string access, string className)
{
_writer = writer;
_scopes = scopes;
if (scopes.Length > 0)
{
_stringTypeDesc = scopes[0].GetTypeDesc(typeof(string));
_qnameTypeDesc = scopes[0].GetTypeDesc(typeof(XmlQualifiedName));
}
_raCodeGen = new ReflectionAwareCodeGen(writer);
_className = className;
_access = access;
}
internal IndentedWriter Writer { get { return _writer; } }
internal int NextMethodNumber { get { return _nextMethodNumber; } set { _nextMethodNumber = value; } }
internal ReflectionAwareCodeGen RaCodeGen { get { return _raCodeGen; } }
internal TypeDesc StringTypeDesc { get { return _stringTypeDesc; } }
internal TypeDesc QnameTypeDesc { get { return _qnameTypeDesc; } }
internal string ClassName { get { return _className; } }
internal string Access { get { return _access; } }
internal TypeScope[] Scopes { get { return _scopes; } }
internal Hashtable MethodNames { get { return _methodNames; } }
internal Hashtable GeneratedMethods { get { return _generatedMethods; } }
internal virtual void GenerateMethod(TypeMapping mapping) { }
internal void GenerateReferencedMethods()
{
while (_references > 0)
{
TypeMapping mapping = _referencedMethods[--_references];
GenerateMethod(mapping);
}
}
internal string ReferenceMapping(TypeMapping mapping)
{
if (!mapping.IsSoap)
{
if (_generatedMethods[mapping] == null)
{
_referencedMethods = EnsureArrayIndex(_referencedMethods, _references);
_referencedMethods[_references++] = mapping;
}
}
return (string)_methodNames[mapping];
}
private TypeMapping[] EnsureArrayIndex(TypeMapping[] a, int index)
{
if (a == null) return new TypeMapping[32];
if (index < a.Length) return a;
TypeMapping[] b = new TypeMapping[a.Length + 32];
Array.Copy(a, b, index);
return b;
}
internal void WriteQuotedCSharpString(string value)
{
_raCodeGen.WriteQuotedCSharpString(value);
}
internal void GenerateHashtableGetBegin(string privateName, string publicName)
{
_writer.Write(typeof(Hashtable).FullName);
_writer.Write(" ");
_writer.Write(privateName);
_writer.WriteLine(" = null;");
_writer.Write("public override ");
_writer.Write(typeof(Hashtable).FullName);
_writer.Write(" ");
_writer.Write(publicName);
_writer.WriteLine(" {");
_writer.Indent++;
_writer.WriteLine("get {");
_writer.Indent++;
_writer.Write("if (");
_writer.Write(privateName);
_writer.WriteLine(" == null) {");
_writer.Indent++;
_writer.Write(typeof(Hashtable).FullName);
_writer.Write(" _tmp = new ");
_writer.Write(typeof(Hashtable).FullName);
_writer.WriteLine("();");
}
internal void GenerateHashtableGetEnd(string privateName)
{
_writer.Write("if (");
_writer.Write(privateName);
_writer.Write(" == null) ");
_writer.Write(privateName);
_writer.WriteLine(" = _tmp;");
_writer.Indent--;
_writer.WriteLine("}");
_writer.Write("return ");
_writer.Write(privateName);
_writer.WriteLine(";");
_writer.Indent--;
_writer.WriteLine("}");
_writer.Indent--;
_writer.WriteLine("}");
}
internal void GeneratePublicMethods(string privateName, string publicName, string[] methods, XmlMapping[] xmlMappings)
{
GenerateHashtableGetBegin(privateName, publicName);
if (methods != null && methods.Length != 0 && xmlMappings != null && xmlMappings.Length == methods.Length)
{
for (int i = 0; i < methods.Length; i++)
{
if (methods[i] == null)
continue;
_writer.Write("_tmp[");
WriteQuotedCSharpString(xmlMappings[i].Key);
_writer.Write("] = ");
WriteQuotedCSharpString(methods[i]);
_writer.WriteLine(";");
}
}
GenerateHashtableGetEnd(privateName);
}
internal void GenerateSupportedTypes(Type[] types)
{
_writer.Write("public override ");
_writer.Write(typeof(bool).FullName);
_writer.Write(" CanSerialize(");
_writer.Write(typeof(Type).FullName);
_writer.WriteLine(" type) {");
_writer.Indent++;
Hashtable uniqueTypes = new Hashtable();
for (int i = 0; i < types.Length; i++)
{
Type type = types[i];
if (type == null)
continue;
if (!type.IsPublic && !type.IsNestedPublic)
continue;
if (uniqueTypes[type] != null)
continue;
if (DynamicAssemblies.IsTypeDynamic(type))
continue;
if (type.IsGenericType || type.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))
continue;
uniqueTypes[type] = type;
_writer.Write("if (type == typeof(");
_writer.Write(CodeIdentifier.GetCSharpName(type));
_writer.WriteLine(")) return true;");
}
_writer.WriteLine("return false;");
_writer.Indent--;
_writer.WriteLine("}");
}
internal string GenerateBaseSerializer(string baseSerializer, string readerClass, string writerClass, CodeIdentifiers classes)
{
baseSerializer = CodeIdentifier.MakeValid(baseSerializer);
baseSerializer = classes.AddUnique(baseSerializer, baseSerializer);
_writer.WriteLine();
_writer.Write("public abstract class ");
_writer.Write(CodeIdentifier.GetCSharpName(baseSerializer));
_writer.Write(" : ");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializer).FullName);
_writer.WriteLine(" {");
_writer.Indent++;
_writer.Write("protected override ");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializationReader).FullName);
_writer.WriteLine(" CreateReader() {");
_writer.Indent++;
_writer.Write("return new ");
_writer.Write(readerClass);
_writer.WriteLine("();");
_writer.Indent--;
_writer.WriteLine("}");
_writer.Write("protected override ");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializationWriter).FullName);
_writer.WriteLine(" CreateWriter() {");
_writer.Indent++;
_writer.Write("return new ");
_writer.Write(writerClass);
_writer.WriteLine("();");
_writer.Indent--;
_writer.WriteLine("}");
_writer.Indent--;
_writer.WriteLine("}");
return baseSerializer;
}
internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass)
{
string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name));
serializerName = classes.AddUnique(serializerName + "Serializer", mapping);
_writer.WriteLine();
_writer.Write("public sealed class ");
_writer.Write(CodeIdentifier.GetCSharpName(serializerName));
_writer.Write(" : ");
_writer.Write(baseSerializer);
_writer.WriteLine(" {");
_writer.Indent++;
_writer.WriteLine();
_writer.Write("public override ");
_writer.Write(typeof(bool).FullName);
_writer.Write(" CanDeserialize(");
_writer.Write(typeof(XmlReader).FullName);
_writer.WriteLine(" xmlReader) {");
_writer.Indent++;
if (mapping.Accessor.Any)
{
_writer.WriteLine("return true;");
}
else
{
_writer.Write("return xmlReader.IsStartElement(");
WriteQuotedCSharpString(mapping.Accessor.Name);
_writer.Write(", ");
WriteQuotedCSharpString(mapping.Accessor.Namespace);
_writer.WriteLine(");");
}
_writer.Indent--;
_writer.WriteLine("}");
if (writeMethod != null)
{
_writer.WriteLine();
_writer.Write("protected override void Serialize(object objectToSerialize, ");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializationWriter).FullName);
_writer.WriteLine(" writer) {");
_writer.Indent++;
_writer.Write("((");
_writer.Write(writerClass);
_writer.Write(")writer).");
_writer.Write(writeMethod);
_writer.Write("(");
if (mapping is XmlMembersMapping)
{
_writer.Write("(object[])");
}
_writer.WriteLine("objectToSerialize);");
_writer.Indent--;
_writer.WriteLine("}");
}
if (readMethod != null)
{
_writer.WriteLine();
_writer.Write("protected override object Deserialize(");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializationReader).FullName);
_writer.WriteLine(" reader) {");
_writer.Indent++;
_writer.Write("return ((");
_writer.Write(readerClass);
_writer.Write(")reader).");
_writer.Write(readMethod);
_writer.WriteLine("();");
_writer.Indent--;
_writer.WriteLine("}");
}
_writer.Indent--;
_writer.WriteLine("}");
return serializerName;
}
private void GenerateTypedSerializers(Hashtable serializers)
{
string privateName = "typedSerializers";
GenerateHashtableGetBegin(privateName, "TypedSerializers");
foreach (string key in serializers.Keys)
{
_writer.Write("_tmp.Add(");
WriteQuotedCSharpString(key);
_writer.Write(", new ");
_writer.Write((string)serializers[key]);
_writer.WriteLine("());");
}
GenerateHashtableGetEnd("typedSerializers");
}
//GenerateGetSerializer(serializers, xmlMappings);
private void GenerateGetSerializer(Hashtable serializers, XmlMapping[] xmlMappings)
{
_writer.Write("public override ");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializer).FullName);
_writer.Write(" GetSerializer(");
_writer.Write(typeof(Type).FullName);
_writer.WriteLine(" type) {");
_writer.Indent++;
for (int i = 0; i < xmlMappings.Length; i++)
{
if (xmlMappings[i] is XmlTypeMapping)
{
Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type;
if (type == null)
continue;
if (!type.IsPublic && !type.IsNestedPublic)
continue;
if (DynamicAssemblies.IsTypeDynamic(type))
continue;
if (type.IsGenericType || type.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))
continue;
_writer.Write("if (type == typeof(");
_writer.Write(CodeIdentifier.GetCSharpName(type));
_writer.Write(")) return new ");
_writer.Write((string)serializers[xmlMappings[i].Key]);
_writer.WriteLine("();");
}
}
_writer.WriteLine("return null;");
_writer.Indent--;
_writer.WriteLine("}");
}
internal void GenerateSerializerContract(string className, XmlMapping[] xmlMappings, Type[] types, string readerType, string[] readMethods, string writerType, string[] writerMethods, Hashtable serializers)
{
_writer.WriteLine();
_writer.Write("public class XmlSerializerContract : global::");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializerImplementation).FullName);
_writer.WriteLine(" {");
_writer.Indent++;
_writer.Write("public override global::");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializationReader).FullName);
_writer.Write(" Reader { get { return new ");
_writer.Write(readerType);
_writer.WriteLine("(); } }");
_writer.Write("public override global::");
_writer.Write(typeof(System.Xml.Serialization.XmlSerializationWriter).FullName);
_writer.Write(" Writer { get { return new ");
_writer.Write(writerType);
_writer.WriteLine("(); } }");
GeneratePublicMethods(nameof(readMethods), "ReadMethods", readMethods, xmlMappings);
GeneratePublicMethods("writeMethods", "WriteMethods", writerMethods, xmlMappings);
GenerateTypedSerializers(serializers);
GenerateSupportedTypes(types);
GenerateGetSerializer(serializers, xmlMappings);
_writer.Indent--;
_writer.WriteLine("}");
}
internal static bool IsWildcard(SpecialMapping mapping)
{
if (mapping is SerializableMapping)
return ((SerializableMapping)mapping).IsAny;
return mapping.TypeDesc.CanBeElementValue;
}
}
}

View File

@@ -0,0 +1 @@
655dfaa141f8b465e43fe5658200a965c3be83e9

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="Microsoft.XmlSerializer.Generator">
<Assembly Name="Microsoft.XmlSerializer.Generator">
<Namespace Name="Microsoft.XmlSerializer.Generator">
<!-- XmlQualifiedName is well known to the serializers and must not be explicitly specified to sg.exe -->
<Type Name="XmlQualifiedName" DataContractSerializer="Excluded" DataContractJsonSerializer="Excluded"/>
</Namespace>
</Assembly>
</Library>
</Directives>

View File

@@ -0,0 +1,328 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace Microsoft.XmlSerializer.Generator
{
public class Sgen
{
public static int Main(string[] args)
{
Sgen sgen = new Sgen();
sgen.Run(args);
return 0;
}
private int Run(string[] args)
{
string assembly = null;
List<string> types = new List<string>();
string codePath = null;
var errs = new ArrayList();
bool force = false;
bool proxyOnly = false;
try
{
for (int i = 0; i < args.Length; i++)
{
bool argument = false;
string arg = args[i];
string value = string.Empty;
if (arg.StartsWith("/") || arg.StartsWith("-"))
{
argument = true;
int colonPos = arg.IndexOf(":");
if (colonPos != -1)
{
value = arg.Substring(colonPos + 1).Trim();
arg = arg.Substring(0, colonPos).Trim();
}
}
arg = arg.ToLower(CultureInfo.InvariantCulture);
if (ArgumentMatch(arg, "?") || ArgumentMatch(arg, "help"))
{
WriteHeader();
WriteHelp();
return 0;
}
else if (!argument && (arg.EndsWith(".dll") || arg.EndsWith(".exe")))
{
if (assembly != null)
{
errs.Add(SR.Format(SR.ErrInvalidArgument, "/assembly", arg));
}
assembly = arg;
}
else if (ArgumentMatch(arg, "force"))
{
force = true;
}
else if (ArgumentMatch(arg, "proxytypes"))
{
proxyOnly = true;
}
else if (ArgumentMatch(arg, "out"))
{
if (codePath != null)
{
errs.Add(SR.Format(SR.ErrInvalidArgument, "/out", arg));
}
codePath = value;
}
else if (ArgumentMatch(arg, "type"))
{
types.Add(value);
}
else
{
errs.Add(SR.Format(SR.ErrInvalidArgument, arg));
continue;
}
}
if (errs.Count > 0)
{
foreach (string err in errs)
{
Console.Error.WriteLine(FormatMessage(true, SR.Format(SR.Warning, err)));
}
}
if (args.Length == 0 || assembly == null)
{
if (assembly == null)
{
Console.Error.WriteLine(FormatMessage(false, SR.Format(SR.ErrMissingRequiredArgument, SR.Format(SR.ErrAssembly, "assembly"))));
}
WriteHelp();
return 0;
}
GenerateFile(types, assembly, proxyOnly, force, codePath);
}
catch (Exception e)
{
if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
{
throw;
}
return 1;
}
return 0;
}
private void GenerateFile(List<string> typeNames, string assemblyName, bool proxyOnly, bool force, string outputDirectory)
{
Assembly assembly = LoadAssembly(assemblyName, true);
Type[] types;
if (typeNames == null || typeNames.Count == 0)
{
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException typeException)
{
List<Type> loadedTypes = new List<Type>();
foreach (Type type in typeException.Types)
{
if (type != null)
{
loadedTypes.Add(type);
}
}
types = loadedTypes.ToArray();
}
}
else
{
types = new Type[typeNames.Count];
int typeIndex = 0;
foreach (string typeName in typeNames)
{
Type type = assembly.GetType(typeName);
if (type == null)
{
Console.Error.WriteLine(FormatMessage(false, SR.Format(SR.ErrorDetails, SR.Format(SR.ErrLoadType, typeName, assemblyName))));
}
types[typeIndex++] = type;
}
}
var mappings = new ArrayList();
var importedTypes = new ArrayList();
var importer = new XmlReflectionImporter();
for (int i = 0; i < types.Length; i++)
{
Type type = types[i];
if (type != null)
{
bool isObsolete = false;
object[] obsoleteAttributes = type.GetCustomAttributes(typeof(ObsoleteAttribute), false);
foreach (object attribute in obsoleteAttributes)
{
if (((ObsoleteAttribute)attribute).IsError)
{
isObsolete = true;
break;
}
}
if (isObsolete)
{
continue;
}
}
if (!proxyOnly)
{
ImportType(type, mappings, importedTypes, importer);
}
}
if (importedTypes.Count > 0)
{
var serializableTypes = (Type[])importedTypes.ToArray(typeof(Type));
var allMappings = (XmlMapping[])mappings.ToArray(typeof(XmlMapping));
bool gac = assembly.GlobalAssemblyCache;
outputDirectory = outputDirectory == null ? (gac ? Environment.CurrentDirectory : Path.GetDirectoryName(assembly.Location)) : outputDirectory;
string serializerName = XmlSerializer.GetXmlSerializerAssemblyName(serializableTypes[0], null);
string codePath = Path.Combine(outputDirectory, serializerName + ".cs");
if (!force)
{
if (File.Exists(codePath))
throw new InvalidOperationException(SR.Format(SR.ErrSerializerExists, codePath, "force"));
}
if (Directory.Exists(codePath))
{
throw new InvalidOperationException(SR.Format(SR.ErrDirectoryExists, codePath));
}
if (!Directory.Exists(outputDirectory))
{
throw new ArgumentException(SR.Format(SR.ErrDirectoryNotExists, codePath, outputDirectory));
}
bool success;
try
{
if (File.Exists(codePath))
{
File.Delete(codePath);
}
using (FileStream fs = File.Create(codePath))
{
success = XmlSerializer.GenerateSerializer(serializableTypes, allMappings, fs);
}
}
catch (UnauthorizedAccessException)
{
throw new UnauthorizedAccessException(SR.Format(SR.DirectoryAccessDenied, outputDirectory));
}
if (success)
{
Console.Out.WriteLine(SR.Format(SR.InfoAssemblyName, codePath));
Console.Out.WriteLine(SR.Format(SR.InfoGeneratedAssembly, assembly.Location, codePath));
}
else
{
Console.Out.WriteLine(FormatMessage(false, SR.Format(SR.ErrGenerationFailed, assembly.Location)));
}
}
}
// assumes all same case.
private bool ArgumentMatch(string arg, string formal)
{
if (arg[0] != '/' && arg[0] != '-')
{
return false;
}
arg = arg.Substring(1);
return (arg == formal || (arg.Length == 1 && arg[0] == formal[0]));
}
private void ImportType(Type type, ArrayList mappings, ArrayList importedTypes, XmlReflectionImporter importer)
{
XmlTypeMapping xmlTypeMapping = null;
var localImporter = new XmlReflectionImporter();
try
{
xmlTypeMapping = localImporter.ImportTypeMapping(type);
}
catch (Exception e)
{
if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
{
throw;
}
return;
}
if (xmlTypeMapping != null)
{
xmlTypeMapping = importer.ImportTypeMapping(type);
mappings.Add(xmlTypeMapping);
importedTypes.Add(type);
}
}
private static Assembly LoadAssembly(string assemblyName, bool throwOnFail)
{
Assembly assembly = null;
string path = Path.GetFullPath(assemblyName);
assembly = Assembly.LoadFile(path);
return assembly;
}
private void WriteHeader()
{
// do not localize Copyright header
Console.WriteLine(String.Format(CultureInfo.CurrentCulture, "[Microsoft (R) .NET Framework, Version {0}]", TempAssembly.ThisAssembly.InformationalVersion));
Console.WriteLine("Copyright (C) Microsoft Corporation. All rights reserved.");
}
void WriteHelp()
{
//TBD
Console.WriteLine("In Development");
}
private static string FormatMessage(bool warning, string message)
{
return FormatMessage(warning, "SGEN1", message);
}
private static string FormatMessage(bool warning, string code, string message)
{
return "SGEN: " + (warning ? "warning " : "error ") + code + ": " + message;
}
}
}

View File

@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Microsoft.XmlSerializer.Generator
{
internal static class SoapAttributesExtensions
{
private static PropertyInfo s_soapFlagsPropertyInfo;
internal static SoapAttributeFlags GetSoapFlags(this SoapAttributes soapAtt)
{
if (s_soapFlagsPropertyInfo == null)
{
s_soapFlagsPropertyInfo = typeof(SoapAttributes).GetProperty("SoapFlags", BindingFlags.NonPublic | BindingFlags.Instance);
}
return (SoapAttributeFlags)s_soapFlagsPropertyInfo.GetValue(soapAtt);
}
}
internal enum SoapAttributeFlags
{
Enum = 0x1,
Type = 0x2,
Element = 0x4,
Attribute = 0x8,
}
}

View File

@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Microsoft.XmlSerializer.Generator
{
internal static class XmlAnyElementAttributeExtensions
{
private static PropertyInfo s_NamespaceSpecifiedPropertyInfo;
internal static bool GetNamespaceSpecified(this XmlAnyElementAttribute xmlAnyElementAtt)
{
if(s_NamespaceSpecifiedPropertyInfo == null)
{
s_NamespaceSpecifiedPropertyInfo = typeof(XmlAnyElementAttribute).GetProperty("NamespaceSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
}
return (bool)s_NamespaceSpecifiedPropertyInfo.GetValue(xmlAnyElementAtt);
}
}
}

View File

@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Microsoft.XmlSerializer.Generator
{
internal static class XmlArrayItemAttributeExtensions
{
private static PropertyInfo s_IsNullableSpecifiedPropertyInfo;
internal static bool GetIsNullableSpecified(this XmlArrayItemAttribute xmlArrayItemAtt)
{
if(s_IsNullableSpecifiedPropertyInfo == null)
{
s_IsNullableSpecifiedPropertyInfo= typeof(XmlArrayItemAttribute).GetProperty("IsNullableSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
}
return (bool)s_IsNullableSpecifiedPropertyInfo.GetValue(xmlArrayItemAtt);
}
}
}

View File

@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Microsoft.XmlSerializer.Generator
{
internal static class XmlChoiceIdentifierAttributeExtensions
{
private static PropertyInfo s_MemberInfoPropertyInfo = typeof(XmlChoiceIdentifierAttribute).GetProperty("MemberInfo", BindingFlags.NonPublic | BindingFlags.Instance);
internal static MemberInfo GetMemberInfo(this XmlChoiceIdentifierAttribute xmlChoiceIdentifierAtt)
{
return (MemberInfo)s_MemberInfoPropertyInfo.GetValue(xmlChoiceIdentifierAtt);
}
internal static void SetMemberInfo(this XmlChoiceIdentifierAttribute xmlChoiceIdentifierAtt, MemberInfo memberInfo)
{
s_MemberInfoPropertyInfo.SetValue(xmlChoiceIdentifierAtt, memberInfo);
}
}
}

View File

@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Microsoft.XmlSerializer.Generator
{
internal static class XmlElementAttributeExtension
{
private static PropertyInfo s_IsNullableSpecifiedPropertyInfo;
internal static bool GetIsNullableSpecified(this XmlElementAttribute xmlElementAtt)
{
if(s_IsNullableSpecifiedPropertyInfo == null)
{
s_IsNullableSpecifiedPropertyInfo = typeof(XmlElementAttribute).GetProperty("IsNullableSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
}
return (bool)s_IsNullableSpecifiedPropertyInfo.GetValue(xmlElementAtt);
}
}
}

View File

@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Microsoft.XmlSerializer.Generator
{
internal static class XmlRootAttributeExtensions
{
private static PropertyInfo s_IsNullableSpecifiedPropertyInfo;
private static PropertyInfo s_KeydPropertyInfo;
internal static bool GetIsNullableSpecified(this XmlRootAttribute xmlRootAtt)
{
if(s_IsNullableSpecifiedPropertyInfo == null)
{
s_IsNullableSpecifiedPropertyInfo = typeof(XmlRootAttribute).GetProperty("IsNullableSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
}
return (bool)s_IsNullableSpecifiedPropertyInfo.GetValue(xmlRootAtt);
}
internal static string GetKey(this XmlRootAttribute xmlRootAtt)
{
if (s_KeydPropertyInfo == null)
{
s_KeydPropertyInfo = typeof(XmlRootAttribute).GetProperty("Key", BindingFlags.NonPublic | BindingFlags.Instance);
}
return (string)s_KeydPropertyInfo.GetValue(xmlRootAtt);
}
}
}

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{0D1E2954-A5C7-4B8C-932A-31EB4A96A737}</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard-Release|AnyCPU'" />
<ItemGroup>
<Compile Include=".\SGenTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Microsoft.XmlSerializer.Generator.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
using Microsoft.XmlSerializer.Generator;
namespace Microsoft.XmlSerializer.Generator.Tests
{
public static class SgenTests
{
[Fact]
public static void BasicTest()
{
int n = Sgen.Main(null);
Assert.Equal(0, n);
}
}
}