diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 4880cac5..b129f490 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -1368,7 +1368,8 @@ namespace CppSharp.Generators.CSharp var name = ((Class) field.Namespace).Layout.Fields.First( f => f.FieldPtr == field.OriginalPtr).Name; string returnVar; - var arrayType = field.Type.Desugar() as ArrayType; + Type fieldType = field.Type.Desugar(); + var arrayType = fieldType as ArrayType; if (@class.IsValueType) { if (arrayType != null) @@ -1385,8 +1386,8 @@ namespace CppSharp.Generators.CSharp // Class field getter should return a reference object instead of a copy. Wrapping `returnVar` in // IntPtr ensures that non-copying object constructor is invoked. Class typeClass; - if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType && - !ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type)) + if (fieldType.TryGetClass(out typeClass) && !typeClass.IsValueType && + !ASTUtils.IsMappedToPrimitive(Context.TypeMaps, fieldType)) returnVar = $"new {TypePrinter.IntPtrType}(&{returnVar})"; } @@ -1410,9 +1411,9 @@ namespace CppSharp.Generators.CSharp Write("return "); var @return = marshal.Context.Return.ToString(); - if (field.Type.IsPointer()) + if (fieldType.IsPointer()) { - var final = field.Type.GetFinalPointee().Desugar(resolveTemplateSubstitution: false); + var final = fieldType.GetFinalPointee().Desugar(resolveTemplateSubstitution: false); var templateSubstitution = final as TemplateParameterSubstitutionType; if (templateSubstitution != null && returnType.Type.IsDependent) Write($"({templateSubstitution.ReplacedParameter.Parameter.Name}) (object) "); @@ -1422,13 +1423,13 @@ namespace CppSharp.Generators.CSharp !final.IsPrimitiveType(PrimitiveType.Char16) && !final.IsPrimitiveType(PrimitiveType.Char32)) || (!Context.Options.MarshalCharAsManagedChar && - !((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst)) && + !((PointerType) fieldType).QualifiedPointee.Qualifiers.IsConst)) && templateSubstitution == null) || - (!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst && + (!((PointerType) fieldType).QualifiedPointee.Qualifiers.IsConst && (final.IsPrimitiveType(PrimitiveType.WideChar) || final.IsPrimitiveType(PrimitiveType.Char16) || final.IsPrimitiveType(PrimitiveType.Char32)))) - Write($"({field.Type.GetPointee().Desugar()}*) "); + Write($"({fieldType.GetPointee().Desugar()}*) "); } WriteLine($"{@return};"); diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index f59039ad..0b42f982 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -24,6 +24,7 @@ public class CommonTests Assert.That(foo.B, Is.EqualTo(5)); Bar bar = foo; Assert.IsTrue(Bar.Item.Item1 == bar); + foo.FieldOfTypedefPrimitivePointer = null; using (var hasOverloadsWithDifferentPointerKindsToSameType = new HasOverloadsWithDifferentPointerKindsToSameType()) diff --git a/tests/Common/Common.h b/tests/Common/Common.h index b8d7cac0..1d5916e1 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -108,6 +108,9 @@ public: typedef Foo* FooPtr; + typedef uint8_t* typedefPrimitivePointer; + typedefPrimitivePointer fieldOfTypedefPrimitivePointer; + void TakesTypedefedPtr(FooPtr date); int TakesRef(const Foo& other);