0

我正在使用 Adob​​e 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);
    }
}

它将替换 Adob​​e 示例代码中的 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=异常已被调用的目标抛出。Source=mscorlib StackTrace:在 System.RuntimeType.InvokeDispMethod(字符串名称,BindingFlags invokeAttr,对象目标,Object[] args,Boolean[] byrefModifiers,Int32 文化,String[] namedParameters)在 System.RuntimeType.InvokeMember(字符串名称,BindingFlags bindingFlags,Binder binder,Object target,Object[] providedArgs,ParameterModifier[] 修饰符,CultureInfo 文化,String[] namedParams) at System.Type.InvokeMember(String name,BindingFlags invokeAttr,Binder binder,Object target,Object[] args)在 Z 中的 Invoke_Extension.CallJSfunction(Object adobeComObject, String sJSfunction, Object[] oPlist):

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

4

0 回答 0