【发布时间】:2014-08-30 11:49:23
【问题描述】:
您好,我无法使用我自己的类结构将此代码实现到 Visual Basic for Window phone。我希望能够保存我的对象的实例并从文件中检索它们。一旦我弄清楚写作,阅读应该是相同的。我从本教程中得到了这段代码: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 是一个类型,不能用作表达式。但是参数要求 System.Type 我很困惑。请帮忙。
Dim serializer as New DataContractJsonSeializer(MyClass4) //gives me an error here
【问题讨论】:
-
我不是 VB 人,但这可能会解决你的问题
Dim serializer = New DataContractJsonSerializer(GetType(MyClass4))。 -
感谢工作。
-
@Zocoloni:考虑接受他的回答。
标签: .net json windows-phone-8.1 serialization