我有要转换为Byte []数组的员工对象列表...
我使用以下代码进行转换:
private byte[] ObjectToByteArray(List<Employee> obj)
{
if (obj == null)
return null;
var bf = new BinaryFormatter();
var ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
但它给了我以下错误..
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
at ExportToExcel.Controllers.HomeController.ObjectToByteArray(List`1 obj) in C:\Users\mutturajnb\Documents\Visual Studio 2010\Projects\ExportToExcel\ExportToExcel\Controllers\HomeController.cs:line 46
at ExportToExcel.Controllers.HomeController.Index() in C:\Users\mutturajnb\Documents\Visual Studio 2010\Projects\ExportToExcel\ExportToExcel\Controllers\HomeController.cs:line 26
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
我是否需要使用SerializableAttribute
装饰我的班级成员?
答案 0 :(得分:1)
至少,您需要确保将Employee(以及所有组成成员)标记为可序列化。
感谢您发布堆栈跟踪,但如果这不能解决您的问题,那么异常消息也会有所帮助!