C# 如何使用Marshal.CreateWrapperOfType(非过时版本)将Com对象强制转换为Outlook类型

C# 如何使用Marshal.CreateWrapperOfType(非过时版本)将Com对象强制转换为Outlook类型,c#,.net,vb.net,com,C#,.net,Vb.net,Com,我有一段代码,使用一个过时的函数对我来说很好。我将精简到最少的部分: private dynamic pSentFolderItems; //will reference the Items of an Outlook Folder public void WatchEmail2(object app, string emailID) { pApp = app; //this is actually an Outlook Application instance //belo

我有一段代码,使用一个过时的函数对我来说很好。我将精简到最少的部分:

private dynamic pSentFolderItems; //will reference the Items of an Outlook Folder 

public void WatchEmail2(object app, string emailID)
{
    pApp = app; //this is actually an Outlook Application instance
    //below reference to the Items object comes back as ComObject
    pSentFolderItems = pApp.Session.GetDefaultFolder(5).Items;

    //get the type through Reflection on the instance
    Type olItemsType = pApp.GetType().Assembly.GetType("Microsoft.Office.Interop.Outlook.ItemsClass", false, true);

    //pSentFolderItems = Marshal.CreateWrapperOfType(pSentFolderItems, olItemsType);
    //above works every time but is marked Obsolete!!
    //http://go.microsoft.com/fwlink/?LinkID=296519

    //I'm struggling to implement the generic version of the CreateWrapperOfType method, as errors occur
    //and I cannot input the type parameters without errors
    pSentFolderItems = Marshal.CreateWrapperOfType<ShouldBeComObjectType, ShouldBeOlItemsType>(pSentFolderItems);

    //.....rest of code........
}
请注意,没有对Outlook dll的引用,也没有互操作引用


因此,我想知道是否有人可以帮助将过时的方法调用转换成新的方式。

尽量不要使用不相关的标记;这不是一个
vb.net
问题您是否尝试过
pSentFolderItems=Marshal.CreateWrapperOfType(pSentFolderItems)?这是怎么发生的?这是在C#支持动态关键字之前编写的吗?不需要包装就能让装订工满意。还要注意的是,此代码只能在调用方使用interop库的早期绑定时工作,因为您可以使用它。我听说您@Martin,但VB程序员可能知道答案,而且我确实在VB中有此代码。“C#只是为更多读者翻译的。”马丁,我试过了,但无法推断类型参数
Public Class OVBO
    Private pApp As Object
    Public pMailId As String = ""
    Public itemSent As Boolean = False  'change to a map?
    Public itemWatching As Boolean = False

    Public pSentFolderItems As Object

    Public Sub WatchEmail2(app As Object, emailID As String)
        pMailId = emailID
        pApp = app

        If Not itemWatching Then
            pSentFolderItems = pApp.Session.GetDefaultFolder(5).Items

            Dim olType As Type = pApp.GetType().Assembly.GetType("Microsoft.Office.Interop.Outlook.ItemsClass", False, True)
            Dim myFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic
            Dim evt As EventInfo = olType.GetEvent("ItemAdd", myFlags)
            Dim evtHandlerType As Type = evt.EventHandlerType

            Dim finalD As [Delegate] = [Delegate].CreateDelegate(evtHandlerType, Me, "MailSent2")
            Dim addHandlerArgs() As Object = {finalD}

            'The below works to return an Items Object(from a ComObject) that can be passed to the method handler invocation
            pSentFolderItems = Marshal.CreateWrapperOfType(pSentFolderItems, olType)
            'The above is obsolete, and am struggling to work with the non-obsolete method at [MSDN][1]

            Dim miAddHandler As MethodInfo = evt.GetAddMethod()
            miAddHandler.Invoke(pSentFolderItems, addHandlerArgs)

            itemWatching = True
        End If

        Console.WriteLine("Watching {0}", emailID)
    End Sub

    'rest of class
End Class