/// <summary> /// Tries to get the DISPID for the requested member name. /// </summary> /// <param name="dispatch">An object that implements IDispatch.</param> /// <param name="name">The name of a member to lookup.</param> /// <param name="dispId">If the method returns true, this holds the DISPID on output. /// If the method returns false, this value should be ignored.</param> /// <returns>True if the member was found and resolved to a DISPID. False otherwise.</returns> private static bool TryGetDispId(IDispatchInfo dispatch, string name, out int dispId) { RequireReference(dispatch, "dispatch"); RequireReference(name, "name"); bool result = false; // Members names aren't usually culture-aware for IDispatch, so we might as well // pass the default locale instead of looking up the current thread's LCID each time // (via CultureInfo.CurrentCulture.LCID). Guid iidNull = Guid.Empty; int hr = dispatch.GetDispId(ref iidNull, ref name, 1, LOCALE_SYSTEM_DEFAULT, out dispId); const int DISP_E_UNKNOWNNAME = unchecked ((int)0x80020006); //From WinError.h const int DISPID_UNKNOWN = -1; //From OAIdl.idl if (hr == S_OK) { result = true; } else if (hr == DISP_E_UNKNOWNNAME && dispId == DISPID_UNKNOWN) { // This is the only supported "error" case because it means IDispatch // is saying it doesn't know the member we asked about. result = false; } else { // The other documented result codes are all errors. Marshal.ThrowExceptionForHR(hr); } return(result); }
private static bool TryGetDispId(IDispatchInfo dispatch, string name, out int dispId) { RequireReference(dispatch, "dispatch"); RequireReference(name, "name"); bool result = false; Guid iidNull = Guid.Empty; int hr = dispatch.GetDispId(ref iidNull, ref name, 1, LOCALE_SYSTEM_DEFAULT, out dispId); const int DISP_E_UNKNOWNNAME = unchecked ((int)0x80020006); //From WinError.h const int DISPID_UNKNOWN = -1; //From OAIdl.idl if (hr == S_OK) { result = true; } else if (hr == DISP_E_UNKNOWNNAME && dispId == DISPID_UNKNOWN) { result = false; } else { Marshal.ThrowExceptionForHR(hr); } return(result); }
/// <summary> /// Gets a Type that can be used with reflection. /// </summary> /// <param name="dispatch">An object that implements IDispatch.</param> /// <param name="throwIfNotFound">Whether an exception should be thrown if a Type can't be obtained.</param> /// <returns>A .NET Type that can be used with reflection.</returns> static Type GetType(IDispatchInfo dispatch, bool throwIfNotFound) { RequireReference(dispatch, "dispatch"); Type result = null; int hr = dispatch.GetTypeInfoCount(out int typeInfoCount); if (hr == SOk && typeInfoCount > 0) { // Type info isn't usually culture-aware for IDispatch, so we might as well pass // the default locale instead of looking up the current thread's LCID each time // (via CultureInfo.CurrentCulture.LCID). dispatch.GetTypeInfo(0, LocaleSystemDefault, out result); } if (result == null && throwIfNotFound) { // If the GetTypeInfoCount called failed, throw an exception for that. Marshal.ThrowExceptionForHR(hr); // Otherwise, throw the same exception that Type.GetType would throw. throw new TypeLoadException(); } return(result); }
private static Type GetType(IDispatchInfo dispatch, bool throwIfNotFound) { RequireReference(dispatch, "dispatch"); Type result = null; int typeInfoCount; int hr = dispatch.GetTypeInfoCount(out typeInfoCount); if (hr == S_OK && typeInfoCount > 0) { dispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT, out result); } if (result == null && throwIfNotFound) { // If the GetTypeInfoCount called failed, throw an exception for that. Marshal.ThrowExceptionForHR(hr); // Otherwise, throw the same exception that Type.GetType would throw. throw new TypeLoadException(); } return(result); }