2014-08-30 112 views
2

您好,我无法使用我自己的类结构将此代码实现到Windows Phone的Visual Basic中。我希望能够保存我的对象的实例并从文件中检索它们。一旦我找出写作,阅读应该是相同的。我从这个教程此代码:Serialize Tutorial如何使用JSON序列化器序列化和反序列化MyClass?

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

我不是一个VB的家伙,但是这可能会解决你的问题 '昏暗的串行器=新DataContractJsonSerializer(的GetType(MyClass4 ))'。 – nkchandra 2014-09-02 19:26:59

+0

谢谢你的工作。 – Zocoloni 2016-11-16 18:11:50

+0

@ Zocoloni:考虑接受他的答案,因为它的工作。 – mrsrinivas 2016-11-22 14:17:34

回答

0

我不是一个VB的家伙,但是这可能会解决你的问题

Dim serializer = New DataContractJsonSerializer(GetType(MyClass4));