Native / CLI C ++:如何从vector 转换为List

时间:2022-03-18 05:05:49

I'm writing a Native/CLI DLL in C++. I'll eventually call the DLL from C# code (which I'm much more familiar with), but I'm trying to wrap my Native C++ classes with a CLI wrapper.

我正在用C ++编写Native / CLI DLL。我最终会用C#代码调用DLL(我比较熟悉),但是我试图用CLI包装器封装我的Native C ++类。

So my question is, what's the best way for me to convert an std::vector to a List class?

所以我的问题是,将std :: vector转换为List类的最佳方法是什么?

The classes are mostly simple, the most complex looks like this:

这些类大多是简单的,最复杂的看起来像这样:

class SecurityPrincipal
{
public:
    wstring distinguishedName;
    SECURITYPRINCIPAL_NODE_TYPE NodeType;
    vector<LDAPAttribute> Attributes;
    vector<SecurityPrincipal> Nodes;
}

To be honest, I haven't even been able to get a vector<wstring> into a List<String>.

说实话,我甚至无法将vector 转换为List

Any help would be much appreciated!

任何帮助将非常感激!

1 个解决方案

#1


4  

I'm not aware of any standard algorithm/function included with C++ that allows that level of transformation. But is there any reason a for loop won't work? The following is brain-compiled.

我不知道C ++中包含的任何标准算法/函数允许该级别的转换。但是有什么理由for循环不起作用?以下是大脑编译。

typedef System::Collections::Generic::List<class1_cli> MyList;
typedef std::vector<class1_native> MyVector;

MyList^ NativeToManaged(MyVector& v) {
    MyList^ result = gcnew MyList();
    if (result != nullptr) {
        for (MyVector::iterator i = v.begin(); i != v.end(); ++i) {
            class1_native& nativeValue = *i;
            result.Add(gcnew class1_cli(nativeValue));
        }
    }
    return result;
}

#1


4  

I'm not aware of any standard algorithm/function included with C++ that allows that level of transformation. But is there any reason a for loop won't work? The following is brain-compiled.

我不知道C ++中包含的任何标准算法/函数允许该级别的转换。但是有什么理由for循环不起作用?以下是大脑编译。

typedef System::Collections::Generic::List<class1_cli> MyList;
typedef std::vector<class1_native> MyVector;

MyList^ NativeToManaged(MyVector& v) {
    MyList^ result = gcnew MyList();
    if (result != nullptr) {
        for (MyVector::iterator i = v.begin(); i != v.end(); ++i) {
            class1_native& nativeValue = *i;
            result.Add(gcnew class1_cli(nativeValue));
        }
    }
    return result;
}