简体   繁体   English

在另一个项目中引用枚举

[英]Reference Enum in another project

I have the following code in project1 that is calling a class from project2. 我在project1中有以下代码,该代码正在从project2中调用类。

Project1 is calling: ConvertDocument.Convert(CommandLineFile, "d:\\testing\\test.pdf", WdSaveFormat.wdFormatPDF ); Project1正在调用:ConvertDocument.Convert(CommandLineFile,“ d:\\ testing \\ test.pdf”, WdSaveFormat.wdFormatPDF );

Project 2 contains ConvertDocument and WdSaveFormat is from Microsoft.Office.Interop.Word that is referenced in project2. 项目2包含ConvertDocument,并且WdSaveFormat来自project2中引用的Microsoft.Office.Interop.Word。

When I try to pass the enum value to project2 with the above code, it gives me: 当我尝试使用上述代码将枚举值传递给project2时,它给了我:

Cannot resolve symbol "WdSaveFormat". 无法解析符号“ WdSaveFormat”。

My question is, can I reference that enum in project2 that is referencing Microsoft.Office.Interop.Word from Project1 without having to add the reference? 我的问题是,我是否可以引用project1中引用Microsoft.Office.Interop.Word的project2中的枚举,而不必添加引用?

I hope that makes sense... 我希望这是有道理的...

Thanks again! 再次感谢!

No, Project 1 will need a reference to Microsoft.Office.Interop.Word. 不,项目1将需要对Microsoft.Office.Interop.Word的引用。

The only way around this is to have Project 2 accept an object (or in the case, an int would be better), rather than WdSaveFormat. 解决此问题的唯一方法是让Project 2接受一个对象 (或者,最好是int )而不是WdSaveFormat。 (Edit: or as other commenters point out, Project 2 could define its own enum that maps back to WdSaveFormat). (编辑:或正如其他评论者所指出的,Project 2可以定义自己的枚举,该枚举映射回WdSaveFormat)。 Project 2 can then cast the int to the WdSaveFormat enum. 然后,项目2可以将int强制转换为WdSaveFormat枚举。

For example: 例如:

void Convert(string in, string out, int saveFormat)
{
    WdSaveFormat wdSaveFormat = (WdSaveFormat) saveFormat;
}

However, when you try to run this piece of code in Project 1, the runtime will still need to be able to locate a copy of the Microsoft.Office.Interop.Word dll anyway. 但是,当您尝试在Project 1中运行这段代码时,无论如何,运行时仍将需要能够找到Microsoft.Office.Interop.Word dll的副本。

The best solution is to create your own type to marshal parameters between libraries and projects. 最好的解决方案是创建自己的类型,以在库和项目之间封送参数。

Do NOT require communications between projects to know about 3rd party libraries (unless they will always be tightly coupled in this manner). 不需要项目之间的通信来了解第三方库(除非它们始终以这种方式紧密耦合)。 It is much better to create your own types to marshal between project libraries. 最好创建自己的类型以在项目库之间编组。 Then the receiving project (callee) will translate from your custom type to the 3rd party library type. 然后,接收项目(被叫者)将从您的自定义类型转换为第三方库类型。

In this scenario, Project 1 will use a custom enum known by both project 1 and 2. Project 1 will use this enum to talk with project 2 who will translate it into the correct 3rd party type. 在这种情况下,项目1将使用项目1和2都知道的自定义枚举。项目1将使用此枚举与项目2进行对话,后者将其转换为正确的第三方类型。

For instance (this is rough pseudocode): 例如(这是粗糙的伪代码):

public enum SaveFormat
{
    SaveFormat1 = 0,
    SaveFormat2 = 1
};

namespace MyProject1
...
    public void Save()
    {
        MyProject2.SaveDocument( SaveFormat.SaveFormat1 );        
    }

namespace MyProject2
...
    ...
    public void SaveDocument( SaveFormat format )
    {
        WdFormat localFormat = this.Translate( format );        
        ...
    }

    private WdFormat Translate( SaveFormat format )
    {
        switch( format )
        {
            case SaveFormat1:
                return WDFormat1;
            case SaveFormat2:
                return WDFormat2;
            default:
                return WDFormat3;
         }
    }

No, it's not possible. 不,不可能。 WsSaveFormat is defined in the Word.Interop so, without adding the reference you can't use in project1. WsSaveFormat是在Word.Interop中定义的,因此,无需添加引用就不能在project1中使用。 However (and keep it clear that I don't recomend it) you could define in your project2 a similar enum and let project1 use that enum. 但是(您应该清楚地说明,我不建议这样做),您可以在project2中定义一个类似的枚举,并让project1使用该枚举。 Of course you need a mapping from your enum to Word.Interop enum. 当然,您需要从枚举到Word.Interop枚举的映射。

The best rule of thumb I have found when dealing with office interop stuff is: don't export anything that exposes an interop type. 在处理办公室互操作性时,我发现的最佳经验法则是:不要导出任何暴露互操作类型的东西。 The only exposed types should be your types which are wrapping the interop behaviour. 唯一公开的类型应该是包装互操作行为的类型。 As far as I know, this is a good rule of thumb for all third-party code. 据我所知,这是所有第三方代码的良好经验。

No, you have to add reference to the Microsoft.Office.Interop.Word in Project1 one to use an enum defined in the assembly. 不,您必须在Project1中添加对Microsoft.Office.Interop.Word的引用,才能使用程序集中定义的枚举。 It wouldn't cost you anything in terms of memory because your Project2 is using the reference anyways (and you are using Project2 in Project1) 就内存而言,这不会花费您任何费用,因为您的Project2无论如何都使用引用(并且您在Project1中使用Project2)

No, you're using the enum WdSaveFormat in project 1 so you need a reference to Microsoft.Office.Interop.Word. 不,您在项目1中使用枚举WdSaveFormat,因此您需要对Microsoft.Office.Interop.Word的引用。

If you were consuming a function from project 2 that consumed another dll (eg foo.dll) but didn't explicitly use anything from foo.dll in project 1 then you wouldn't need to have a reference to foo.dll within project 1. 如果您正在使用项目2中的一个函数,该函数使用了另一个dll(例如foo.dll),但未在项目1中显式使用foo.dll中的任何内容,那么您就不需要在项目1中引用foo.dll。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM