Pregunta

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.

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

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>.

Any help would be much appreciated!

¿Fue útil?

Solución

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.

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top