c# - 如何使用 Marshal.CreateWrapperOfType(非过时版本)从 Com 对象转换为 Outlook 类型
问题描述
我有一段代码很适合我使用过时的函数。我将精简到最小的部分:
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........
}
这是VB代码:
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
请注意,没有对 Outlook dll 的引用,也没有对互操作的引用。
所以我想知道是否有人可以帮助将过时的方法调用转换为新方法。
解决方案
推荐阅读
- sql - SQLite3 数据库在 Azure 中被锁定
- c++ - 正则表达式替换字符串中是否有与 Perl 的 \L 等效的 C++?
- kotlin - 在 kotlin 中隐藏警报对话框
- javascript - 与 .js 文件建立链接
- java - 如何通过wifi配置连接到wifi
- javascript - Vue 在开发模式下编译这个 HTML 模板非常慢
- c# - 将数据返回到具有引用其他模型的模型的视图
- python-3.x - python3 需要对字典和循环有所了解
- javascript - 到达顶部后 div 的宽度发生变化
- react-native - 是否可以在本机反应中使用反应挂钩?