2017-02-27 28 views
0

我想傳遞一個字符串和一個類型到一個通用的方法並試圖反序列化。以下是我已經有了:如何傳遞一個類型到一個通用的反序列化方法

public static T DeserializeObject<T>(string value, Type type) 
{ 
    T result = default(T); 

    try 
    { 
     result = JsonConvert.DeserializeObject<type>(value); 
     System.Diagnostics.Debug.WriteLine($"\nDeserialization Success! : { result }\n"); 

    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine($"\nDeserialization failed with exception : { ex }\n"); 
    } 

    return result; 
} 

我試圖調用其中「GroupObject」是我想返回類型的方法:

var deserialized = Core.Deserializer.DeserializeObject(value: response, type: GroupObject); 

導致錯誤:

Error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected (CS0119) 

可以做到這一點嗎?

回答

1

如果我正確地理解了你,你根本不需要參數type。你已經有一個通用的,可以做

GroupObject deserialized = Core.Deserializer.DeserializeObject<GroupObject>(response) 

這也可以與任何其他類,例如,

Foo foo = Core.Deserializer.DeserializeObject<Foo>(response) 

P.S.我沒有看到你實際上使用了type

+0

我想用它像這樣:Core.Deserializer.DeserializeObject (response)所以我可以重用該方法來反序列化其他類型的對象 –

+0

是的,你可以做到這一點。你只需要在括號''中傳遞你想要的任何類型。這是仿製藥如何工作 –

+0

我編輯了答案。 –

4

您的方法不使用type變量。

此外,打電話與一類參數使用typeof功能的方法:

var deserialized = Core.Deserializer.DeserializeObject(value: response, type: typeof(GroupObject)); 

當然,你不要在這裏提供大量的信息,但在我的經驗,你正在尋找更多的方法等這樣的:

public static T DeserializeObject<T>(string value) 
{ 
    T result = default(T); 
    try { 
     result = JsonConvert.DeserializeObject<T>(value); 
    } 
    return result; 
} 

被稱爲像:

var deserialized = Core.Deserializer.DeserializeObject<GroupObject>(value: response); 
+0

我將包括類型作爲參數的原因是我想使用相同的方法來反序列化其他特定類型的對象 –

+1

'var deserialized = Core.Deserializer.DeserializeObject (value:response);'聽起來像你應該閱讀泛型。 –

+0

我希望能夠在調用方法時指定類型。我會一直傳遞一個字符串,但有時我希望它返回一個CompanyObject,而其他時候我想返回一個GroupObject/SomeOtherObject。 –

0

實際上根本沒有在方法中使用參數type。你實際上並不需要這個參數。您可以從該方法中刪除該參數。

方法並試圖反序列化。以下是我已經有了:

public static T DeserializeObject<T>(string value) 
{ 
    T result = default(T); 

    try 
    { 
     result = JsonConvert.DeserializeObject<T>(value); 
       System.Diagnostics.Debug.WriteLine($"\nDeserialization Success! : { result }\n"); 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine($"\nDeserialization failed with exception : { ex }\n"); 
    } 

    return result; 
} 
-1

這裏是我的解決方案:

public static T DeserializeObject<T>(string inputString) 
{ 
    T result = default(T); 

    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(inputString))) 
    { 
     try 
     { 
      var serializer = new DataContractJsonSerializer(typeof(T)); 

      result = (T)serializer.ReadObject(ms); 

      WriteLine($"\nDeserialization Success : { result }\n"); 
     } 
     catch (Exception ex) 
     { 
      WriteLine($"\nDeserialization Failed With Exception : { ex }\n"); 
     } 
    } 

    return result; 
} 

的「類型」被包含在方法調用:

var deserialized = Core.Deserializer.DeserializeObject<GroupObject>(value: response); 
相關問題