File size: 13,515 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 379 380 381 382 383 384 385 386 387 388 389 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.ObjectModel;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using COM = System.Runtime.InteropServices.ComTypes;
namespace System.Management.Automation
{
/// <summary>
/// Defines a property in the COM object.
/// </summary>
internal class ComProperty
{
private bool _hasSetter = false;
private bool _hasSetterByRef = false;
private int _dispId;
private int _setterIndex;
private int _setterByRefIndex;
private int _getterIndex;
private readonly COM.ITypeInfo _typeInfo;
/// <summary>
/// Initializes a new instance of ComProperty.
/// </summary>
/// <param name="typeinfo">Reference to the ITypeInfo of the COM object.</param>
/// <param name="name">Name of the property being created.</param>
internal ComProperty(COM.ITypeInfo typeinfo, string name)
{
_typeInfo = typeinfo;
Name = name;
}
/// <summary>
/// Defines the name of the property.
/// </summary>
internal string Name { get; }
private Type _cachedType;
/// <summary>
/// Defines the type of the property.
/// </summary>
internal Type Type
{
get
{
_cachedType = null;
if (_cachedType == null)
{
IntPtr pFuncDesc = IntPtr.Zero;
try
{
_typeInfo.GetFuncDesc(GetFuncDescIndex(), out pFuncDesc);
COM.FUNCDESC funcdesc = Marshal.PtrToStructure<COM.FUNCDESC>(pFuncDesc);
if (IsGettable)
{
// use the return type of the getter
_cachedType = ComUtil.GetTypeFromTypeDesc(funcdesc.elemdescFunc.tdesc);
}
else
{
// use the type of the first argument to the setter
ParameterInformation[] parameterInformation = ComUtil.GetParameterInformation(funcdesc, false);
Diagnostics.Assert(parameterInformation.Length == 1, "Invalid number of parameters in a property setter");
_cachedType = parameterInformation[0].parameterType;
}
}
finally
{
if (pFuncDesc != IntPtr.Zero)
{
_typeInfo.ReleaseFuncDesc(pFuncDesc);
}
}
}
return _cachedType;
}
}
/// <summary>
/// Retrieves the index of the FUNCDESC for the current property.
/// </summary>
private int GetFuncDescIndex()
{
if (IsGettable)
{
return _getterIndex;
}
else if (_hasSetter)
{
return _setterIndex;
}
else
{
Diagnostics.Assert(_hasSetterByRef, "Invalid property setter type");
return _setterByRefIndex;
}
}
/// <summary>
/// Defines whether the property has parameters or not.
/// </summary>
internal bool IsParameterized { get; private set; } = false;
/// <summary>
/// Returns the number of parameters in this property.
/// This is applicable only for parameterized properties.
/// </summary>
internal int ParamCount
{
get
{
return 0;
}
}
/// <summary>
/// Defines whether this property is settable.
/// </summary>
internal bool IsSettable
{
get
{
return _hasSetter || _hasSetterByRef;
}
}
/// <summary>
/// Defines whether this property is gettable.
/// </summary>
internal bool IsGettable { get; private set; } = false;
/// <summary>
/// Get value of this property.
/// </summary>
/// <param name="target">Instance of the object from which to get the property value.</param>
/// <returns>Value of the property.</returns>
internal object GetValue(object target)
{
try
{
return ComInvoker.Invoke(target as IDispatch, _dispId, null, null, COM.INVOKEKIND.INVOKE_PROPERTYGET);
}
catch (TargetInvocationException te)
{
var innerCom = te.InnerException as COMException;
if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
{
throw;
}
}
catch (COMException ce)
{
if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
{
throw;
}
}
return null;
}
/// <summary>
/// Get value of this property.
/// </summary>
/// <param name="target">Instance of the object from which to get the property value.</param>
/// <param name="arguments">Parameters to get the property value.</param>
/// <returns>Value of the property</returns>
internal object GetValue(object target, object[] arguments)
{
try
{
object[] newarguments;
var getterCollection = new Collection<int> { _getterIndex };
var methods = ComUtil.GetMethodInformationArray(_typeInfo, getterCollection, false);
var bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);
object returnValue = ComInvoker.Invoke(target as IDispatch,
bestMethod.DispId,
newarguments,
ComInvoker.GetByRefArray(bestMethod.parameters,
newarguments.Length,
isPropertySet: false),
bestMethod.InvokeKind);
Adapter.SetReferences(newarguments, bestMethod, arguments);
return returnValue;
}
catch (TargetInvocationException te)
{
var innerCom = te.InnerException as COMException;
if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
{
throw;
}
}
catch (COMException ce)
{
if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
{
throw;
}
}
return null;
}
/// <summary>
/// Sets value of this property.
/// </summary>
/// <param name="target">Instance of the object to which to set the property value.</param>
/// <param name="setValue">Value to set this property.</param>
internal void SetValue(object target, object setValue)
{
object[] propValue = new object[1];
setValue = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, this.Type, CultureInfo.InvariantCulture);
propValue[0] = setValue;
try
{
ComInvoker.Invoke(target as IDispatch, _dispId, propValue, null, COM.INVOKEKIND.INVOKE_PROPERTYPUT);
}
catch (TargetInvocationException te)
{
var innerCom = te.InnerException as COMException;
if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
{
throw;
}
}
catch (COMException ce)
{
if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
{
throw;
}
}
}
/// <summary>
/// Sets the value of the property.
/// </summary>
/// <param name="target">Instance of the object to which to set the property value.</param>
/// <param name="setValue">Value to set this property.</param>
/// <param name="arguments">Parameters to set this property.</param>
internal void SetValue(object target, object setValue, object[] arguments)
{
object[] newarguments;
var setterCollection = new Collection<int> { _hasSetterByRef ? _setterByRefIndex : _setterIndex };
var methods = ComUtil.GetMethodInformationArray(_typeInfo, setterCollection, true);
var bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);
var finalArguments = new object[newarguments.Length + 1];
for (int i = 0; i < newarguments.Length; i++)
{
finalArguments[i] = newarguments[i];
}
finalArguments[newarguments.Length] = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, Type, CultureInfo.InvariantCulture);
try
{
ComInvoker.Invoke(target as IDispatch,
bestMethod.DispId,
finalArguments,
ComInvoker.GetByRefArray(bestMethod.parameters,
finalArguments.Length,
isPropertySet: true),
bestMethod.InvokeKind);
Adapter.SetReferences(finalArguments, bestMethod, arguments);
}
catch (TargetInvocationException te)
{
var innerCom = te.InnerException as COMException;
if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
{
throw;
}
}
catch (COMException ce)
{
if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
{
throw;
}
}
}
/// <summary>
/// Updates the COM property with setter and getter information.
/// </summary>
/// <param name="desc">Functional descriptor for property getter or setter.</param>
/// <param name="index">Index of function descriptor in type information.</param>
internal void UpdateFuncDesc(COM.FUNCDESC desc, int index)
{
_dispId = desc.memid;
switch (desc.invkind)
{
case COM.INVOKEKIND.INVOKE_PROPERTYGET:
IsGettable = true;
_getterIndex = index;
if (desc.cParams > 0)
{
IsParameterized = true;
}
break;
case COM.INVOKEKIND.INVOKE_PROPERTYPUT:
_hasSetter = true;
_setterIndex = index;
if (desc.cParams > 1)
{
IsParameterized = true;
}
break;
case COM.INVOKEKIND.INVOKE_PROPERTYPUTREF:
_setterByRefIndex = index;
_hasSetterByRef = true;
if (desc.cParams > 1)
{
IsParameterized = true;
}
break;
}
}
internal string GetDefinition()
{
IntPtr pFuncDesc = IntPtr.Zero;
try
{
_typeInfo.GetFuncDesc(GetFuncDescIndex(), out pFuncDesc);
COM.FUNCDESC funcdesc = Marshal.PtrToStructure<COM.FUNCDESC>(pFuncDesc);
return ComUtil.GetMethodSignatureFromFuncDesc(_typeInfo, funcdesc, !IsGettable);
}
finally
{
if (pFuncDesc != IntPtr.Zero)
{
_typeInfo.ReleaseFuncDesc(pFuncDesc);
}
}
}
/// <summary>
/// Returns the property signature string.
/// </summary>
/// <returns>Property signature.</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append(this.GetDefinition());
builder.Append(' ');
if (IsGettable)
{
builder.Append("{get} ");
}
if (_hasSetter)
{
builder.Append("{set} ");
}
if (_hasSetterByRef)
{
builder.Append("{set by ref}");
}
return builder.ToString();
}
}
}
|