File size: 17,271 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Management.Automation.Language;
using System.Runtime.InteropServices;
namespace System.Management.Automation.ComInterop
{
/// <summary>
/// Provides helper methods to bind COM objects dynamically.
/// </summary>
internal static class ComBinder
{
/// <summary>
/// Determines if an object is a COM object.
/// </summary>
/// <param name="value">The object to test.</param>
/// <returns>True if the object is a COM object, false otherwise.</returns>
public static bool IsComObject(object value)
{
return value != null && Marshal.IsComObject(value);
}
/// <summary>
/// Tries to perform binding of the dynamic get member operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="GetMemberBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="instance">The target of the dynamic operation. </param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <param name="delayInvocation">True if member evaluation may be delayed.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryBindGetMember(GetMemberBinder binder, DynamicMetaObject instance, out DynamicMetaObject result, bool delayInvocation)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
if (TryGetMetaObject(ref instance))
{
var comGetMember = new ComGetMemberBinder(binder, delayInvocation);
result = instance.BindGetMember(comGetMember);
if (result.Expression.Type.IsValueType)
{
result = new DynamicMetaObject(
Expression.Convert(result.Expression, typeof(object)),
result.Restrictions
);
}
return true;
}
result = null;
return false;
}
/// <summary>
/// Tries to perform binding of the dynamic set member operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="SetMemberBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="instance">The target of the dynamic operation.</param>
/// <param name="value">The <see cref="DynamicMetaObject"/> representing the value for the set member operation.</param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryBindSetMember(SetMemberBinder binder, DynamicMetaObject instance, DynamicMetaObject value, out DynamicMetaObject result)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
Requires.NotNull(value);
if (TryGetMetaObject(ref instance))
{
result = instance.BindSetMember(binder, value);
result = new DynamicMetaObject(result.Expression, result.Restrictions.Merge(value.PSGetMethodArgumentRestriction()));
return true;
}
result = null;
return false;
}
/// <summary>
/// Tries to perform binding of the dynamic invoke operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="InvokeBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="instance">The target of the dynamic operation. </param>
/// <param name="args">An array of <see cref="DynamicMetaObject"/> instances - arguments to the invoke member operation.</param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryBindInvoke(InvokeBinder binder, DynamicMetaObject instance, DynamicMetaObject[] args, out DynamicMetaObject result)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
Requires.NotNull(args);
if (TryGetMetaObjectInvoke(ref instance))
{
result = instance.BindInvoke(binder, args);
return true;
}
result = null;
return false;
}
/// <summary>
/// Tries to perform binding of the dynamic invoke member operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="InvokeMemberBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="isSetProperty">True if this is for setting a property, false otherwise.</param>
/// <param name="instance">The target of the dynamic operation. </param>
/// <param name="args">An array of <see cref="DynamicMetaObject"/> instances - arguments to the invoke member operation.</param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryBindInvokeMember(InvokeMemberBinder binder, bool isSetProperty, DynamicMetaObject instance, DynamicMetaObject[] args, out DynamicMetaObject result)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
Requires.NotNull(args);
if (TryGetMetaObject(ref instance))
{
var comInvokeMember = new ComInvokeMemberBinder(binder, isSetProperty);
result = instance.BindInvokeMember(comInvokeMember, args);
BindingRestrictions argRestrictions = args.Aggregate(
BindingRestrictions.Empty, (current, arg) => current.Merge(arg.PSGetMethodArgumentRestriction()));
var newRestrictions = result.Restrictions.Merge(argRestrictions);
if (result.Expression.Type.IsValueType)
{
result = new DynamicMetaObject(
Expression.Convert(result.Expression, typeof(object)),
newRestrictions
);
}
else
{
result = new DynamicMetaObject(result.Expression, newRestrictions);
}
return true;
}
result = null;
return false;
}
/// <summary>
/// Tries to perform binding of the dynamic get index operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="GetIndexBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="instance">The target of the dynamic operation. </param>
/// <param name="args">An array of <see cref="DynamicMetaObject"/> instances - arguments to the invoke member operation.</param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryBindGetIndex(GetIndexBinder binder, DynamicMetaObject instance, DynamicMetaObject[] args, out DynamicMetaObject result)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
Requires.NotNull(args);
if (TryGetMetaObjectInvoke(ref instance))
{
result = instance.BindGetIndex(binder, args);
return true;
}
result = null;
return false;
}
/// <summary>
/// Tries to perform binding of the dynamic set index operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="SetIndexBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="instance">The target of the dynamic operation. </param>
/// <param name="args">An array of <see cref="DynamicMetaObject"/> instances - arguments to the invoke member operation.</param>
/// <param name="value">The <see cref="DynamicMetaObject"/> representing the value for the set index operation.</param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryBindSetIndex(SetIndexBinder binder, DynamicMetaObject instance, DynamicMetaObject[] args, DynamicMetaObject value, out DynamicMetaObject result)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
Requires.NotNull(args);
Requires.NotNull(value);
if (TryGetMetaObjectInvoke(ref instance))
{
result = instance.BindSetIndex(binder, args, value);
result = new DynamicMetaObject(result.Expression, result.Restrictions.Merge(value.PSGetMethodArgumentRestriction()));
return true;
}
result = null;
return false;
}
/// <summary>
/// Tries to perform binding of the dynamic Convert operation.
/// </summary>
/// <param name="binder">An instance of the <see cref="ConvertBinder"/> that represents the details of the dynamic operation.</param>
/// <param name="instance">The target of the dynamic operation.</param>
/// <param name="result">The new <see cref="DynamicMetaObject"/> representing the result of the binding.</param>
/// <returns>True if operation was bound successfully; otherwise, false.</returns>
public static bool TryConvert(ConvertBinder binder, DynamicMetaObject instance, out DynamicMetaObject result)
{
Requires.NotNull(binder);
Requires.NotNull(instance);
if (IsComObject(instance.Value))
{
// Converting a COM object to any interface is always considered possible - it will result in
// a QueryInterface at runtime
if (binder.Type.IsInterface)
{
result = new DynamicMetaObject(
Expression.Convert(
instance.Expression,
binder.Type
),
BindingRestrictions.GetExpressionRestriction(
Expression.Call(
typeof(ComBinder).GetMethod(nameof(ComBinder.IsComObject), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public),
Helpers.Convert(instance.Expression, typeof(object))
)
)
);
return true;
}
}
result = null;
return false;
}
/// <summary>
/// Gets the member names of the data-like members associated with the object.
/// This function can operate only with objects for which <see cref="IsComObject"/> returns true.
/// </summary>
/// <param name="value">The object for which member names are requested.</param>
/// <returns>The collection of member names.</returns>
internal static IList<string> GetDynamicDataMemberNames(object value)
{
Requires.NotNull(value);
Requires.Condition(IsComObject(value), nameof(value));
return ComObject.ObjectToComObject(value).GetMemberNames(true);
}
/// <summary>
/// Gets the data-like members and associated data for an object.
/// This function can operate only with objects for which <see cref="IsComObject"/> returns true.
/// </summary>
/// <param name="value">The object for which data members are requested.</param>
/// <param name="names">The enumeration of names of data members for which to retrieve values.</param>
/// <returns>The collection of pairs that represent data member's names and their data.</returns>
internal static IList<KeyValuePair<string, object>> GetDynamicDataMembers(object value, IEnumerable<string> names)
{
Requires.NotNull(value);
Requires.Condition(IsComObject(value), nameof(value));
return ComObject.ObjectToComObject(value).GetMembers(names);
}
private static bool TryGetMetaObject(ref DynamicMetaObject instance)
{
// If we're already a COM MO don't make a new one
// (we do this to prevent recursion if we call Fallback from COM)
if (instance is ComUnwrappedMetaObject)
{
return false;
}
if (IsComObject(instance.Value))
{
instance = new ComMetaObject(instance.Expression, instance.Restrictions, instance.Value);
return true;
}
return false;
}
private static bool TryGetMetaObjectInvoke(ref DynamicMetaObject instance)
{
// If we're already a COM MO don't make a new one
// (we do this to prevent recursion if we call Fallback from COM)
if (TryGetMetaObject(ref instance))
{
return true;
}
if (instance.Value is IPseudoComObject o)
{
instance = o.GetMetaObject(instance.Expression);
return true;
}
return false;
}
/// <summary>
/// Special binder that indicates special semantics for COM GetMember operation.
/// </summary>
internal class ComGetMemberBinder : GetMemberBinder
{
private readonly GetMemberBinder _originalBinder;
internal bool _canReturnCallables;
internal ComGetMemberBinder(GetMemberBinder originalBinder, bool canReturnCallables)
: base(originalBinder.Name, originalBinder.IgnoreCase)
{
_originalBinder = originalBinder;
_canReturnCallables = canReturnCallables;
}
public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
{
return _originalBinder.FallbackGetMember(target, errorSuggestion);
}
public override int GetHashCode()
{
return _originalBinder.GetHashCode() ^ (_canReturnCallables ? 1 : 0);
}
public override bool Equals(object obj)
{
return obj is ComGetMemberBinder other
&& _canReturnCallables == other._canReturnCallables
&& _originalBinder.Equals(other._originalBinder);
}
}
/// <summary>
/// Special binder that indicates special semantics for COM InvokeMember operation.
/// </summary>
internal class ComInvokeMemberBinder : InvokeMemberBinder
{
private readonly InvokeMemberBinder _originalBinder;
internal bool IsPropertySet;
internal ComInvokeMemberBinder(InvokeMemberBinder originalBinder, bool isPropertySet)
: base(originalBinder.Name, originalBinder.IgnoreCase, originalBinder.CallInfo)
{
_originalBinder = originalBinder;
this.IsPropertySet = isPropertySet;
}
public override DynamicMetaObject FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
{
return _originalBinder.FallbackInvoke(target, args, errorSuggestion);
}
public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
{
return _originalBinder.FallbackInvokeMember(target, args, errorSuggestion);
}
public override int GetHashCode()
{
return _originalBinder.GetHashCode() ^ (IsPropertySet ? 1 : 0);
}
public override bool Equals(object obj)
{
ComInvokeMemberBinder other = obj as ComInvokeMemberBinder;
return other != null &&
IsPropertySet == other.IsPropertySet &&
_originalBinder.Equals(other._originalBinder);
}
}
}
}
|