Skip to content

Commit

Permalink
Sgen: Added 'default-namespace' argument (#46500)
Browse files Browse the repository at this point in the history
* Sgen: Added an optional 'default-namespace' argument

The .NET runtime support loading pre-generated assemblies according to both the type & defaultNamespace when creating an XmlSerializer instance,
Given that, it is extremely useful to have the ability to generate an assembly for different defaultNamespace.

* Added defaultNamespace argument to XmlReflectionImporter

* Prevent setting default-namespace twice

* Added missing using statement
  • Loading branch information
TalAloni authored Aug 6, 2021
1 parent 3caf2f0 commit f26a143
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -28,6 +29,7 @@ private int Run(string[] args)
{
string assembly = null;
var types = new List<string>();
string defaultNamespace = null;
string codePath = null;
var errs = new List<string>();
bool force = false;
Expand Down Expand Up @@ -102,6 +104,18 @@ private int Run(string[] args)
assembly = args[i];
}
}
else if (ArgumentMatch(arg, "default-namespace"))
{
i++;
if (i >= args.Length || defaultNamespace != null)
{
errs.Add(SR.Format(SR.ErrInvalidArgument, arg));
}
else
{
defaultNamespace = args[i];
}
}
else if (ArgumentMatch(arg, "quiet"))
{
disableRun = false;
Expand Down Expand Up @@ -189,7 +203,7 @@ private int Run(string[] args)
ParseReferences();
}

GenerateFile(types, assembly, proxyOnly, silent, verbose, force, codePath, parsableErrors);
GenerateFile(types, defaultNamespace, assembly, proxyOnly, silent, verbose, force, codePath, parsableErrors);
}
catch (Exception e)
{
Expand All @@ -205,7 +219,7 @@ private int Run(string[] args)
return 0;
}

private void GenerateFile(List<string> typeNames, string assemblyName, bool proxyOnly, bool silent, bool verbose, bool force, string outputDirectory, bool parsableerrors)
private void GenerateFile(List<string> typeNames, string defaultNamespace, string assemblyName, bool proxyOnly, bool silent, bool verbose, bool force, string outputDirectory, bool parsableerrors)
{
Assembly assembly = LoadAssembly(assemblyName, true);
Type[] types;
Expand Down Expand Up @@ -248,7 +262,7 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox

var mappings = new List<XmlMapping>();
var importedTypes = new List<Type>();
var importer = new XmlReflectionImporter();
var importer = new XmlReflectionImporter(defaultNamespace);

for (int i = 0; i < types.Length; i++)
{
Expand Down Expand Up @@ -294,7 +308,7 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox

if (!proxyOnly)
{
ImportType(type, mappings, importedTypes, verbose, importer, parsableerrors);
ImportType(type, defaultNamespace, mappings, importedTypes, verbose, importer, parsableerrors);
}
}

Expand All @@ -318,7 +332,7 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox
}
}

string serializerName = GetXmlSerializerAssemblyName(serializableTypes[0], null);
string serializerName = GetXmlSerializerAssemblyName(serializableTypes[0], defaultNamespace);
string codePath = Path.Combine(outputDirectory, serializerName + ".cs");

if (!force)
Expand All @@ -344,14 +358,31 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox

using (FileStream fs = File.Create(codePath))
{
MethodInfo method = typeof(System.Xml.Serialization.XmlSerializer).GetMethod("GenerateSerializer", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
MethodInfo method;
if (defaultNamespace == null)
{
method = typeof(System.Xml.Serialization.XmlSerializer).GetMethod("GenerateSerializer", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
}
else
{
Type tempAssemblyType = typeof(System.Xml.Serialization.XmlSerializer).Assembly.GetType("System.Xml.Serialization.TempAssembly");
method = tempAssemblyType.GetMethod("GenerateSerializerToStream", BindingFlags.Static | BindingFlags.NonPublic);
}

if (method == null)
{
Console.Error.WriteLine(FormatMessage(parsableerrors: false, warning: false, message: SR.GenerateSerializerNotFound));
}
else
{
success = (bool)method.Invoke(null, new object[] { serializableTypes, allMappings, fs });
if (defaultNamespace == null)
{
success = (bool)method.Invoke(null, new object[] { serializableTypes, allMappings, fs });
}
else
{
success = (bool)method.Invoke(null, new object[] { allMappings, serializableTypes, defaultNamespace, assembly, new Hashtable(), fs });
}
}
}
}
Expand Down Expand Up @@ -410,13 +441,13 @@ private bool ShortNameArgumentMatch(string arg, string shortName)
return arg.Equals(shortName, StringComparison.InvariantCultureIgnoreCase);
}

private void ImportType(Type type, List<XmlMapping> mappings, List<Type> importedTypes, bool verbose, XmlReflectionImporter importer, bool parsableerrors)
private void ImportType(Type type, string defaultNamespace, List<XmlMapping> mappings, List<Type> importedTypes, bool verbose, XmlReflectionImporter importer, bool parsableerrors)
{
XmlTypeMapping xmlTypeMapping = null;
var localImporter = new XmlReflectionImporter();
var localImporter = new XmlReflectionImporter(defaultNamespace);
try
{
xmlTypeMapping = localImporter.ImportTypeMapping(type);
xmlTypeMapping = localImporter.ImportTypeMapping(type, defaultNamespace);
}
catch (Exception e)
{
Expand All @@ -435,7 +466,7 @@ private void ImportType(Type type, List<XmlMapping> mappings, List<Type> importe
}
if (xmlTypeMapping != null)
{
xmlTypeMapping = importer.ImportTypeMapping(type);
xmlTypeMapping = importer.ImportTypeMapping(type, defaultNamespace);
mappings.Add(xmlTypeMapping);
importedTypes.Add(type);
}
Expand Down

0 comments on commit f26a143

Please sign in to comment.