我正在编写Outlook外接程序,其中我希望将数据从一个outlook外接程序项目发送到另一个outlook项目。但是,当我尝试用参数中的对象类型数据调用另一个项目的函数时,它将抛出“无法将'System.Runtime.Remoting.Proxies.__TransparentProxy‘类型的对象强制转换为在System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo中键入的对象”错误。。
这是供您参考的代码。
public IProfileAttribute[] profileAttributes= null;
Outlook.Application outlookApp = new Outlook.Application();
this.profileAttributes = new FilingNotifiableIpmlementation().FilingNotification(); // to fill the object
object destAddinName = "Tikit.CarpeDiem.AddIn.Outlook";
Office.COMAddIn destAddIn = outlookApp.COMAddIns.Item(ref destAddinName)
destAddIn.Object.FilingNotification(this.profileAttributes);FilingNotification()是我们想要调用的Tikit.CarpeDiem.AddIn.Outlook方法,这个项目和this.profileAttributes是对象数组。
如果参数类型为string或int,则流非常适合Outlook项目,但如果参数是对象类型,则会引发错误。
FilingNotification()方法在Tikit.CarpeDiem.AddIn.Outlook项目中的实现。
public void FilingNotification(IProfileAttribute[] profileAttributesList)
{
if (profileAttributesList != null)
{
var x = profileAttributesList;
}
else
{
string y = "Try again";
}
}有人能帮我这个忙吗。我被困在这里面两天了。这会很有帮助的。提前谢谢。
发布于 2022-07-19 15:36:08
与其传递.Net对象,不如让它实现COM接口并将其作为接口传递:
var obj = new MyBlah();
destAddIn.Object.FilingNotification(obj as IBlah);
...
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IBlah
{
void DoBlah();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class MyBlah: StandardOleMarshalObject, IBlah
{
public void DoBlah()
{
//todo
};
}发布于 2022-07-19 15:36:18
您可以按照这种方式传递标量数据类型。如果要在两个实体之间传递对象,则需要实现双方都知道的接口。在演练:从VBA调用VSTO外接程序中的代码。文章中阅读更多有关这方面的内容。例如:
[ComVisible(true)]
public interface IAddInUtilities
{
void ImportData();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{
// This method tries to write a string to cell A1 in the active worksheet.
public void ImportData()
{
// implementation
}
}此外,您还可以考虑使用任何可用于基于.net的应用程序(如远程处理或WCF )的标准机制,有关更多信息,请参见基本WCF编程。
https://stackoverflow.com/questions/73038057
复制相似问题