diff options
author | Stephen Toub <stoub@microsoft.com> | 2017-02-07 23:15:50 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-07 23:15:50 +0300 |
commit | edfd662f8cfb32444b89b1945b5c700760e1d5c2 (patch) | |
tree | 43da82b0b6fd40b9666e978e2526dd89d23259f6 | |
parent | 9e66e17bc4035936217b8ed3c4c2438a01755e3d (diff) | |
parent | 90f07f2de74e38ebaba4cf07efb341caa750efa5 (diff) |
Merge pull request #15918 from stephentoub/codedom_fix
CodeDom fixes for explicit interface implementation with built-in types
4 files changed, 150 insertions, 137 deletions
diff --git a/src/System.CodeDom/src/Microsoft/CSharp/CSharpCodeGenerator.cs b/src/System.CodeDom/src/Microsoft/CSharp/CSharpCodeGenerator.cs index 242a60a2e8..e180cb5c5a 100644 --- a/src/System.CodeDom/src/Microsoft/CSharp/CSharpCodeGenerator.cs +++ b/src/System.CodeDom/src/Microsoft/CSharp/CSharpCodeGenerator.cs @@ -1213,7 +1213,7 @@ namespace Microsoft.CSharp string name = e.Name; if (e.PrivateImplementationType != null) { - name = GetBaseTypeOutput(e.PrivateImplementationType) + "." + name; + name = GetBaseTypeOutput(e.PrivateImplementationType, preferBuiltInTypes: false) + "." + name; } OutputTypeNamePair(e.Type, name); Output.WriteLine(';'); @@ -1472,7 +1472,7 @@ namespace Microsoft.CSharp Output.Write(' '); if (e.PrivateImplementationType != null) { - Output.Write(GetBaseTypeOutput(e.PrivateImplementationType)); + Output.Write(GetBaseTypeOutput(e.PrivateImplementationType, preferBuiltInTypes: false)); Output.Write('.'); } OutputIdentifier(e.Name); @@ -1558,7 +1558,7 @@ namespace Microsoft.CSharp if (e.PrivateImplementationType != null && !IsCurrentInterface) { - Output.Write(GetBaseTypeOutput(e.PrivateImplementationType)); + Output.Write(GetBaseTypeOutput(e.PrivateImplementationType, preferBuiltInTypes: false)); Output.Write('.'); } @@ -2803,68 +2803,56 @@ namespace Microsoft.CSharp } // returns the type name without any array declaration. - private string GetBaseTypeOutput(CodeTypeReference typeRef) + private string GetBaseTypeOutput(CodeTypeReference typeRef, bool preferBuiltInTypes = true) { string s = typeRef.BaseType; - if (s.Length == 0) - { - s = "void"; - return s; - } - string lowerCaseString = s.ToLower(CultureInfo.InvariantCulture).Trim(); + if (preferBuiltInTypes) + { + if (s.Length == 0) + { + return "void"; + } + + string lowerCaseString = s.ToLower(CultureInfo.InvariantCulture).Trim(); + + switch (lowerCaseString) + { + case "system.int16": + return "short"; + case "system.int32": + return "int"; + case "system.int64": + return "long"; + case "system.string": + return "string"; + case "system.object": + return "object"; + case "system.boolean": + return "bool"; + case "system.void": + return "void"; + case "system.char": + return "char"; + case "system.byte": + return "byte"; + case "system.uint16": + return "ushort"; + case "system.uint32": + return "uint"; + case "system.uint64": + return "ulong"; + case "system.sbyte": + return "sbyte"; + case "system.single": + return "float"; + case "system.double": + return "double"; + case "system.decimal": + return "decimal"; + } + } - switch (lowerCaseString) - { - case "system.int16": - s = "short"; - break; - case "system.int32": - s = "int"; - break; - case "system.int64": - s = "long"; - break; - case "system.string": - s = "string"; - break; - case "system.object": - s = "object"; - break; - case "system.boolean": - s = "bool"; - break; - case "system.void": - s = "void"; - break; - case "system.char": - s = "char"; - break; - case "system.byte": - s = "byte"; - break; - case "system.uint16": - s = "ushort"; - break; - case "system.uint32": - s = "uint"; - break; - case "system.uint64": - s = "ulong"; - break; - case "system.sbyte": - s = "sbyte"; - break; - case "system.single": - s = "float"; - break; - case "system.double": - s = "double"; - break; - case "system.decimal": - s = "decimal"; - break; - default: // replace + with . for nested classes. // var sb = new StringBuilder(s.Length + 10); @@ -2919,8 +2907,6 @@ namespace Microsoft.CSharp sb.Append(CreateEscapedIdentifier(baseType.Substring(lastIndex))); return sb.ToString(); - } - return s; } private string GetTypeArgumentsOutput(CodeTypeReferenceCollection typeArguments) diff --git a/src/System.CodeDom/src/Microsoft/VisualBasic/VBCodeGenerator.cs b/src/System.CodeDom/src/Microsoft/VisualBasic/VBCodeGenerator.cs index 60f5a4f18d..a03d0b02be 100644 --- a/src/System.CodeDom/src/Microsoft/VisualBasic/VBCodeGenerator.cs +++ b/src/System.CodeDom/src/Microsoft/VisualBasic/VBCodeGenerator.cs @@ -1434,7 +1434,7 @@ namespace Microsoft.VisualBasic string eventName = e.Name; if (e.PrivateImplementationType != null) { - string impl = GetBaseTypeOutput(e.PrivateImplementationType); + string impl = GetBaseTypeOutput(e.PrivateImplementationType, preferBuiltInTypes: false); impl = impl.Replace('.', '_'); e.Name = impl + "_" + e.Name; } @@ -1561,7 +1561,7 @@ namespace Microsoft.VisualBasic string methodName = e.Name; if (e.PrivateImplementationType != null) { - string impl = GetBaseTypeOutput(e.PrivateImplementationType); + string impl = GetBaseTypeOutput(e.PrivateImplementationType, preferBuiltInTypes: false); impl = impl.Replace('.', '_'); e.Name = impl + "_" + e.Name; } @@ -1714,7 +1714,7 @@ namespace Microsoft.VisualBasic string propName = e.Name; if (e.PrivateImplementationType != null) { - string impl = GetBaseTypeOutput(e.PrivateImplementationType); + string impl = GetBaseTypeOutput(e.PrivateImplementationType, preferBuiltInTypes: false); impl = impl.Replace('.', '_'); e.Name = impl + "_" + e.Name; } @@ -2399,80 +2399,56 @@ namespace Microsoft.VisualBasic return name; } - private string GetBaseTypeOutput(CodeTypeReference typeRef) + private string GetBaseTypeOutput(CodeTypeReference typeRef, bool preferBuiltInTypes = true) { string baseType = typeRef.BaseType; - if (baseType.Length == 0) - { - return "Void"; - } - else if (string.Equals(baseType, "System.Byte", StringComparison.OrdinalIgnoreCase)) - { - return "Byte"; - } - else if (string.Equals(baseType, "System.SByte", StringComparison.OrdinalIgnoreCase)) - { - return "SByte"; - } - else if (string.Equals(baseType, "System.Int16", StringComparison.OrdinalIgnoreCase)) - { - return "Short"; - } - else if (string.Equals(baseType, "System.Int32", StringComparison.OrdinalIgnoreCase)) - { - return "Integer"; - } - else if (string.Equals(baseType, "System.Int64", StringComparison.OrdinalIgnoreCase)) - { - return "Long"; - } - else if (string.Equals(baseType, "System.UInt16", StringComparison.OrdinalIgnoreCase)) - { - return "UShort"; - } - else if (string.Equals(baseType, "System.UInt32", StringComparison.OrdinalIgnoreCase)) - { - return "UInteger"; - } - else if (string.Equals(baseType, "System.UInt64", StringComparison.OrdinalIgnoreCase)) - { - return "ULong"; - } - else if (string.Equals(baseType, "System.String", StringComparison.OrdinalIgnoreCase)) - { - return "String"; - } - else if (string.Equals(baseType, "System.DateTime", StringComparison.OrdinalIgnoreCase)) - { - return "Date"; - } - else if (string.Equals(baseType, "System.Decimal", StringComparison.OrdinalIgnoreCase)) - { - return "Decimal"; - } - else if (string.Equals(baseType, "System.Single", StringComparison.OrdinalIgnoreCase)) - { - return "Single"; - } - else if (string.Equals(baseType, "System.Double", StringComparison.OrdinalIgnoreCase)) - { - return "Double"; - } - else if (string.Equals(baseType, "System.Boolean", StringComparison.OrdinalIgnoreCase)) - { - return "Boolean"; - } - else if (string.Equals(baseType, "System.Char", StringComparison.OrdinalIgnoreCase)) - { - return "Char"; - } - else if (string.Equals(baseType, "System.Object", StringComparison.OrdinalIgnoreCase)) - { - return "Object"; + if (preferBuiltInTypes) + { + if (baseType.Length == 0) + { + return "Void"; + } + + string lowerCaseString = baseType.ToLowerInvariant(); + + switch (lowerCaseString) + { + case "system.byte": + return "Byte"; + case "system.sbyte": + return "SByte"; + case "system.int16": + return "Short"; + case "system.int32": + return "Integer"; + case "system.int64": + return "Long"; + case "system.uint16": + return "UShort"; + case "system.uint32": + return "UInteger"; + case "system.uint64": + return "ULong"; + case "system.string": + return "String"; + case "system.datetime": + return "Date"; + case "system.decimal": + return "Decimal"; + case "system.single": + return "Single"; + case "system.double": + return "Double"; + case "system.boolean": + return "Boolean"; + case "system.char": + return "Char"; + case "system.object": + return "Object"; + } } - else - { + var sb = new StringBuilder(baseType.Length + 10); if ((typeRef.Options & CodeTypeReferenceOptions.GlobalReference) != 0) { @@ -2525,7 +2501,6 @@ namespace Microsoft.VisualBasic } return sb.ToString(); - } } private string GetTypeOutputWithoutArrayPostFix(CodeTypeReference typeRef) diff --git a/src/System.CodeDom/tests/CSharpCodeGenerationTests.cs b/src/System.CodeDom/tests/CSharpCodeGenerationTests.cs index bc36ebccaf..38777f60e4 100644 --- a/src/System.CodeDom/tests/CSharpCodeGenerationTests.cs +++ b/src/System.CodeDom/tests/CSharpCodeGenerationTests.cs @@ -448,6 +448,32 @@ namespace System.CodeDom.Tests }"); } + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework outputs C# keywords rather than type names")] + [Theory] + [InlineData(typeof(byte), "void System.Byte.MyMethod() { }")] + [InlineData(typeof(short), "void System.Int16.MyMethod() { }")] + [InlineData(typeof(ushort), "void System.UInt16.MyMethod() { }")] + [InlineData(typeof(int), "void System.Int32.MyMethod() { }")] + [InlineData(typeof(uint), "void System.UInt32.MyMethod() { }")] + [InlineData(typeof(long), "void System.Int64.MyMethod() { }")] + [InlineData(typeof(ulong), "void System.UInt64.MyMethod() { }")] + [InlineData(typeof(string), "void System.String.MyMethod() { }")] + [InlineData(typeof(object), "void System.Object.MyMethod() { }")] + [InlineData(typeof(bool), "void System.Boolean.MyMethod() { }")] + [InlineData(typeof(void), "void System.Void.MyMethod() { }")] + [InlineData(typeof(char), "void System.Char.MyMethod() { }")] + [InlineData(typeof(float), "void System.Single.MyMethod() { }")] + [InlineData(typeof(double), "void System.Double.MyMethod() { }")] + [InlineData(typeof(decimal), "void System.Decimal.MyMethod() { }")] + public void ExplicitImplementation(Type type, string expectedResult) + { + AssertEqual(new CodeMemberMethod() + { + Name = "MyMethod", + PrivateImplementationType = new CodeTypeReference(type) + }, expectedResult); + } + [Fact] public void Arrays_SingleDimensional_PrimitiveTypes() { diff --git a/src/System.CodeDom/tests/VBCodeGenerationTests.cs b/src/System.CodeDom/tests/VBCodeGenerationTests.cs index 43e752c1b9..619694b8ba 100644 --- a/src/System.CodeDom/tests/VBCodeGenerationTests.cs +++ b/src/System.CodeDom/tests/VBCodeGenerationTests.cs @@ -416,6 +416,32 @@ namespace System.CodeDom.Tests End Namespace"); } + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework outputs C# keywords rather than type names")] + [Theory] + [InlineData(typeof(byte), "Sub System_[Byte]_MyMethod() Implements Byte.MyMethod End Sub")] + [InlineData(typeof(short), "Sub System_Int16_MyMethod() Implements Short.MyMethod End Sub")] + [InlineData(typeof(ushort), "Sub System_UInt16_MyMethod() Implements UShort.MyMethod End Sub")] + [InlineData(typeof(int), "Sub System_Int32_MyMethod() Implements Integer.MyMethod End Sub")] + [InlineData(typeof(uint), "Sub System_UInt32_MyMethod() Implements UInteger.MyMethod End Sub")] + [InlineData(typeof(long), "Sub System_Int64_MyMethod() Implements Long.MyMethod End Sub")] + [InlineData(typeof(ulong), "Sub System_UInt64_MyMethod() Implements ULong.MyMethod End Sub")] + [InlineData(typeof(string), "Sub System_[String]_MyMethod() Implements String.MyMethod End Sub")] + [InlineData(typeof(object), "Sub System_[Object]_MyMethod() Implements Object.MyMethod End Sub")] + [InlineData(typeof(bool), "Sub System_[Boolean]_MyMethod() Implements Boolean.MyMethod End Sub")] + [InlineData(typeof(void), "Sub System_Void_MyMethod() Implements System.Void.MyMethod End Sub")] + [InlineData(typeof(char), "Sub System_[Char]_MyMethod() Implements Char.MyMethod End Sub")] + [InlineData(typeof(float), "Sub System_[Single]_MyMethod() Implements Single.MyMethod End Sub")] + [InlineData(typeof(double), "Sub System_[Double]_MyMethod() Implements Double.MyMethod End Sub")] + [InlineData(typeof(decimal), "Sub System_[Decimal]_MyMethod() Implements Decimal.MyMethod End Sub")] + public void ExplicitImplementation_BuiltIns_UsesTypeNamesForBetterCompilerHandling(Type type, string expectedResult) + { + AssertEqual(new CodeMemberMethod() + { + Name = "MyMethod", + PrivateImplementationType = new CodeTypeReference(type) + }, expectedResult); + } + [Fact] public void Arrays_SingleDimensional_PrimitiveTypes() { |