【问题标题】:convert a list of structs from c# to c++将结构列表从 c# 转换为 c++
【发布时间】:2009-11-20 21:44:48
【问题描述】:

我有以下 c# 代码

static void Main(string[] args)
{
            List<Results> votes = new List<Results>();
}
public struct Results
    {
        public int Vote1;
        public int Vote2;
        public int Vote3;
        public Candidate precinctCandidate;
    };
    public class Candidate
    {
        public Candidate()
        {
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }

我想将该代码转换为 Visual c++ CLR,谢谢

【问题讨论】:

    标签: c# c++ visual-c++ clr


    【解决方案1】:

    这里是您的代码到 C++/CLI 的直接翻译:

    using namespace System;
    using namespace System::Collections::Generic;
    
    public ref class Candidate
    {
    public:  
        Candidate()
        {
        }
        property String^ Name
        {
            String^ get() { return this->name; }
            void set(String^ value) { this->name = value; }
        }
        property String^ LastName
        {
           String^ get() { return this->lastName; }
           void set(String^ value) { this->lastName = value; }
        }
    private:
        String^ name;
        String^ lastName;
    };
    
    public value class Results
    {
    public:
        Int32 Vote1;
        Int32 Vote2;
        Int32 Vote3;
        Candidate^ precinctCandidate;
    };
    
    int main(array<String^>^ args)
    {
        List<Results> votes = gcnew List<Results>();
        return 0;
    }
    

    【讨论】:

    • 谢谢尼克,你提到你使用反射器或类似的东西获得了这个代码,但我找不到它,你能告诉 mw 是什么。谢谢
    • 上面的代码是我亲手写的。其他人发布了另一个答案,该答案已被从 Reflector 的 C++ 反编译器中获得的“旧语法”托管 C++ 删除。你可以在这里获得反射器:red-gate.com/products/reflector.
    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    相关资源
    最近更新 更多