问题
Is this supported in C++ CLI? I want to do something like the following C# example in C++ CLI
var dictionary = new Dictionary<string, string> { { "foo", "bar" } };
Thanks
回答1:
The best I came up with was creating an array initialized inline, then initializing the dictionary with the contents of the array in a static constructor. Something like
static initonly System::Collections::Generic::Dictionary<System::String^, System::String^>^ dictionary;
static initonly array<System::String^> arrayToPopulateDictionary = gcnew array<System::String^> { "foo", "bar" };
static Foo()
{
for (int i = 0; i < arrayToPopulateDictionary->Length; i += 2)
listMappings->Add(arrayToPopulateDictionary[i], arrayToPopulateDictionary[i + 1]));
}
回答2:
Unless I'm horribly mistaken, this is simply not possible.
回答3:
It is definitely going to be possible in C++0x, whether that translates to C++/CLI or not is unknown (it should do).
来源:https://stackoverflow.com/questions/1923928/c-cli-collection-initializer-syntax