@@ -701,46 +701,61 @@ private void MarshalRefClass(Class @class)
701701 @interface . IsInterface )
702702 paramInstance = $ "{ param } .__PointerTo{ @interface . OriginalClass . Name } ";
703703 else
704- paramInstance = $@ "{ param } .{ Helpers . InstanceIdentifier } ";
705- if ( type . IsAddress ( ) )
704+ paramInstance = $ "{ param } .{ Helpers . InstanceIdentifier } ";
705+
706+ if ( ! type . IsAddress ( ) )
707+ {
708+ Context . Before . WriteLine ( $ "if (ReferenceEquals({ Context . Parameter . Name } , null))") ;
709+ Context . Before . WriteLineIndent (
710+ $@ "throw new global::System.ArgumentNullException(""{
711+ Context . Parameter . Name } "", ""Cannot be null because it is passed by value."");" ) ;
712+ var realClass = @class . OriginalClass ?? @class ;
713+ var qualifiedIdentifier = typePrinter . PrintNative ( realClass ) ;
714+ Context . Return . Write ( $ "*({ qualifiedIdentifier } *) { paramInstance } ") ;
715+ return ;
716+ }
717+
718+ Class decl ;
719+ if ( type . TryGetClass ( out decl ) && decl . IsValueType )
720+ {
721+ Context . Return . Write ( paramInstance ) ;
722+ return ;
723+ }
724+
725+ if ( type . IsPointer ( ) )
706726 {
707- Class decl ;
708- if ( type . TryGetClass ( out decl ) && decl . IsValueType )
727+ if ( Context . Parameter . IsIndirect )
728+ {
729+ Context . Before . WriteLine ( $ "if (ReferenceEquals({ Context . Parameter . Name } , null))") ;
730+ Context . Before . WriteLineIndent (
731+ $@ "throw new global::System.ArgumentNullException(""{
732+ Context . Parameter . Name } "", ""Cannot be null because it is passed by value."");" ) ;
709733 Context . Return . Write ( paramInstance ) ;
734+ }
710735 else
711736 {
712- if ( type . IsPointer ( ) )
713- {
714- Context . Return . Write ( "{0}{1}" ,
715- method != null && method . OperatorKind == CXXOperatorKind . EqualEqual
716- ? string . Empty
717- : $ "ReferenceEquals({ param } , null) ? global::System.IntPtr.Zero : ",
718- paramInstance ) ;
719- }
720- else
721- {
722- if ( method == null ||
723- // redundant for comparison operators, they are handled in a special way
724- ( method . OperatorKind != CXXOperatorKind . EqualEqual &&
725- method . OperatorKind != CXXOperatorKind . ExclaimEqual ) )
726- {
727- Context . Before . WriteLine ( "if (ReferenceEquals({0}, null))" , param ) ;
728- Context . Before . WriteLineIndent (
729- "throw new global::System.ArgumentNullException(\" {0}\" , " +
730- "\" Cannot be null because it is a C++ reference (&).\" );" ,
731- param ) ;
732- }
733- Context . Return . Write ( paramInstance ) ;
734- }
737+ Context . Return . Write ( "{0}{1}" ,
738+ method != null && method . OperatorKind == CXXOperatorKind . EqualEqual
739+ ? string . Empty
740+ : $ "ReferenceEquals({ param } , null) ? global::System.IntPtr.Zero : ",
741+ paramInstance ) ;
735742 }
736743 return ;
737744 }
738745
739- var realClass = @class . OriginalClass ?? @class ;
740- var qualifiedIdentifier = typePrinter . PrintNative ( realClass ) ;
741- Context . Return . Write (
742- "ReferenceEquals({0}, null) ? new {1}() : *({1}*) {2}" ,
743- param , qualifiedIdentifier , paramInstance ) ;
746+ if ( method == null ||
747+ // redundant for comparison operators, they are handled in a special way
748+ ( method . OperatorKind != CXXOperatorKind . EqualEqual &&
749+ method . OperatorKind != CXXOperatorKind . ExclaimEqual ) )
750+ {
751+ Context . Before . WriteLine ( $ "if (ReferenceEquals({ Context . Parameter . Name } , null))") ;
752+ Context . Before . WriteLineIndent (
753+ $@ "throw new global::System.ArgumentNullException(""{
754+ Context . Parameter . Name } "", ""Cannot be null because it is a C++ reference (&)."");" ,
755+ param ) ;
756+ }
757+
758+ Context . Return . Write ( paramInstance ) ;
744759 }
745760
746761 private void MarshalValueClass ( )
0 commit comments