Проблема с сериализацией на C++ CLR проект
При разработке функции в приложение(WinForms) произошла проблема записи в файл. Мне нужно записать объект класса в файл, а для этого, как я понимаю, надо разрешить сериализацию класса(объекта). Попробовал написать перед определением класса атрибут [Serializable] и [SerializableAttribute], но вылетает ошибка, что атрибут не определен. Код заголовочного файла с определением класса:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
//using namespace System;
//using namespace System::Collections::Generic;
//[SerializableAttribute]
//[ SerializableAttribute ]
public ref class Ingredient {
public:
//bool is_there_one; //показатель - если ли он в рецепте
double amount_gr; // количество в граммах
System::String^ name; // название ингредиента
Ingredient() {
//this->is_there_one = false;
this->amount_gr = 0;
this->name = "";
}
Ingredient(System::String^ name, double count) {
this->name = name;
this->amount_gr = count;
}
~Ingredient() {}
};
//[SerializableAttribute]
ref class Recipe {
public:
Recipe() {}
Recipe(System::String^ name, System::String^ instruction) {
}
System::Collections::Generic::List<Ingredient^>^ Ing_List = gcnew System::Collections::Generic::List<Ingredient^>();
System::String^ name;
System::String^ instruction;
};
Код обработчика нажатия, чтобы записать объект в файл:
private: System::Void button_OK_Click(System::Object^ sender, System::EventArgs^ e) {
try {
BinaryFormatter^ formatter = gcnew BinaryFormatter();
FileStream^ stream = gcnew FileStream("test.bin", FileMode::OpenOrCreate);
formatter->Serialize(stream, Current_Recipe);
stream->Close();
}
catch (Exception^ e) { this->label_warning->Text = e->ToString(); }
}