C++ DataContractSerialize错误

C++ DataContractSerialize错误,c++,.net,visual-studio-2010,C++,.net,Visual Studio 2010,下面是一个说明问题的小测试代码: 编译配置: 公共语言运行时支持:/clr C++语言 错误消息: 错误4错误C2065:“DataContractSerializer”:未声明的标识符C:…\SerializationTest.cpp 21 1 SerializationTest 守则: //SerializationTest.cpp:主项目文件。 #包括“stdafx.h” 使用命名空间System::Collections::Generic; 使用名称空间系统; 使用名称空间系统::IO;

下面是一个说明问题的小测试代码:

编译配置: 公共语言运行时支持:/clr

C++语言

错误消息: 错误4错误C2065:“DataContractSerializer”:未声明的标识符C:…\SerializationTest.cpp 21 1 SerializationTest

守则:
//SerializationTest.cpp:主项目文件。
#包括“stdafx.h”
使用命名空间System::Collections::Generic;
使用名称空间系统;
使用名称空间系统::IO;
使用名称空间系统::Xml;
使用名称空间System::Runtime::序列化;
int main(数组^args)
{
控制台::WriteLine(L“Hello World”);
字典^teste=gcnew Dictionary();
teste->Add(“Teste1”,2);
测试->添加(“测试2”,4);
DataContractSerializer^serializer=gcnew DataContractSerializer(teste->GetType());
StringWriter^writer=gcnew StringWriter();
XmlTextWriter^stm=gcnewxmltextwriter(writer);
序列化程序->写对象(stm、teste);
控制台::WriteLine(writer->ToString());
返回0;
}

听起来很简单,您缺少对System.Runtime.Serialization.dll的引用(除了使用指令的
之外,还需要该引用):

#使用
// SerializationTest.cpp : main project file.

#include "stdafx.h"

using namespace System::Collections::Generic;
using namespace System;

using namespace System::IO;
using namespace System::Xml;
using namespace System::Runtime::Serialization;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    Dictionary<System::String^, System::Double>^ teste = gcnew Dictionary<System::String^, System::Double>();
    teste->Add("Teste1",2);
    teste->Add("Teste2",4);    

    DataContractSerializer^ serializer = gcnew DataContractSerializer(teste->GetType());
    StringWriter^ writer = gcnew StringWriter();
    XmlTextWriter^ stm = gcnew XmlTextWriter(writer);
    serializer->WriteObject(stm, teste);

    Console::WriteLine(writer->ToString());

    return 0;
}
#using <System.Runtime.Serialization.dll>