在C#中跨项目使用枚举

Swi*_*Run 2 c# enums

我有一个硬件数据位时钟我想在C#中设置不同的模式,但我想知道实现它的最佳方法.这是我使用字符串的旧实现,它工作得很好,因为两个项目都知道字符串是什么,但使用字符串和字符串比较似乎容易出错,所以我切换到枚举.

int SetDAI_BCLK(int32 Hz, string mode)
{
     // int32 Hz Specifies the BCLK rate in Hz
     // string mode: "master" => master mode, "slave" => slave mode, "off" => clock off, "poll1" => special polling mode

     // Implementation here, use str compares to set the clock mode

     // return 1 if success, -1 if comm error, -2 if invalid clock settings, -3 if invalid string master
}
Run Code Online (Sandbox Code Playgroud)

如果枚举全部在同一个项目中,那么枚举效果很好,但不会跨项目工作,而字符串则会有效.如果不对两个项目中的枚举进行硬编码,我无法弄清楚如何做到这一点.

项目A包含枚举定义和引用项目B,其中包含设备功能.项目B不知道这些枚举,当我尝试引用项目A时,我得到一个循环引用错误,不允许引用它.反正只是引用一个文件?在这两个项目中使用相同枚举的最佳方法是什么.使用VS 2013 Express.

项目B.

namespace EquipControl
{
    public interface IMotherboard
    {
        int connectOutput_to_DUT(EAPOutput APOutput);
    }

    public class HWMotherBoard : IMotherboard
    {
        public int connectOutput_to_DUT(EAPOutput APOutput)
        {
            if (APOutput == EAPOutput.AnalogStereo)
            {
                //set stereo
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

项目A.

using EquipControl;

namespace TestSuite
{
    public partial class test1
    {
        public enum EAPOutput
        {
            AnalogMonoL,
            AnalogMonoR,
            AnalogStereo
        }

        public EAPOutput APOutput = EAPOutput.AnalogStereo;

        public void main()
        {
            HWMotherBoard HWMB = new HWMotherBoard();
            HWMB.connectOutput_to_DUT(APOutput);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

adv*_*v12 6

将枚举放在最底层的项目(由另一个项目引用的项目)中,它们将可供两个项目使用.此外,将枚举公开,您可能希望在类定义之外定义它们.