PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Dynamic/VarEnumSelector.cs

https://bitbucket.org/stefanrusek/xronos
C# | 416 lines | 265 code | 63 blank | 88 comment | 57 complexity | a6c472f8daa8170a055a306f99384baf MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. #if !SILVERLIGHT // ComObject
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. #if CODEPLEX_40
  20. using System.Linq.Expressions;
  21. #else
  22. using Microsoft.Linq.Expressions;
  23. #endif
  24. using System.Reflection;
  25. using System.Runtime.InteropServices;
  26. using System.Runtime.CompilerServices;
  27. #if !CODEPLEX_40
  28. using Microsoft.Runtime.CompilerServices;
  29. #endif
  30. #if CODEPLEX_40
  31. using System.Dynamic.Utils;
  32. #else
  33. using Microsoft.Scripting.Utils;
  34. #endif
  35. #if CODEPLEX_40
  36. namespace System.Dynamic {
  37. #else
  38. namespace Microsoft.Scripting {
  39. #endif
  40. /// <summary>
  41. /// If a managed user type (as opposed to a primitive type or a COM object) is passed as an argument to a COM call, we need
  42. /// to determine the VarEnum type we will marshal it as. We have the following options:
  43. /// 1. Raise an exception. Languages with their own version of primitive types would not be able to call
  44. /// COM methods using the language's types (for eg. strings in IronRuby are not System.String). An explicit
  45. /// cast would be needed.
  46. /// 2. We could marshal it as VT_DISPATCH. Then COM code will be able to access all the APIs in a late-bound manner,
  47. /// but old COM components will probably malfunction if they expect a primitive type.
  48. /// 3. We could guess which primitive type is the closest match. This will make COM components be as easily
  49. /// accessible as .NET methods.
  50. /// 4. We could use the type library to check what the expected type is. However, the type library may not be available.
  51. ///
  52. /// VarEnumSelector implements option # 3
  53. /// </summary>
  54. internal class VarEnumSelector {
  55. private readonly VariantBuilder[] _variantBuilders;
  56. private static readonly Dictionary<VarEnum, Type> _ComToManagedPrimitiveTypes = CreateComToManagedPrimitiveTypes();
  57. private static readonly IList<IList<VarEnum>> _ComPrimitiveTypeFamilies = CreateComPrimitiveTypeFamilies();
  58. internal VarEnumSelector(Type[] explicitArgTypes) {
  59. _variantBuilders = new VariantBuilder[explicitArgTypes.Length];
  60. for (int i = 0; i < explicitArgTypes.Length; i++) {
  61. _variantBuilders[i] = GetVariantBuilder(explicitArgTypes[i]);
  62. }
  63. }
  64. internal VariantBuilder[] VariantBuilders {
  65. get {
  66. return _variantBuilders;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets the managed type that an object needs to be coverted to in order for it to be able
  71. /// to be represented as a Variant.
  72. ///
  73. /// In general, there is a many-to-many mapping between Type and VarEnum. However, this method
  74. /// returns a simple mapping that is needed for the current implementation. The reason for the
  75. /// many-to-many relation is:
  76. /// 1. Int32 maps to VT_I4 as well as VT_ERROR, and Decimal maps to VT_DECIMAL and VT_CY. However,
  77. /// this changes if you throw the wrapper types into the mix.
  78. /// 2. There is no Type to represent COM types. __ComObject is a private type, and Object is too
  79. /// general.
  80. /// </summary>
  81. internal static Type GetManagedMarshalType(VarEnum varEnum) {
  82. Debug.Assert((varEnum & VarEnum.VT_BYREF) == 0);
  83. if (varEnum == VarEnum.VT_CY) {
  84. return typeof(CurrencyWrapper);
  85. }
  86. if (Variant.IsPrimitiveType(varEnum)) {
  87. return _ComToManagedPrimitiveTypes[varEnum];
  88. }
  89. switch (varEnum) {
  90. case VarEnum.VT_EMPTY:
  91. case VarEnum.VT_NULL:
  92. case VarEnum.VT_UNKNOWN:
  93. case VarEnum.VT_DISPATCH:
  94. case VarEnum.VT_VARIANT:
  95. return typeof(Object);
  96. case VarEnum.VT_ERROR:
  97. return typeof(ErrorWrapper);
  98. default:
  99. throw Error.UnexpectedVarEnum(varEnum);
  100. }
  101. }
  102. private static Dictionary<VarEnum, Type> CreateComToManagedPrimitiveTypes() {
  103. Dictionary<VarEnum, Type> dict = new Dictionary<VarEnum, Type>();
  104. #region Generated ComToManagedPrimitiveTypes
  105. // *** BEGIN GENERATED CODE ***
  106. // generated by function: gen_ComToManagedPrimitiveTypes from: generate_comdispatch.py
  107. dict[VarEnum.VT_I1] = typeof(SByte);
  108. dict[VarEnum.VT_I2] = typeof(Int16);
  109. dict[VarEnum.VT_I4] = typeof(Int32);
  110. dict[VarEnum.VT_I8] = typeof(Int64);
  111. dict[VarEnum.VT_UI1] = typeof(Byte);
  112. dict[VarEnum.VT_UI2] = typeof(UInt16);
  113. dict[VarEnum.VT_UI4] = typeof(UInt32);
  114. dict[VarEnum.VT_UI8] = typeof(UInt64);
  115. dict[VarEnum.VT_INT] = typeof(IntPtr);
  116. dict[VarEnum.VT_UINT] = typeof(UIntPtr);
  117. dict[VarEnum.VT_BOOL] = typeof(bool);
  118. dict[VarEnum.VT_R4] = typeof(Single);
  119. dict[VarEnum.VT_R8] = typeof(Double);
  120. dict[VarEnum.VT_DECIMAL] = typeof(Decimal);
  121. dict[VarEnum.VT_DATE] = typeof(DateTime);
  122. dict[VarEnum.VT_BSTR] = typeof(String);
  123. // *** END GENERATED CODE ***
  124. #endregion
  125. dict[VarEnum.VT_CY] = typeof(CurrencyWrapper);
  126. dict[VarEnum.VT_ERROR] = typeof(ErrorWrapper);
  127. return dict;
  128. }
  129. #region Primitive COM types
  130. /// <summary>
  131. /// Creates a family of COM types such that within each family, there is a completely non-lossy
  132. /// conversion from a type to an earlier type in the family.
  133. /// </summary>
  134. private static IList<IList<VarEnum>> CreateComPrimitiveTypeFamilies() {
  135. VarEnum[][] typeFamilies = new VarEnum[][] {
  136. new VarEnum[] { VarEnum.VT_I8, VarEnum.VT_I4, VarEnum.VT_I2, VarEnum.VT_I1 },
  137. new VarEnum[] { VarEnum.VT_UI8, VarEnum.VT_UI4, VarEnum.VT_UI2, VarEnum.VT_UI1 },
  138. new VarEnum[] { VarEnum.VT_INT },
  139. new VarEnum[] { VarEnum.VT_UINT },
  140. new VarEnum[] { VarEnum.VT_BOOL },
  141. new VarEnum[] { VarEnum.VT_DATE },
  142. new VarEnum[] { VarEnum.VT_R8, VarEnum.VT_R4 },
  143. new VarEnum[] { VarEnum.VT_DECIMAL },
  144. new VarEnum[] { VarEnum.VT_BSTR },
  145. // wrappers
  146. new VarEnum[] { VarEnum.VT_CY },
  147. new VarEnum[] { VarEnum.VT_ERROR },
  148. };
  149. return typeFamilies;
  150. }
  151. /// <summary>
  152. /// Get the (one representative type for each) primitive type families that the argument can be converted to
  153. /// </summary>
  154. private static List<VarEnum> GetConversionsToComPrimitiveTypeFamilies(Type argumentType) {
  155. List<VarEnum> compatibleComTypes = new List<VarEnum>();
  156. foreach (IList<VarEnum> typeFamily in _ComPrimitiveTypeFamilies) {
  157. foreach (VarEnum candidateType in typeFamily) {
  158. Type candidateManagedType = _ComToManagedPrimitiveTypes[candidateType];
  159. if (TypeUtils.IsImplicitlyConvertible(argumentType, candidateManagedType, true)) {
  160. compatibleComTypes.Add(candidateType);
  161. // Move on to the next type family. We need atmost one type from each family
  162. break;
  163. }
  164. }
  165. }
  166. return compatibleComTypes;
  167. }
  168. /// <summary>
  169. /// If there is more than one type family that the argument can be converted to, we will throw a
  170. /// AmbiguousMatchException instead of randomly picking a winner.
  171. /// </summary>
  172. private static void CheckForAmbiguousMatch(Type argumentType, List<VarEnum> compatibleComTypes) {
  173. if (compatibleComTypes.Count <= 1) {
  174. return;
  175. }
  176. String typeNames = "";
  177. for (int i = 0; i < compatibleComTypes.Count; i++) {
  178. string typeName = _ComToManagedPrimitiveTypes[compatibleComTypes[i]].Name;
  179. if (i == (compatibleComTypes.Count - 1)) {
  180. typeNames += " and ";
  181. } else if (i != 0) {
  182. typeNames += ", ";
  183. }
  184. typeNames += typeName;
  185. }
  186. throw Error.AmbiguousConversion(argumentType.Name, typeNames);
  187. }
  188. private static bool TryGetPrimitiveComType(Type argumentType, out VarEnum primitiveVarEnum) {
  189. // Look for an exact match with a COM primitive type
  190. foreach (KeyValuePair<VarEnum, Type> kvp in _ComToManagedPrimitiveTypes) {
  191. if (kvp.Value == argumentType) {
  192. primitiveVarEnum = kvp.Key;
  193. return true;
  194. }
  195. }
  196. primitiveVarEnum = VarEnum.VT_VOID; // error
  197. return false;
  198. }
  199. /// <summary>
  200. /// Is there a unique primitive type that has the best conversion for the argument
  201. /// </summary>
  202. private static bool TryGetPrimitiveComTypeViaConversion(Type argumentType, out VarEnum primitiveVarEnum) {
  203. // Look for a unique type family that the argument can be converted to.
  204. List<VarEnum> compatibleComTypes = GetConversionsToComPrimitiveTypeFamilies(argumentType);
  205. CheckForAmbiguousMatch(argumentType, compatibleComTypes);
  206. if (compatibleComTypes.Count == 1) {
  207. primitiveVarEnum = compatibleComTypes[0];
  208. return true;
  209. }
  210. primitiveVarEnum = VarEnum.VT_VOID; // error
  211. return false;
  212. }
  213. #endregion
  214. // Type.InvokeMember tries to marshal objects as VT_DISPATCH, and falls back to VT_UNKNOWN
  215. // VT_RECORD here just indicates that we have user defined type.
  216. // We will try VT_DISPATCH and then call GetNativeVariantForObject.
  217. const VarEnum VT_DEFAULT = VarEnum.VT_RECORD;
  218. private VarEnum GetComType(ref Type argumentType) {
  219. if (argumentType == typeof(Missing)) {
  220. //actual variant type will be VT_ERROR | E_PARAMNOTFOUND
  221. return VarEnum.VT_RECORD;
  222. }
  223. if (argumentType.IsArray) {
  224. //actual variant type will be VT_ARRAY | VT_<ELEMENT_TYPE>
  225. return VarEnum.VT_ARRAY;
  226. }
  227. if (argumentType == typeof(UnknownWrapper)) {
  228. return VarEnum.VT_UNKNOWN;
  229. } else if (argumentType == typeof(DispatchWrapper)) {
  230. return VarEnum.VT_DISPATCH;
  231. } else if (argumentType == typeof(VariantWrapper)) {
  232. return VarEnum.VT_VARIANT;
  233. } else if (argumentType == typeof(BStrWrapper)) {
  234. return VarEnum.VT_BSTR;
  235. } else if (argumentType == typeof(ErrorWrapper)) {
  236. return VarEnum.VT_ERROR;
  237. } else if (argumentType == typeof(CurrencyWrapper)) {
  238. return VarEnum.VT_CY;
  239. }
  240. // Many languages require an explicit cast for an enum to be used as the underlying type.
  241. // However, we want to allow this conversion for COM without requiring an explicit cast
  242. // so that enums from interop assemblies can be used as arguments.
  243. if (argumentType.IsEnum) {
  244. argumentType = Enum.GetUnderlyingType(argumentType);
  245. return GetComType(ref argumentType);
  246. }
  247. // COM cannot express valuetype nulls so we will convert to underlying type
  248. // it will throw if there is no value
  249. if (TypeUtils.IsNullableType(argumentType)) {
  250. argumentType = TypeUtils.GetNonNullableType(argumentType);
  251. return GetComType(ref argumentType);
  252. }
  253. //generic types cannot be exposed to COM so they do not implement COM interfaces.
  254. if (argumentType.IsGenericType) {
  255. return VarEnum.VT_UNKNOWN;
  256. }
  257. VarEnum primitiveVarEnum;
  258. if (TryGetPrimitiveComType(argumentType, out primitiveVarEnum)) {
  259. return primitiveVarEnum;
  260. }
  261. // We could not find a way to marshal the type as a specific COM type
  262. return VT_DEFAULT;
  263. }
  264. /// <summary>
  265. /// Get the COM Variant type that argument should be marshaled as for a call to COM
  266. /// </summary>
  267. private VariantBuilder GetVariantBuilder(Type argumentType) {
  268. //argumentType is coming from MarshalType, null means the dynamic object holds
  269. //a null value and not byref
  270. if (argumentType == null) {
  271. return new VariantBuilder(VarEnum.VT_EMPTY, new NullArgBuilder());
  272. }
  273. if (argumentType == typeof(DBNull)) {
  274. return new VariantBuilder(VarEnum.VT_NULL, new NullArgBuilder());
  275. }
  276. ArgBuilder argBuilder;
  277. if (argumentType.IsByRef) {
  278. Type elementType = argumentType.GetElementType();
  279. VarEnum elementVarEnum;
  280. if (elementType == typeof(object) || elementType == typeof(DBNull)) {
  281. //no meaningful value to pass ByRef.
  282. //perhaps the calee will replace it with something.
  283. //need to pass as a variant reference
  284. elementVarEnum = VarEnum.VT_VARIANT;
  285. } else {
  286. elementVarEnum = GetComType(ref elementType);
  287. }
  288. argBuilder = GetSimpleArgBuilder(elementType, elementVarEnum);
  289. return new VariantBuilder(elementVarEnum | VarEnum.VT_BYREF, argBuilder);
  290. }
  291. VarEnum varEnum = GetComType(ref argumentType);
  292. argBuilder = GetByValArgBuilder(argumentType, ref varEnum);
  293. return new VariantBuilder(varEnum, argBuilder);
  294. }
  295. // This helper is called when we are looking for a ByVal marhsalling
  296. // In a ByVal case we can take into account conversions or IConvertible if all other
  297. // attempts to find marshalling type failed
  298. private static ArgBuilder GetByValArgBuilder(Type elementType, ref VarEnum elementVarEnum) {
  299. // if VT indicates that marshalling type is unknown
  300. if (elementVarEnum == VT_DEFAULT) {
  301. //trying to find a conversion.
  302. VarEnum convertibleTo;
  303. if (TryGetPrimitiveComTypeViaConversion(elementType, out convertibleTo)) {
  304. elementVarEnum = convertibleTo;
  305. Type marshalType = GetManagedMarshalType(elementVarEnum);
  306. return new ConversionArgBuilder(elementType, GetSimpleArgBuilder(marshalType, elementVarEnum));
  307. }
  308. //checking for IConvertible.
  309. if (typeof(IConvertible).IsAssignableFrom(elementType)) {
  310. return new ConvertibleArgBuilder();
  311. }
  312. }
  313. return GetSimpleArgBuilder(elementType, elementVarEnum);
  314. }
  315. // This helper can produce a builder for types that are directly supported by Variant.
  316. private static SimpleArgBuilder GetSimpleArgBuilder(Type elementType, VarEnum elementVarEnum) {
  317. SimpleArgBuilder argBuilder;
  318. switch (elementVarEnum) {
  319. case VarEnum.VT_BSTR:
  320. argBuilder = new StringArgBuilder(elementType);
  321. break;
  322. case VarEnum.VT_BOOL:
  323. argBuilder = new BoolArgBuilder(elementType);
  324. break;
  325. case VarEnum.VT_DATE:
  326. argBuilder = new DateTimeArgBuilder(elementType);
  327. break;
  328. case VarEnum.VT_CY:
  329. argBuilder = new CurrencyArgBuilder(elementType);
  330. break;
  331. case VarEnum.VT_DISPATCH:
  332. argBuilder = new DispatchArgBuilder(elementType);
  333. break;
  334. case VarEnum.VT_UNKNOWN:
  335. argBuilder = new UnknownArgBuilder(elementType);
  336. break;
  337. case VarEnum.VT_VARIANT:
  338. case VarEnum.VT_ARRAY:
  339. case VarEnum.VT_RECORD:
  340. argBuilder = new VariantArgBuilder(elementType);
  341. break;
  342. case VarEnum.VT_ERROR:
  343. argBuilder = new ErrorArgBuilder(elementType);
  344. break;
  345. default:
  346. argBuilder = new SimpleArgBuilder(elementType);
  347. break;
  348. }
  349. return argBuilder;
  350. }
  351. }
  352. }
  353. #endif