diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 8217848b..540124d6 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -1218,12 +1218,19 @@ namespace CppSharp.Generators.CSharp } else { - var systemType = Internal.ExpressionHelper.GetSystemType(Context, type); - if (!Internal.ExpressionHelper.TryParseExactLiteralExpression(ref initializerString, systemType)) - Write(type.IsPrimitiveType() || type.IsEnum() ? $"({type}) {initializerString}" : - $"new {type}({initializerString})"); - else + if (Internal.ExpressionHelper.PrintExpression(Context, null, type, + variable.Initializer, false, ref initializerString) == null) + { Write(initializerString); + } + else + { + var systemType = Internal.ExpressionHelper.GetSystemType(Context, type); + if (!Internal.ExpressionHelper.TryParseExactLiteralExpression(ref initializerString, systemType)) + Write($"({type}) {initializerString}"); + else + Write(initializerString); + } } WriteLine(";"); } diff --git a/src/Generator/Passes/ExpressionHelper.cs b/src/Generator/Passes/ExpressionHelper.cs index 708e4eb8..dffb6f82 100644 --- a/src/Generator/Passes/ExpressionHelper.cs +++ b/src/Generator/Passes/ExpressionHelper.cs @@ -1,12 +1,10 @@ using System.Collections.Generic; -using System.Linq; using System.Text; using System.Text.RegularExpressions; using CppSharp.AST; using CppSharp.AST.Extensions; using CppSharp.Generators; using CppSharp.Generators.CSharp; -using CppSharp.Parser; using CppSharp.Types; using TypeCode = System.TypeCode; @@ -40,8 +38,7 @@ namespace CppSharp.Internal if (desugared.IsPrimitiveTypeConvertibleToRef()) { - var method = function as Method; - if (method != null && method.IsConstructor) + if (function is Method method && method.IsConstructor) { result = string.Empty; return false; @@ -282,7 +279,8 @@ namespace CppSharp.Internal var method = (Method)expression.Declaration; var expressionSupported = decl.IsValueType && method.Parameters.Count == 0; - if (expression.String.Contains('(') || expression.String.StartsWith("{")) + if (expression.String.Contains('(') || expression.String.StartsWith("{") || + ctor.Arguments.Count > 1) { var argsBuilder = new StringBuilder("new "); argsBuilder.Append(typePrinterResult); @@ -312,14 +310,14 @@ namespace CppSharp.Internal switch (builtin.Type) { case PrimitiveType.Float: - if (statement.String.EndsWith(".F", System.StringComparison.Ordinal)) + if (statement.String.EndsWith(".F", System.StringComparison.OrdinalIgnoreCase)) { result = statement.String.Replace(".F", ".0F"); return true; } break; case PrimitiveType.Double: - if (statement.String.EndsWith(".", System.StringComparison.Ordinal)) + if (statement.String.EndsWith(".", System.StringComparison.OrdinalIgnoreCase)) { result = statement.String + '0'; return true; diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 4521a20f..c018c422 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -491,13 +491,13 @@ namespace SomeNamespace class Inlines { public: - constexpr Inlines(int param, const char* name) {} + constexpr Inlines(float param, const char* name) {} inline operator NamespacedAbstractImpl () const { return NamespacedAbstractImpl(); } protected: void protectedInlined() {} }; - constexpr Inlines constWithParams(5, "test"); + constexpr Inlines constWithParams(5.f, "test"); class AbstractInlines {