Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / System.ComponentModel.Composition.4.5 / src / ComponentModel / System / ComponentModel / Composition / Primitives / ExportedDelegate.cs
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Diagnostics.CodeAnalysis;
6 using System.Reflection;
7 using Microsoft.Internal;
8 using System.Linq.Expressions;
9
10 namespace System.ComponentModel.Composition.Primitives
11 {
12     [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
13     public class ExportedDelegate
14     {
15         private object _instance;
16         private MethodInfo _method;
17
18         protected ExportedDelegate() { }
19
20 #if FEATURE_CAS_APTCA
21         [System.Security.SecurityCritical]
22 #endif //FEATURE_CAS_APTCA
23         public ExportedDelegate(object instance, MethodInfo method)
24         {
25             Requires.NotNull(method, "method");
26
27             this._instance = instance;
28             this._method = method;
29         }
30
31         public virtual Delegate CreateDelegate(Type delegateType) 
32         {
33             Requires.NotNull(delegateType, "delegateType");
34
35             if (delegateType == typeof(Delegate) || delegateType == typeof(MulticastDelegate))
36             {
37                 delegateType = this.CreateStandardDelegateType();
38             }
39             
40             return Delegate.CreateDelegate(delegateType, this._instance, this._method, false);
41         }
42
43         private Type CreateStandardDelegateType()
44         {
45 #if MONOTOUCH
46             throw new NotImplementedException ();
47 #else
48             ParameterInfo[] parameters = this._method.GetParameters();
49
50             // This array should contains a lit of all argument types, and the last one is the return type (could be void)
51             Type[] parameterTypes = new Type[parameters.Length + 1];
52             parameterTypes[parameters.Length] = this._method.ReturnType;
53             for (int i = 0; i < parameters.Length; i++ )
54             {
55                 parameterTypes[i] = parameters[i].ParameterType;
56             }
57
58             return Expression.GetDelegateType(parameterTypes);
59 #endif
60         }
61     }
62 }