Type.InvokeMember在扩展方法内抛出“ COMException:类型不匹配”

时间:2019-04-04 07:11:10

标签: c# com extension-methods acrobat-sdk

我正在使用Adobe Acrobat SDK,并且尝试通过将InvokeMember调用包装在Extension方法中以使代码更易读来清理COM调用。

我要编写的扩展方法如下:

using System;
using System.Reflection;

public static class Invoke_Extension
{
    public static object CallJSfunction(this Object adobeComObject, string sJSfunction, params object[] oPlist)
    {

        Type T = adobeComObject.GetType();

        return T.InvokeMember(
                        sJSfunction,
                        BindingFlags.GetProperty | //fixed per Simon's comment
                        BindingFlags.Public |
                        BindingFlags.Instance,
                        null, adobeComObject, oPlist);
    }
}

它将替换Adobe示例代码中的COM调用,如下所示:

    // Example from the Adobe SDK sample code
using System;
using System.Reflection;
using Acrobat;
//---------------- Create necessary objects ----------------------
    AcroAVDoc g_AVDoc = new AcroAVDoc();
    g_AVDoc.Open(filename, "");
    CAcroPDDoc pdDoc = (CAcroPDDoc)g_AVDoc.GetPDDoc();
    //Acquire the Acrobat JavaScript Object interface from the PDDoc object
    Object jsObj = pdDoc.GetJSObject();

//------- This is the part I am trying to make more readable --------
    Type T = jsObj.GetType();

    // total number of pages
    double nPages = (double)T.InvokeMember(
                "numPages",
                BindingFlags.GetProperty |
                BindingFlags.Public |
                BindingFlags.Instance,
                null, jsObj, null);

//-------------------------------------------------------------------------
//-- using the extension method, the two calls above are replaced with: ---

    double nPages2 = (double)jsObj.CallJSfunction("numPages", null);
/--------------------------------------------------------------------------
// ...but the InvokeMember call within the extension method is throwing an error.




//BTW, the optional parameters in the Extension method is because some of the functions have more than one parameter eg.
    object[] addTextWatermarkParam = { currentTime.ToShortTimeString(), 1, "Helvetica", 100, blueColorObj, 0, 0, true, true, true, 0, 3, 20, -45, false, 1.0, false, 0, 0.7 };
    T.InvokeMember(
            "addWatermarkFromText",
            BindingFlags.InvokeMethod |
            BindingFlags.Public |
            BindingFlags.Instance,
            null, jsObj, addTextWatermarkParam);
// which would be replaced with
    jsObj.CallJSfunction("addWatermarkFromText",currentTime.ToShortTimeString(), 1, "Helvetica", 100, blueColorObj, 0, 0, true, true, true, 0, 3, 20, -45, false, 1.0, false, 0, 0.7 );

完整的错误为:

System.Reflection.TargetInvocationException   HResult = 0x80131604   调用目标已抛出Message = Exception。   来源= mscorlib   堆栈跟踪:    在System.RuntimeType.InvokeDispMethod上(字符串名称,BindingFlags invokeAttr,对象目标,Object [] args,Boolean [] byrefModifiers,Int32文化,String [] namedParameters)    在System.RuntimeType.InvokeMember处(字符串名称,BindingFlags bindingFlags,活页夹绑定程序,对象目标,Object []提供的Args,ParameterModifier []修饰符,CultureInfo文化,String [] namedParams)    在System.Type.InvokeMember处(字符串名称,BindingFlags invokeAttr,活页夹装订器,对象目标,Object [] args)    在Z :: mike \ Downloads \ Adob​​e \ Acrobat DC SDK \ Version 1 \ InterAppCommunicationSupport \ C#Samples \ JSObjectControlCS \ JSObjectControlCS \ Invoke_Extension.cs:第11行中的Invoke_Extension.CallJSfunction(Object adobeComObject,String sJSfunction,Object [] oPlist)中    在Z:\ mike \ Downloads \ Adob​​e \ Acrobat DC SDK \ Version 1 \ InterAppCommunicationSupport \ C#Samples \ JSObjectControlCS \ JSObjectControlCS \ Form1.cs:line 239中的JSObjectControlCS.JSObjectControlForm.searchButton_Click(Object sender,EventArgs e)    在System.Windows.Forms.Control.OnClick(EventArgs e)    在System.Windows.Forms.Button.OnClick(EventArgs e)    在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    在System.Windows.Forms.Control.WmMouseUp上(消息和m,MouseButtons按钮,Int32单击)    在System.Windows.Forms.Control.WndProc(Message&m)    在System.Windows.Forms.ButtonBase.WndProc(Message&m)    在System.Windows.Forms.Button.WndProc(Message&m)    在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)    在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)    在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,Int32 pvLoopData)    在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)    在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)    在Z:\ mike \ Downloads \ Adob​​e \ Acrobat DC SDK \ Version 1 \ InterAppCommunicationSupport \ C#Samples \ JSObjectControlCS \ JSObjectControlCS \ Program.cs:第17行的JSObjectControlCS.Program.Main()中

内部异常1: COMException:类型不匹配。 (来自HRESULT的异常:0x80020005(DISP_E_TYPEMISMATCH))

0 个答案:

没有答案