コード例 #1
0
        public object GetProperty(string name, out bool isCacheable, params object[] args)
        {
            isCacheable = false;

            DispatchMember member = null;

            if (args.Length < 1)
            {
                // some objects crash on attempt to retrieve a method as a property

                member = GetMembers().FirstOrDefault(testMember => testMember.Name == name);
                if ((member != null) && member.DispatchFlags == DispatchFlags.Method)
                {
                    return(new HostMethod(hostItem, name));
                }
            }

            try
            {
                var result = dispatch.GetProperty(name, args);
                return(result);
            }
            catch (Exception)
            {
                if (args.Length < 1)
                {
                    if (member == null)
                    {
                        member = GetMembers().FirstOrDefault(testMember => testMember.Name == name);
                    }

                    if ((member != null) && !member.DispatchFlags.HasFlag(DispatchFlags.Method))
                    {
                        return(new HostIndexedProperty(hostItem, name));
                    }

                    return(new HostMethod(hostItem, name));
                }

                throw;
            }
        }
コード例 #2
0
        public static object InvokeMethod(this IDispatch dispatch, string name, params object[] args)
        {
            var dispid = dispatch.GetDispIDForName(name);

            if (dispid == SpecialDispIDs.GetEnumerator)
            {
                return(dispatch.GetProperty(SpecialMemberNames.NewEnum, args));
            }

            using (var argVariantArrayBlock = new CoTaskMemVariantArgsByRefBlock(args))
            {
                using (var resultVariantBlock = new CoTaskMemVariantBlock())
                {
                    var dispArgs = new DISPPARAMS {
                        cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero
                    };
                    HResult.Check(dispatch.Invoke(dispid, iid, 0, DispatchFlags.Method, ref dispArgs, resultVariantBlock.Addr, out _, out _));
                    return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr));
                }
            }
        }