diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 0b52e3b9..7e47bba4 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -356,6 +356,14 @@ namespace CppSharp.Generators.CSharp if (@class.IsIncomplete) return; + System.Type typeMap = null; + if (Driver.TypeDatabase.TypeMaps.ContainsKey(@class.Name)) + { + typeMap = Driver.TypeDatabase.TypeMaps[@class.Name]; + // disable the type map for the mapped class itself so that operator params are not mapped + Driver.TypeDatabase.TypeMaps.Remove(@class.Name); + } + PushBlock(CSharpBlockKind.Class); GenerateDeclarationCommon(@class); @@ -413,6 +421,9 @@ namespace CppSharp.Generators.CSharp exit: WriteCloseBraceIndent(); PopBlock(NewLineKind.BeforeNextBlock); + + if (typeMap != null) + Driver.TypeDatabase.TypeMaps.Add(@class.Name, typeMap); } private void GenerateClassMarshals(Class @class) diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index a22adb66..a1dc54df 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -42,6 +42,10 @@ public class CSharpTempTests : GeneratorTestFixture using (ComplexType complexType = TestFlag.Flag1) { } + using (var typeMappedWithOperator = new TypeMappedWithOperator()) + { + int i = typeMappedWithOperator | 5; + } } [Test] diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index 727fa5f5..89cd312c 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -663,3 +663,12 @@ void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual() void freeFunctionWithUnsupportedDefaultArg(Foo foo) { } + +TypeMappedWithOperator::TypeMappedWithOperator() +{ +} + +int TypeMappedWithOperator::operator |(int i) +{ + return 0; +} diff --git a/tests/CSharpTemp/CSharpTemp.cs b/tests/CSharpTemp/CSharpTemp.cs index 586e6502..9156c41b 100644 --- a/tests/CSharpTemp/CSharpTemp.cs +++ b/tests/CSharpTemp/CSharpTemp.cs @@ -78,6 +78,26 @@ namespace CppSharp.Tests } } + [TypeMap("TypeMappedWithOperator")] + public class TypeMappedWithOperator : TypeMap + { + public override string CSharpSignature(CSharpTypePrinterContext ctx) + { + // doesn't matter, we just need it to compile + return "int"; + } + + public override void CSharpMarshalToNative(MarshalContext ctx) + { + ctx.Return.Write(ctx.Parameter.Name); + } + + public override void CSharpMarshalToManaged(MarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + } + public class TestAttributesPass : TranslationUnitPass { public override bool VisitFunctionDecl(Function function) diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 726580ec..32d5341f 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -593,3 +593,10 @@ protected: }; void DLL_API freeFunctionWithUnsupportedDefaultArg(Foo foo = Foo()); + +class DLL_API TypeMappedWithOperator +{ +public: + TypeMappedWithOperator(); + int operator |(int i); +};