Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sgen: Added 'default-namespace' argument #46500

Merged
merged 6 commits into from
Aug 6, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private int Run(string[] args)
{
string assembly = null;
List<string> types = new List<string>();
string defaultNamespace = null;
string codePath = null;
var errs = new ArrayList();
bool force = false;
Expand Down Expand Up @@ -103,6 +104,18 @@ private int Run(string[] args)
assembly = args[i];
}
}
else if (ArgumentMatch(arg, "default-namespace"))
TalAloni marked this conversation as resolved.
Show resolved Hide resolved
{
i++;
if (i >= args.Length)
{
errs.Add(SR.Format(SR.ErrInvalidArgument, arg));
}
else
{
defaultNamespace = args[i];
}
}
else if (ArgumentMatch(arg, "quiet"))
{
disableRun = false;
Expand Down Expand Up @@ -190,7 +203,7 @@ private int Run(string[] args)
ParseReferences();
}

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

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

var mappings = new ArrayList();
var importedTypes = new ArrayList();
var importer = new XmlReflectionImporter();
var importer = new XmlReflectionImporter(defaultNamespace);

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

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

Expand All @@ -314,7 +327,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 @@ -340,14 +353,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");
TalAloni marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -406,13 +436,13 @@ private bool ShortNameArgumentMatch(string arg, string shortName)
return arg.Equals(shortName, StringComparison.InvariantCultureIgnoreCase);
}

private void ImportType(Type type, ArrayList mappings, ArrayList importedTypes, bool verbose, XmlReflectionImporter importer, bool parsableerrors)
private void ImportType(Type type, string defaultNamespace, ArrayList mappings, ArrayList 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);
TalAloni marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception e)
{
Expand All @@ -431,7 +461,7 @@ private void ImportType(Type type, ArrayList mappings, ArrayList importedTypes,
}
if (xmlTypeMapping != null)
{
xmlTypeMapping = importer.ImportTypeMapping(type);
xmlTypeMapping = importer.ImportTypeMapping(type, defaultNamespace);
TalAloni marked this conversation as resolved.
Show resolved Hide resolved
mappings.Add(xmlTypeMapping);
importedTypes.Add(type);
}
Expand Down