@@ -896,7 +896,8 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
896896 var marshal = new CSharpMarshalManagedToNativePrinter ( ctx ) ;
897897 ctx . Declaration = field ;
898898
899- var arrayType = field . Type . Desugar ( ) as ArrayType ;
899+ Type type = field . Type . Desugar ( ) ;
900+ var arrayType = type as ArrayType ;
900901
901902 if ( arrayType != null && @class . IsValueType )
902903 {
@@ -923,13 +924,20 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
923924
924925 if ( marshal . Context . Return . StringBuilder . Length > 0 )
925926 {
926- WriteLine ( "{0} = {1}{2};" , ctx . ReturnVarName ,
927- field . Type . IsPointer ( ) && field . Type . GetFinalPointee ( ) . IsPrimitiveType ( ) &&
928- ! CSharpTypePrinter . IsConstCharString ( field . Type ) ?
929- string . Format ( "({0}) " , CSharpTypePrinter . IntPtrType ) :
930- string . Empty ,
931- marshal . Context . Return ) ;
932-
927+ Write ( $ "{ ctx . ReturnVarName } = ") ;
928+ if ( type . IsPointer ( ) )
929+ {
930+ Type pointee = type . GetFinalPointee ( ) ;
931+ if ( pointee . IsPrimitiveType ( ) &&
932+ ! CSharpTypePrinter . IsConstCharString ( type ) )
933+ {
934+ Write ( $ "({ CSharpTypePrinter . IntPtrType } ) ") ;
935+ var templateSubstitution = pointee . Desugar ( false ) as TemplateParameterSubstitutionType ;
936+ if ( templateSubstitution != null )
937+ Write ( $ "(object) ") ;
938+ }
939+ }
940+ WriteLine ( $ "{ marshal . Context . Return } ;") ;
933941 }
934942
935943 if ( ( arrayType != null && @class . IsValueType ) || ctx . HasCodeBlock )
@@ -1191,21 +1199,26 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
11911199 if ( ctx . HasCodeBlock )
11921200 PushIndent ( ) ;
11931201
1202+ Write ( "return " ) ;
1203+
11941204 var @return = marshal . Context . Return . ToString ( ) ;
11951205 if ( field . Type . IsPointer ( ) )
11961206 {
1197- var final = field . Type . GetFinalPointee ( ) . Desugar ( ) ;
1198- if ( final . IsPrimitiveType ( ) && ! final . IsPrimitiveType ( PrimitiveType . Void ) &&
1207+ var final = field . Type . GetFinalPointee ( ) . Desugar ( resolveTemplateSubstitution : false ) ;
1208+ var templateSubstitution = final as TemplateParameterSubstitutionType ;
1209+ if ( templateSubstitution != null )
1210+ Write ( $ "({ templateSubstitution . ReplacedParameter . Parameter . Name } ) (object) ") ;
1211+ if ( ( final . IsPrimitiveType ( ) && ! final . IsPrimitiveType ( PrimitiveType . Void ) &&
11991212 ( ! final . IsPrimitiveType ( PrimitiveType . Char ) &&
12001213 ! final . IsPrimitiveType ( PrimitiveType . WideChar ) ||
12011214 ( ! Context . Options . MarshalCharAsManagedChar &&
1202- ! ( ( PointerType ) field . Type ) . QualifiedPointee . Qualifiers . IsConst ) ) )
1203- @return = string . Format ( "({0}*) {1}" , field . Type . GetPointee ( ) . Desugar ( ) , @return ) ;
1204- if ( ! ( ( PointerType ) field . Type ) . QualifiedPointee . Qualifiers . IsConst &&
1205- final . IsPrimitiveType ( PrimitiveType . WideChar ) )
1206- @return = string . Format ( "({0}*) {1}" , field . Type . GetPointee ( ) . Desugar ( ) , @return ) ;
1215+ ! ( ( PointerType ) field . Type ) . QualifiedPointee . Qualifiers . IsConst ) ) &&
1216+ templateSubstitution == null ) ||
1217+ ( ! ( ( PointerType ) field . Type ) . QualifiedPointee . Qualifiers . IsConst &&
1218+ final . IsPrimitiveType ( PrimitiveType . WideChar ) ) )
1219+ Write ( $ "({ field . Type . GetPointee ( ) . Desugar ( ) } *) " ) ;
12071220 }
1208- WriteLine ( "return {0 };", @return ) ;
1221+ WriteLine ( $ " { @return } ;") ;
12091222
12101223 if ( ( arrayType != null && @class . IsValueType ) || ctx . HasCodeBlock )
12111224 WriteCloseBraceIndent ( ) ;
0 commit comments