@@ -38,22 +38,22 @@ public static IEnumerable<ExcelFunction> ProcessAsyncRegistrations(this IEnumera
38
38
if ( ReturnsObservable ( reg . FunctionLambda ) )
39
39
{
40
40
ParameterConversionRegistration . ApplyParameterConversions ( reg , ObjectHandles . ObjectHandleRegistration . GetParameterConversionConfiguration ( ) ) ;
41
- reg . FunctionLambda = WrapMethodObservable ( reg . FunctionLambda ) ;
41
+ reg . FunctionLambda = WrapMethodObservable ( reg . FunctionLambda , reg . Return . CustomAttributes ) ;
42
42
}
43
43
else if ( ReturnsTask ( reg . FunctionLambda ) || reg . FunctionAttribute is ExcelAsyncFunctionAttribute )
44
44
{
45
45
ParameterConversionRegistration . ApplyParameterConversions ( reg , ObjectHandles . ObjectHandleRegistration . GetParameterConversionConfiguration ( ) ) ;
46
46
if ( HasCancellationToken ( reg . FunctionLambda ) )
47
47
{
48
48
reg . FunctionLambda = useNativeAsync ? WrapMethodNativeAsyncTaskWithCancellation ( reg . FunctionLambda )
49
- : WrapMethodRunTaskWithCancellation ( reg . FunctionLambda ) ;
49
+ : WrapMethodRunTaskWithCancellation ( reg . FunctionLambda , reg . Return . CustomAttributes ) ;
50
50
// Also need to strip out the info for the last argument which is the CancellationToken
51
51
reg . ParameterRegistrations . RemoveAt ( reg . ParameterRegistrations . Count - 1 ) ;
52
52
}
53
53
else
54
54
{
55
55
reg . FunctionLambda = useNativeAsync ? WrapMethodNativeAsyncTask ( reg . FunctionLambda )
56
- : WrapMethodRunTask ( reg . FunctionLambda ) ;
56
+ : WrapMethodRunTask ( reg . FunctionLambda , reg . Return . CustomAttributes ) ;
57
57
}
58
58
}
59
59
// else do nothing to this registration
@@ -84,7 +84,7 @@ static bool HasCancellationToken(LambdaExpression functionLambda)
84
84
return pis . Any ( ) && pis . Last ( ) . Type == typeof ( CancellationToken ) ;
85
85
}
86
86
87
- static LambdaExpression WrapMethodRunTask ( LambdaExpression functionLambda )
87
+ static LambdaExpression WrapMethodRunTask ( LambdaExpression functionLambda , List < object > returnCustomAttributes )
88
88
{
89
89
/* Either, from a lambda expression wrapping a method that looks like this:
90
90
*
@@ -116,7 +116,15 @@ static LambdaExpression WrapMethodRunTask(LambdaExpression functionLambda)
116
116
*/
117
117
118
118
bool returnsTask = ReturnsTask ( functionLambda ) ;
119
- bool userType = ObjectHandles . TaskObjectHandler . IsUserType ( returnsTask ? functionLambda . ReturnType . GetGenericArguments ( ) [ 0 ] : functionLambda . ReturnType ) ;
119
+ Type returnType = returnsTask ? functionLambda . ReturnType . GetGenericArguments ( ) [ 0 ] : functionLambda . ReturnType ;
120
+ bool userType = ObjectHandles . TaskObjectHandler . IsUserType ( returnType ) ;
121
+ if ( userType )
122
+ {
123
+ if ( ! ObjectHandles . ObjectHandleRegistration . HasExcelHandle ( returnCustomAttributes ) )
124
+ throw new Exception ( $ "Unsupported task return type { returnType } .") ;
125
+
126
+ ObjectHandles . ObjectHandleRegistration . ClearExcelHandle ( returnCustomAttributes ) ;
127
+ }
120
128
121
129
// Either RunTask or RunAsTask, depending on whether the method returns Task<string> or string
122
130
string runMethodName = returnsTask ? "RunTask" : "RunAsTask" ;
@@ -155,7 +163,7 @@ static LambdaExpression WrapMethodRunTask(LambdaExpression functionLambda)
155
163
return Expression . Lambda ( callTaskRun , functionLambda . Name , newParams ) ;
156
164
}
157
165
158
- static LambdaExpression WrapMethodRunTaskWithCancellation ( LambdaExpression functionLambda )
166
+ static LambdaExpression WrapMethodRunTaskWithCancellation ( LambdaExpression functionLambda , List < object > returnCustomAttributes )
159
167
{
160
168
/* Either, from a lambda expression that looks like this:
161
169
*
@@ -187,7 +195,15 @@ static LambdaExpression WrapMethodRunTaskWithCancellation(LambdaExpression funct
187
195
*/
188
196
189
197
bool returnsTask = ReturnsTask ( functionLambda ) ;
190
- bool userType = ObjectHandles . TaskObjectHandler . IsUserType ( returnsTask ? functionLambda . ReturnType . GetGenericArguments ( ) [ 0 ] : functionLambda . ReturnType ) ;
198
+ Type returnType = returnsTask ? functionLambda . ReturnType . GetGenericArguments ( ) [ 0 ] : functionLambda . ReturnType ;
199
+ bool userType = ObjectHandles . TaskObjectHandler . IsUserType ( returnType ) ;
200
+ if ( userType )
201
+ {
202
+ if ( ! ObjectHandles . ObjectHandleRegistration . HasExcelHandle ( returnCustomAttributes ) )
203
+ throw new Exception ( $ "Unsupported task return type { returnType } .") ;
204
+
205
+ ObjectHandles . ObjectHandleRegistration . ClearExcelHandle ( returnCustomAttributes ) ;
206
+ }
191
207
192
208
// Either RunTask or RunAsTask, depending on whether the method returns Task<string> or string
193
209
string runMethodName = returnsTask ? "RunTaskWithCancellation" : "RunAsTaskWithCancellation" ;
@@ -341,7 +357,7 @@ static LambdaExpression WrapMethodNativeAsyncTaskWithCancellation(LambdaExpressi
341
357
return Expression . Lambda ( callTaskRun , functionLambda . Name , allParams ) ;
342
358
}
343
359
344
- static LambdaExpression WrapMethodObservable ( LambdaExpression functionLambda )
360
+ static LambdaExpression WrapMethodObservable ( LambdaExpression functionLambda , List < object > returnCustomAttributes )
345
361
{
346
362
/* Either, from a lambda expression that looks like this:
347
363
*
@@ -359,10 +375,19 @@ static LambdaExpression WrapMethodObservable(LambdaExpression functionLambda)
359
375
*/
360
376
361
377
// mi returns some kind of IObservable<T>. What is T?
362
- var returnType = functionLambda . ReturnType . GetGenericArguments ( ) [ 0 ] ;
378
+ Type returnType = functionLambda . ReturnType . GetGenericArguments ( ) [ 0 ] ;
379
+ bool userType = ObjectHandles . TaskObjectHandler . IsUserType ( returnType ) ;
380
+ if ( userType )
381
+ {
382
+ if ( ! ObjectHandles . ObjectHandleRegistration . HasExcelHandle ( returnCustomAttributes ) )
383
+ throw new Exception ( $ "Unsupported observable return type { returnType } .") ;
384
+
385
+ ObjectHandles . ObjectHandleRegistration . ClearExcelHandle ( returnCustomAttributes ) ;
386
+ }
387
+
363
388
// Build up the Observe method with the right generic type argument
364
389
var obsMethod = typeof ( ExcelAsyncUtil )
365
- . GetMember ( " Observe", MemberTypes . Method , BindingFlags . Static | BindingFlags . Public )
390
+ . GetMember ( userType ? "ObserveObject" : " Observe", MemberTypes . Method , BindingFlags . Static | BindingFlags . Public | BindingFlags . NonPublic )
366
391
. Cast < MethodInfo > ( ) . First ( i => i . IsGenericMethodDefinition )
367
392
. MakeGenericMethod ( returnType ) ;
368
393
0 commit comments