1
我使用ProtoBuf序列化在運行時使用Activator.CreateInstance創建的類。 不幸的是,Serializer.Deserialize方法給出錯誤「類型不是預期的,並且不能推斷出合同:System.Object」。任何提示如何解決這個問題。ProtoBuf類型投射問題
var converterName = "Passing class name as string"
var type = Type.GetType(converterName);
var yourObject = Activator.CreateInstance(type)
if (yourObject != null)
{
FillRequest(Request.Params, yourObject);
var com = new CommunicationLayer();
yourObject = com.Submit(yourObject);
FillResponse(yourObject);
}
public class CommunicationLayer
{
public T Submit<T>(T engine)
{
<code skip>
Serializer.Serialize(stream, engine); //Works fine
<code skip>
engine = Serializer.Deserialize<T>(stream); //Gives error
<code skip>
}
}
我選擇通過串運行時創建和鑄造類,因爲converterName變量參數在ASP.NET應用程序通過,這樣的班會約100是的,我可以取代100如果的
整個代碼If converterName=="MyClass1"
{
var yourObject = new MyClass1();
FillRequest(Request.Params, yourObject);
var com = new CommunicationLayer();
yourObject = com.Submit(yourObject);
FillResponse(yourObject);
}
Else
If converterName=="MyClass2"
{
var yourObject = new MyClass2();
FillRequest(Request.Params, yourObject);
var com = new CommunicationLayer();
yourObject = com.Submit(yourObject);
FillResponse(yourObject);
}
Else
....
但我希望儘可能少用代碼。
使用Serializer.NonGeneric類有什麼缺點嗎? – Tomas
@Tomas無論如何;在* regular *代碼中,它有點不太方便(因爲您可能需要給某些方法一個'Type'實例和一個cast或兩個等),但是對於您正在做的事情 - 它是預期的API。 –