1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
15 using System; using Microsoft;
18 #if !SILVERLIGHT // ComObject
20 using System.Collections.Generic;
22 using System.Linq.Expressions;
24 using Microsoft.Linq.Expressions;
26 using System.Runtime.InteropServices;
27 using System.Diagnostics;
29 using System.Dynamic.Utils;
31 using Microsoft.Scripting.Utils;
35 namespace System.Dynamic {
37 namespace Microsoft.Scripting {
40 internal class DispatchArgBuilder : SimpleArgBuilder {
41 private readonly bool _isWrapper;
43 internal DispatchArgBuilder(Type parameterType)
44 : base(parameterType) {
46 _isWrapper = parameterType == typeof(DispatchWrapper);
49 internal override Expression Marshal(Expression parameter) {
50 parameter = base.Marshal(parameter);
52 // parameter.WrappedObject
54 parameter = Expression.Property(
55 Helpers.Convert(parameter, typeof(DispatchWrapper)),
56 typeof(DispatchWrapper).GetProperty("WrappedObject")
60 return Helpers.Convert(parameter, typeof(object));
63 internal override Expression MarshalToRef(Expression parameter) {
64 parameter = Marshal(parameter);
66 // parameter == null ? IntPtr.Zero : Marshal.GetIDispatchForObject(parameter);
67 return Expression.Condition(
68 Expression.Equal(parameter, Expression.Constant(null)),
69 Expression.Constant(IntPtr.Zero),
71 typeof(Marshal).GetMethod("GetIDispatchForObject"),
77 internal override Expression UnmarshalFromRef(Expression value) {
78 // value == IntPtr.Zero ? null : Marshal.GetObjectForIUnknown(value);
79 Expression unmarshal = Expression.Condition(
80 Expression.Equal(value, Expression.Constant(IntPtr.Zero)),
81 Expression.Constant(null),
83 typeof(Marshal).GetMethod("GetObjectForIUnknown"),
89 unmarshal = Expression.New(
90 typeof(DispatchWrapper).GetConstructor(new Type[] { typeof(object) }),
95 return base.UnmarshalFromRef(unmarshal);