您好我无法使用自己的类结构将此代码实现到Visual Basic for Window手机中。我希望能够保存我的对象的实例并从文件中检索它们。一旦我弄清楚写作,阅读应该是相同的。我从本教程中获得了此代码:Serialize Tutorial
private const string JSONFILENAME = "data.json";
private async Task writeJsonAsync()
{
// Notice that the write is ALMOST identical ... except for the serializer.
var myCars = buildObjectGraph();
var serializer = new DataContractJsonSerializer(typeof(List<Car>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
JSONFILENAME,
CreationCollisionOption.ReplaceExisting))
{
serializer.WriteObject(stream, myCars);
}
resultTextBlock.Text = "Write succeeded";
}
我的班级结构是这样的
MyClass1 inherits MyClass2
MyClass2 inherits MyClass3
MyClass4
这是我已经走了多远
Dim test as MyClass4 = new MyClass4
Dim serializer as New DataContractJsonSeializer(MyClass4)
Dim stream as System.IO.Stream = Await ApplicationData.Current.localfolder.OpenStreamForWriteAsync(dir,CreationCollisionOption.ReplaceExisting)
问题是我无法初始化序列化程序,因为MyClass4
是一个Type而不能用作表达式。但参数是要求System.Type我很困惑。请帮忙。
Dim serializer as New DataContractJsonSeializer(MyClass4) //gives me an error here
答案 0 :(得分:0)
我不是VB人,但这可以解决你的问题
Dim serializer = New DataContractJsonSerializer(GetType(MyClass4));