如何从其他C#项目中调用VSTO类 [英] How to call VSTO class from other c# project

查看:136
本文介绍了如何从其他C#项目中调用VSTO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案中,我有2个项目.

In my solution I have 2 projects.

一个是控制器,在最终产品中,该控制器用于检查是否从控制台/非用户输入发出执行,因此将基于xml文件的输入在后台执行想要的更改,或者执行是由用户输入发出的,它将打开一个界面.

One is the controller, which in the final product is used to check if a execution is issued from console/non user input and therefore will execute the wanted changes in background based on imput from a xml-file, or if the execution was issued by user input, which will open an interface.

非用户输入可能是计划中的任务或类似的任务,但是那是另一回事了,我只是为某些情况而写.

A non user input would maybe a planed task or something like that, but thats is for an other time now and I just write that for some context.

在这两种情况下,迟早都需要访问Word文档以及读取,写入和更改文档属性.

In both cases, sooner or later there is the need to access word-documents and read, write and change document-properties.

为此,我创建了一个具有所需功能的VSTO-Word-Addin,到目前为止,我对路径进行了硬编码,并且除其他文档外没有返回结果.

To do that I created a VSTO-Word-Addin with the needed functions and up to this point I hardcoded the paths and didn't return the results anywhere else than an other document.

由于我确信我在VSTO中的代码可以正常工作,因此我想将原型扩展到下一个级别,并尝试在控制台和VSTO之间添加连接.

Since I am sure my code in VSTO itself works, I wanted to extend the prototype to the next level and tried adding the connections between console and VSTO.

为了进行测试,我稍微简化了过程,只是尝试在没有任何用户输入的情况下在控制台和VSTO之间建立连接,并尝试执行一些方法来测试我的VSTO的功能.

For testing I am simplifying the process a bit and just try to establish the connection between console and VSTO without any userinput and trying to execute some methods to test functionality of my VSTO.

我的方法是打开控制台,然后打开Word/插件,打开隐藏的文件并进行魔术操作.

My approach was to open the console, which then opens Word/the addin, open the file hidden and do the magic.

首先要做的是设置要打开的文档的路径,然后用返回的值调用多个方法.

First thing to do is to set a path for the document to be opened and then call multiple methods with returned values.

在这种情况下,我的VSTO返回true

In this case my VSTO returns a true for

SetCustomProperty 

和一个新的元组列表

GetCustomProperties

这些是占位符,将在开发中被替换.

Those are placeholders and will be replaced in developement.

我已经尝试了一些可能的解决方案,但是大多数方法都是从VSTO中启动WinForms/WPF/Console,或者尝试从其AddIn调用另一个AddIn.

I already tried some possible solutions, but most of them go the other way around to start a WinForms/WPF/Console out of VSTO or try to call an other AddIn from their AddIn.

我最成功的方法是:

MSDN 通过其他Office解决方案在VSTO加载项中调用代码

MSDN Calling Code in VSTO Add-ins from Other Office Solutions

但是这当然是办公室用的,所以我遇到了无法使用的问题

But of course this is for office so I hit the problem of not being able to use

Globals

有关Globals的更多信息,请参见在MSDN中

More info about Globals can be found here in MSDN

所以也许我错过了要点,只是盲目,但是如何从控制台调用VSTO项目中的类?

So maybe I am missing the point and am just blind, but how can I call a class in a VSTO-project from console?

以下是我当前失败的一些代码示例:

Here are some codesamples of my current failure:

具有我要访问的使用的接口的类:

The class with the used interface I want to access:

[ComVisible(true)]
public interface IPropertyReadWriter
{
  bool Open(string Path);

  bool SetCustomProperty(String Name, PropertyTypes Type, object Value);

  List<Tuple<String, PropertyTypes, object>> GetCustomProperties();
}

[ComVisible(true)]
public class PropertyReaderWriter : IPropertyReadWriter
{
  public List<Tuple<string, PropertyTypes, object>> GetCustomProperties()
  {
    return new List<Tuple<string, PropertyTypes, object>>();
  }

  public bool Open(string Path)
  {
    return false;
  }

  public bool SetCustomProperty(string Name, PropertyTypes Type, object Value)
  {
    return false;
  }
}

MSDN文章中有关从其他Office项目进行调用的代码:

The code used in the MSDN article about calling from other office-project:

object addInName = "ExcelImportData";
Office.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName);
ExcelImportData.IAddInUtilities utilities = (ExcelImportData.IAddInUtilities)addIn.Object;
utilities.ImportData();

我不知道如何利用它,因为我无法访问VSTO的Globals异地?

I dont know how to make use of this, because I don't have access to Globals outsite of VSTO?

类似的问题,由于缺少上下文或示例,我无法使用,但无法回答:

Somewhat similar question on so with no answer I could use, because lack of context or example:

我不知道DanByström的答案是什么意思,也不是Mike Regan的答案导致先前提到的MSDN.

I don't know what Dan Byström meant with his answer, also Mike Regan's answer lead to prior stated MSDN.

如何调用一个单独的C#项目中的VSTO AddIn方法?

推荐答案

首先,在您要调用的Addin中添加一个接口:

First, to your Addin that you want to call into add an Interface:

[ComVisible(true)]
public interface IExcelUtilities
{
    bool DoSomething();
}

接下来,添加一个实现该接口的类:

Next, add a class that implements the interface:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : 
    StandardOleMarshalObject,
    IExcelUtilities
{
    public bool DoSomething()
    {
        return true;
    }
}

然后在ThisAddin.cs中覆盖对象RequestComAddInAutomationService:

Then override object RequestComAddInAutomationService in ThisAddin.cs:

private AddInUtilities utilities;

protected override object RequestComAddInAutomationService()
{
   try
   {
       if (utilities == null)
       {
           utilities = new AddInUtilities();
       }

       return utilities;
    }
    catch (System.Exception ex)
    {
         // Catch your ex here
    }
}

现在,您应该能够像这样从外部应用程序调用公开的方法:

Now you should be able to call the exposed method from your external application like this:

foreach (COMAddIn comaddin in addins)
{
     if (comaddin.ProgId.Equals("YourAddinNameHere", StringComparison.InvariantCultureIgnoreCase) == true)
     {
              bool returnvalue = comaddin.Object.DoSomething();
              break;
     }
}

有关此主题的更多详细信息,请阅读: http://blogs.msdn.com/b/andreww/archive/2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx

for some more deep info on this subject, also read: http://blogs.msdn.com/b/andreww/archive/2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx

希望它会有所帮助:-)

Hope it helps :-)

这篇关于如何从其他C#项目中调用VSTO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆