@@ -79,25 +79,40 @@ public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext conte
79
79
80
80
private ExpressionSyntax GetNumElementsExpressionFromMarshallingInfo ( TypePositionInfo info , CountInfo count , StubCodeContext context )
81
81
{
82
- return count switch
82
+ switch ( count )
83
83
{
84
- SizeAndParamIndexInfo ( int size , SizeAndParamIndexInfo . UnspecifiedParam ) => GetConstSizeExpression ( size ) ,
85
- ConstSizeCountInfo ( int size ) => GetConstSizeExpression ( size ) ,
86
- SizeAndParamIndexInfo ( SizeAndParamIndexInfo . UnspecifiedConstSize , TypePositionInfo param ) => CheckedExpression ( SyntaxKind . CheckedExpression , GetExpressionForParam ( param ) ) ,
87
- SizeAndParamIndexInfo ( int size , TypePositionInfo param ) => CheckedExpression ( SyntaxKind . CheckedExpression , BinaryExpression ( SyntaxKind . AddExpression , GetConstSizeExpression ( size ) , GetExpressionForParam ( param ) ) ) ,
88
- CountElementCountInfo ( TypePositionInfo elementInfo ) => CheckedExpression ( SyntaxKind . CheckedExpression , GetExpressionForParam ( elementInfo ) ) ,
89
- _ => throw new MarshallingNotSupportedException ( info , context )
84
+ case SizeAndParamIndexInfo ( int size , SizeAndParamIndexInfo . UnspecifiedParam ) :
85
+ return GetConstSizeExpression ( size ) ;
86
+ case ConstSizeCountInfo ( int size ) :
87
+ return GetConstSizeExpression ( size ) ;
88
+ case SizeAndParamIndexInfo ( SizeAndParamIndexInfo . UnspecifiedConstSize , TypePositionInfo param ) :
90
89
{
91
- NotSupportedDetails = SR . ArraySizeMustBeSpecified
92
- } ,
93
- } ;
90
+ ExpressionSyntax expr = GetExpressionForParam ( param , out bool isIntType ) ;
91
+ return isIntType ? expr : CheckedExpression ( SyntaxKind . CheckedExpression , expr ) ;
92
+ }
93
+ case SizeAndParamIndexInfo ( int size , TypePositionInfo param ) :
94
+ return CheckedExpression ( SyntaxKind . CheckedExpression ,
95
+ BinaryExpression ( SyntaxKind . AddExpression ,
96
+ GetConstSizeExpression ( size ) ,
97
+ GetExpressionForParam ( param , out _ ) ) ) ;
98
+ case CountElementCountInfo ( TypePositionInfo elementInfo ) :
99
+ {
100
+ ExpressionSyntax expr = GetExpressionForParam ( elementInfo , out bool isIntType ) ;
101
+ return isIntType ? expr : CheckedExpression ( SyntaxKind . CheckedExpression , expr ) ;
102
+ }
103
+ default :
104
+ throw new MarshallingNotSupportedException ( info , context )
105
+ {
106
+ NotSupportedDetails = SR . ArraySizeMustBeSpecified
107
+ } ;
108
+ }
94
109
95
110
static LiteralExpressionSyntax GetConstSizeExpression ( int size )
96
111
{
97
112
return LiteralExpression ( SyntaxKind . NumericLiteralExpression , Literal ( size ) ) ;
98
113
}
99
114
100
- ExpressionSyntax GetExpressionForParam ( TypePositionInfo paramInfo )
115
+ ExpressionSyntax GetExpressionForParam ( TypePositionInfo paramInfo , out bool isIntType )
101
116
{
102
117
ExpressionSyntax numElementsExpression = GetIndexedNumElementsExpression (
103
118
context ,
@@ -132,7 +147,10 @@ ExpressionSyntax GetExpressionForParam(TypePositionInfo paramInfo)
132
147
} ;
133
148
}
134
149
135
- return CastExpression (
150
+ isIntType = specialType . SpecialType == SpecialType . System_Int32 ;
151
+ return isIntType
152
+ ? numElementsExpression
153
+ : CastExpression (
136
154
PredefinedType ( Token ( SyntaxKind . IntKeyword ) ) ,
137
155
ParenthesizedExpression ( numElementsExpression ) ) ;
138
156
}
0 commit comments