简体   繁体   English

C#到C ++ / CLI枚举声明

[英]c# to c++/cli Enum declaration

I have looked several places, but can't seem to find quite what I'm looking for. 我看过几个地方,但似乎找不到我要找的东西。 I'm making a WPF app, using c++/cli to mess with the c# xmal. 我正在制作一个WPF应用程序,使用c ++ / cli弄乱了c#xmal。 I have an interface where I declare whatever I want the xaml to see for DataContext that is in c#, but what I declared in my c# interface is implemented in my c++/cli file. 我有一个接口,可以在其中声明希望xaml为c#中的DataContext查看的内容,但是我在c#接口中声明的内容在c ++ / cli文件中实现。

I'm trying to find a way to declare an enum in c# and then actually define what it is in the c++/cli, but I am having problems with it. 我试图找到一种方法在c#中声明一个枚举,然后实际定义它在c ++ / cli中的含义,但是我遇到了问题。 (Whatever I declare in the c# interface requires an accessor, which might be part of the problem) (无论我在c#接口中声明的内容都需要访问器,这可能是问题的一部分)

What I have tried so far is in the c# interface, 到目前为止,我尝试过的是在c#接口中,

Enum Days { get; }

Then in the c++/cli 然后在c ++ / cli中

enum class days { Sun= 0, Mon, ...}
virtual property System::Enum^ Days
    { System::Enum^ get() { return days; } }

However on the return statement, it says, "type name is not allowed". 但是,在return语句上显示“不允许使用类型名称”。 I think that is because the System::Enum^ is supposed to be value from an enum, not the created enum type itself. 我认为这是因为System :: Enum ^应该是枚举值,而不是所创建的枚举类型本身。 Is there some kind of base class or something that all enum types inherit from, or any other way to do what I'm looking to do? 是否存在某种基类或所有枚举类型都继承的某种基类,或采取其他任何方式来执行我要执行的操作?

I'm trying to find a way to declare an enum in c# and then actually define what it is in the c++/cli. 我试图找到一种方法来在c#中声明一个枚举,然后实际定义它在c ++ / cli中的含义。

You can't declare a type in one assembly and define it in another. 您不能在一个程序集中声明一个类型,而在另一个程序集中定义它。 It doesn't work like that. 它不是那样工作的。

You can declare properties as type Enum in C#, and return any enum value, and that'll work. 您可以在C#中将属性声明为Enum类型,并返回任何枚举值,这将起作用。 However, you need to return an enum value , not the name of an enum class. 但是,您需要返回一个枚举 ,而不是枚举类的名称。

System::Enum^ get() { return days::Monday; } // works

Or, you could declare a property where you inform the rest of the application of the enum type you're using. 或者,您可以声明一个属性,以在其中将所使用的枚举类型通知应用程序的其余部分。 The rest of the application would use reflection to figure out which values are in the enumeration. 应用程序的其余部分将使用反射来找出枚举中的值。

System::Type^ get() { return days::typeid; } // also works.

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

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