1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using System.Diagnostics.CodeAnalysis;
6 using System.Reflection;
7 using Microsoft.Internal;
8 using System.Linq.Expressions;
10 namespace System.ComponentModel.Composition.Primitives
12 [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
13 public class ExportedDelegate
15 private object _instance;
16 private MethodInfo _method;
18 protected ExportedDelegate() { }
21 [System.Security.SecurityCritical]
22 #endif //FEATURE_CAS_APTCA
23 public ExportedDelegate(object instance, MethodInfo method)
25 Requires.NotNull(method, "method");
27 this._instance = instance;
28 this._method = method;
31 public virtual Delegate CreateDelegate(Type delegateType)
33 Requires.NotNull(delegateType, "delegateType");
35 if (delegateType == typeof(Delegate) || delegateType == typeof(MulticastDelegate))
37 delegateType = this.CreateStandardDelegateType();
40 return Delegate.CreateDelegate(delegateType, this._instance, this._method, false);
43 private Type CreateStandardDelegateType()
46 throw new NotImplementedException ();
48 ParameterInfo[] parameters = this._method.GetParameters();
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++ )
55 parameterTypes[i] = parameters[i].ParameterType;
58 return Expression.GetDelegateType(parameterTypes);