Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.ComponentModel.Composition / src / ComponentModel / System / ComponentModel / Composition / Primitives / ExportedDelegate.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.Diagnostics.CodeAnalysis;\r
6 using System.Reflection;\r
7 using Microsoft.Internal;\r
8 \r
9 namespace System.ComponentModel.Composition.Primitives\r
10 {\r
11     [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]\r
12     public class ExportedDelegate\r
13     {\r
14         private object _instance;\r
15         private MethodInfo _method;\r
16 \r
17         protected ExportedDelegate() { }\r
18 #if !SILVERLIGHT\r
19         [System.Security.SecurityCritical]\r
20 #endif\r
21         public ExportedDelegate(object instance, MethodInfo method)\r
22         {\r
23             Requires.NotNull(method, "method");\r
24 \r
25             this._instance = instance;\r
26             this._method = method;\r
27         }\r
28 \r
29         public virtual Delegate CreateDelegate(Type delegateType) \r
30         {\r
31             Requires.NotNull(delegateType, "delegateType");\r
32 \r
33             if (delegateType == typeof(Delegate) || delegateType == typeof(MulticastDelegate))\r
34             {\r
35                 Type funcOrAction = ConvertMethodInfoToFuncOrActionType(this._method);\r
36 \r
37                 if (funcOrAction != null)\r
38                 {\r
39                     delegateType = funcOrAction;\r
40                 }\r
41                 else\r
42                 {\r
43                     return null;\r
44                 }\r
45             }\r
46 \r
47             return Delegate.CreateDelegate(delegateType, this._instance, this._method, false);\r
48         }\r
49 \r
50         private static Type[] _funcTypes = \r
51         { \r
52             typeof(Func<>), typeof(Func<,>), typeof(Func<,,>), typeof(Func<,,,>), typeof(Func<,,,,>) \r
53 #if CLR40 && !SILVERLIGHT\r
54             , typeof(Func<,,,,,>), typeof(Func<,,,,,,>), typeof(Func<,,,,,,,>), typeof(Func<,,,,,,,,>)\r
55 #endif \r
56         };\r
57 \r
58         private static Type[] _actionTypes = \r
59         { \r
60             typeof(Action), typeof(Action<>), typeof(Action<,>), typeof(Action<,,>), typeof(Action<,,,>) \r
61 #if CLR40 && !SILVERLIGHT\r
62             , typeof(Action<,,,,>), typeof(Action<,,,,,>), typeof(Action<,,,,,,>), typeof(Action<,,,,,,,>)\r
63 #endif\r
64         };\r
65 \r
66         private static Type ConvertMethodInfoToFuncOrActionType(MethodInfo method)\r
67         {\r
68             ParameterInfo[] parameters = method.GetParameters();\r
69 \r
70             bool isVoid = method.ReturnType == typeof(void);\r
71             Type[] typeArray = isVoid ? _actionTypes : _funcTypes;\r
72 \r
73             if (parameters.Length >= typeArray.Length)\r
74             {\r
75                 return null;\r
76             }\r
77 \r
78             Type[] genericArgTypes = new Type[parameters.Length + (isVoid ? 0 : 1)];\r
79 \r
80             for (int i = 0; i < parameters.Length; i++)\r
81             {\r
82                 genericArgTypes[i] = parameters[i].ParameterType;\r
83             }\r
84 \r
85             if (!isVoid)\r
86             {\r
87                 genericArgTypes[parameters.Length] = method.ReturnType;\r
88             }\r
89 \r
90             Type delegateType = typeArray[parameters.Length].IsGenericType ?\r
91                 typeArray[parameters.Length].MakeGenericType(genericArgTypes) :\r
92                 typeArray[parameters.Length];\r
93 \r
94             return delegateType;\r
95         }\r
96     }\r
97 }\r