我正在尝试序列化一个结构,并能够在以后对其进行反序列化。使用stackoverflow上的上一篇文章,我在以下代码中实现了this article中显示的方法...
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace tcardebug
{
public class Contract1 : SmartContract
{
public static void Main(params object[] args)
{
Driver driver = (Driver)args[0];
string license = (string)args[1];
AddDriver(driver.first, driver.last, license);
Runtime.Notify("Driver Added", driver, license);
}
[Serializable]
public struct Driver
{
public string first;
public string last;
//public string license;
public Driver(string _first, string _last)
{
first = _first;
last = _last;
// license = _license;
}
}
private static byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
private static T ByteArrayToObject<T>(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
T obj = (T)binForm.Deserialize(memStream);
return obj;
}
public static bool AddDriver(string first, string last, string license)
{
Driver driver = new Driver(first, last);
//byte[] keyBytes = Encoding.ASCII.GetBytes(license);
byte[] valueBytes = ObjectToByteArray(driver);
Storage.Put(Storage.CurrentContext, license, valueBytes);
return true;
}
}
}
这给了我以下错误......
CONVERTTASK : Convert error : System.Exception: error:System.Byte[] tcardebug.Contract1::ObjectToByteArray(System.Object)::IL_000D Newobj System.Void System.Runtime.Serialization.Formatters.Binary.BinaryFormatter::.ctor() ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
1> at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
1> at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name)
1> at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name)
1> at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
1> at Mono.Cecil.MetadataResolver.Resolve(MethodReference method)
1> at Mono.Cecil.ModuleDefinition.Resolve(MethodReference method)
1> at Mono.Cecil.MethodReference.Resolve()
1> at Neo.Compiler.MSIL.ModuleConverter._ConvertNewObj(OpCode src, NeoMethod to)
1> at Neo.Compiler.MSIL.ModuleConverter.ConvertCode(ILMethod method, OpCode src, NeoMethod to)
1> at Neo.Compiler.MSIL.ModuleConverter.ConvertMethod(ILMethod from, NeoMethod to)
1> --- End of inner exception stack trace ---
1> at Neo.Compiler.MSIL.ModuleConverter.ConvertMethod(ILMethod from, NeoMethod to)
1> at Neo.Compiler.MSIL.ModuleConverter.Convert(ILModule _in)
1> at Neo.Compiler.Program.Main(String[] args)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
以前,我忘记了[Serializable]在我的结构上面,但添加这个没有帮助。我有什么步骤吗?