C# 2008(.net)百分跪求高手关于自定义的类转换byte[]问题!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 2008(.net)百分跪求高手关于自定义的类转换byte[]问题!相关的知识,希望对你有一定的参考价值。

把一个DataTable类转换成Byte数组可以这样
DataTable LeafDataTableTemp;
byte[] LeafDataTableByteTemp;
System.IO.MemoryStream LeafMemoryStreamTemp = new MemoryStream();
BinaryFormatter LeafBinaryFormatterTemp = new BinaryFormatter();
LeafBinaryFormatterTemp.Serialize(LeafMemoryStreamTemp, LeafDataTableTemp);
LeafDataTableByteTemp = LeafMemoryStreamTemp.GetBuffer();

然后从Byte数组转换回DataTable类可以这样
byte[] LeafDataTableByteTemp;
DataTable LeafData;
BinaryFormatter LeafBinaryFormatterTemp = new BinaryFormatter();
LeafData = (DataTable)LeafBinaryFormatterTemp.Deserialize(new MemoryStream(LeafDataTableByteTemp));

那么假如我自定义一个类
class LeafClassDataPack

public string a;
public int b;
public byte[] c;

我如何把这个类转换成二进制数据byte[]
又如何从二进制数据byte[]转换回这个类呢

要序列化一个对象,首先设置该对象类是可以序列化的,然后序列化为二进制或XML数据。
你的方法是没问题的,goorcome说得对,只能序列化实例。另外你的类声明没有说明可序列化。

[Serializable()]
class LeafClassDataPack

public string a;
public int b;
public byte[] c;

下面是测试代码:
private void button3_Click(object sender, EventArgs e)

LeafClassDataPack lcdp= DeserializeMyClass(SerializeMyClass());
Text = lcdp.a;

private byte[] SerializeMyClass()

LeafClassDataPack leafClassDataPack=new LeafClassDataPack();
leafClassDataPack.a = "test";
byte[] leafMyClassByteTemp;
MemoryStream leafMemoryStreamTemp = new MemoryStream();
BinaryFormatter leafBinaryFormatterTemp = new BinaryFormatter();
leafBinaryFormatterTemp.Serialize(leafMemoryStreamTemp, leafClassDataPack);
leafMyClassByteTemp = leafMemoryStreamTemp.GetBuffer();
return leafMyClassByteTemp;

private LeafClassDataPack DeserializeMyClass(byte[] leafMyClassByteTemp)

LeafClassDataPack leafClassDataPack;
BinaryFormatter leafBinaryFormatterTemp = new BinaryFormatter();
leafClassDataPack = (LeafClassDataPack)leafBinaryFormatterTemp.Deserialize(new MemoryStream(leafMyClassByteTemp));
return leafClassDataPack;
参考技术A 觉得只能转换数据,实例什么得,不能转换类本身吧

c#大圣之路笔记——c# DataGrid checkbox 操作

  1 ///html
  2 
  3 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xOEMStoreNominationForCAM.aspx.cs" EnableEventValidation="false" Inherits="PRCSales_external.Store.xOEMStoreNominationForCAM" %>
  4 
  5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6 
  7 <html xmlns="http://www.w3.org/1999/xhtml">
  8 <head runat="server">
  9     <title>xOEM Store Nomination for CAM</title>
 10     <link href="../css/main.css" rel="stylesheet" type="text/css" />
 11     <link href="../css/Styles_1.css" rel="stylesheet" type="text/css" />
 12     
 13     <script type="text/javascript" src="../js/jquery-1.8.2.js"></script>
 14     <script type="text/javascript" src="../js/LocationFilterQuarter.js"></script>
 15 
 16     <script type="text/javascript">
 17 
 18 
 19             function CheckAll(chkObject) {
 20                 var checkArray = document.getElementsByTagName("input");
 21                 for (j = 0; j < checkArray.length; j++) {
 22                     if (checkArray[j].type == "checkbox" & checkArray[j] != chkObject ) {
 23                         if (chkObject.checked != checkArray[j].checked)
 24                             checkArray[j].checked = chkObject.checked;
 25                     }
 26                 }
 27             }
 28 
 29          
 30 
 31 
 32             function ShowWaiting() {
 33                 document.getElementById(doing).style.visibility = visible;
 34             }
 35             function CloseWaiting() {
 36                 document.getElementById(doing).style.visibility = hidden;
 37             }
 38             function MyOnload() {
 39                 document.getElementById(doing).style.visibility = hidden;
 40             }
 41 
 42             if (window.onload == null) {
 43                 window.onload = MyOnload;
 44             }
 45 
 46     </script>
 47 
 48 
 49     <style type="text/css">
 50             #btnMark ,#btnUnmark
 51             {
 52                 margin-left:5px;
 53             }
 54     
 55     </style>
 56 
 57 
 58 </head>
 59 <body>
 60     <form id="form1" runat="server"> 
 61     <table width="100%" id="Table1">
 62         <tr ><td style="height:30px; "><b>xOEM Store Nomination for CAM</b></td></tr>
 63         <tr>
 64          <tr style="HEIGHT: 1px; BACKGROUND-COLOR: #3366ff">
 65                     <td colSpan="4"><FONT style="BACKGROUND-COLOR: #ffffff" face="宋体"></FONT><FONT face="宋体"></FONT></td>
 66                 </tr>
 67         <tr>
 68                 <td class="fieldtitle" width="30%">Quarter:</td>
 69                 <td class="fieldtitle" width="20%"><asp:dropdownlist id="ddlQuarter" runat="server"  AutoPostBack="true"
 70                         onselectedindexchanged="ddlQuarter_SelectedIndexChanged"  onchange="initData(‘region‘)"></asp:dropdownlist></td>
 71                 <td class="fieldtitle" width="20%">
 72                     Region/Grid/City:
 73                 </td>
 74                 <td class="fieldtitle" colspan="3" width="30%">
 75                     <asp:DropDownList ID="ddlRegion" runat="server" CssClass="fieldList" onchange="initData(‘subregion‘)">
 76                     </asp:DropDownList>
 77                     <asp:DropDownList ID="ddlSubRegion" runat="server" CssClass="fieldList" style="display:none;" onchange="initData(‘grid‘)">
 78                     </asp:DropDownList>
 79                     <asp:DropDownList ID="ddlGrid" runat="server" CssClass="fieldList"  onchange="initData(‘pref‘)">
 80                     </asp:DropDownList>
 81                     <asp:DropDownList ID="ddlPref" runat="server" CssClass="fieldList" style="display:none;"  onchange="initData(‘city‘)">
 82                     </asp:DropDownList>
 83                     <asp:DropDownList ID="ddlCity" runat="server" CssClass="fieldList"  onchange="initData(‘county‘)">
 84                     </asp:DropDownList>
 85                     <asp:DropDownList ID="ddlCounty" runat="server" CssClass="fieldList" style="display:none;" onchange="initData(‘town‘)">
 86                     </asp:DropDownList>
 87                     <asp:DropDownList ID="ddlTown" runat="server" CssClass="fieldList" style="display:none;" onchange="initData(‘village‘)">
 88                     </asp:DropDownList>
 89                     <asp:DropDownList ID="ddlVillage" runat="server" CssClass="fieldList" style="display:none;" onchange="initData(‘village1‘)">
 90                     </asp:DropDownList>
 91                         
 92                     <asp:TextBox ID="txtWWID" runat="server" Style="display: none"></asp:TextBox>
 93                     <asp:TextBox ID="txtQuarter" runat="server" Style="display: none"></asp:TextBox>
 94                     <asp:TextBox ID="txtRegion" runat="server" Style="display: none"></asp:TextBox>
 95                     <asp:TextBox ID="txtSubRegion" runat="server" Style="display: none"></asp:TextBox>
 96                     <asp:TextBox ID="txtGrid" runat="server" Style="display: none"></asp:TextBox>
 97                     <asp:TextBox ID="txtPref" runat="server" Style="display: none"></asp:TextBox>
 98                     <asp:TextBox ID="txtCity" runat="server" Style="display: none"></asp:TextBox>
 99                     <asp:TextBox ID="txtCounty" runat="server" Style="display: none"></asp:TextBox>
100                     <asp:TextBox ID="txtTown" runat="server" Style="display: none"></asp:TextBox>
101                     <asp:TextBox ID="txtVillage" runat="server" Style="display: none"></asp:TextBox>
102                 </td>
103 
104         </tr>
105         <tr>
106             <td class="fieldtitle" width="30%" >Store Name:</td>
107             <td class="fieldtitle" width="20%" colspan="1"><asp:TextBox ID="txtStorNm" runat="server"></asp:TextBox></td>
108             <td class="fieldtitle" width="20%" >Store ID:</td>
109             <td class="fieldtitle" width="30%"><asp:TextBox ID="txtStorID" runat="server"></asp:TextBox></td>
110         </tr>
111        <%-- <tr>
112             <td  class="fieldtitle" width="20%" colspan=""><asp:CheckBox ID="chkStore"  runat="server" CssClass="fieldtitle" Text="Show Unassigned Store Only"/></td>
113         </tr>--%>
114         <tr style="HEIGHT: 1px; BACKGROUND-COLOR: #3366ff">
115                     <td colSpan="4"><FONT style="BACKGROUND-COLOR: #ffffff" face="宋体"></FONT><FONT face="宋体"></FONT></td>
116                 </tr>
117         <tr>
118             <td class="fieldtitle" width="30%" colspan="1">Store list <asp:Label ID="lblCount" runat="server" CssClass="fieldList"></asp:Label>
119               <%-- <asp:Label ID="lblMessage" runat="server" CssClass="fieldList" ForeColor="Red" Text="季度店面标记"></asp:Label>  --%>
120                     <asp:Label ID="lblLeftTime" CssClass="fieldList" runat="server" ForeColor="Red"></asp:Label>  
121             </td>
122              
123             <td align="right" colspan="3" width="70%">
124             <asp:Button ID="btnSearch" runat="server" Text="Search"   CssClass="fieldButton" onclick="btnSearch_Click"  Width="80px"/> 
125             <asp:Button ID="btnMark" runat="server" Text="Mark"   CssClass="fieldButton" onclick="btnMark_Click" Width="80px" />
126             <asp:Button ID="btnUnmark" runat="server" Text="Unmark" CssClass="fieldButton"   onclick="btnUnmark_Click" Width="80px" />
127             </td>
128         </tr>
129         <tr>
130             <td colspan="4">
131                 <asp:DataGrid ID="GridMain" runat="server" Width="100%" 
132                     AutoGenerateColumns="false" PageSize="20" AllowPaging="true" 
133                      onselectedindexchanged="GridMain_SelectedIndexChanged"  onpageindexchanged="GridMain_PageIndexChanged">
134                 <AlternatingItemStyle CssClass="datagridAlternating" />
135                 <HeaderStyle CssClass="tableHead" />
136                 <Columns>
137                     <asp:TemplateColumn>
138                    <HeaderStyle Width="20px"></HeaderStyle>
139                      <HeaderTemplate><asp:CheckBox ID ="checkAll" runat="server" onclick="CheckAll(this);" /></HeaderTemplate>
140                      <ItemTemplate><asp:CheckBox ID="chkSelected" runat="server"  /></ItemTemplate>
141                     </asp:TemplateColumn>
142                     <asp:BoundColumn DataField="stor_rgn_nm" HeaderText="Store Region">
143                                     <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle>
144                     </asp:BoundColumn>
145                   <asp:BoundColumn DataField="stor_grid_cd" HeaderText="Store Grid">
146                                     <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle>
147                     </asp:BoundColumn>
148                     <asp:BoundColumn DataField="stor_city_std_loc_nm" HeaderText="Store City">
149                                     <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle>
150                     </asp:BoundColumn>
151                      <asp:BoundColumn DataField="stor_id" HeaderText="Store ID">
152                                     <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle>
153                     </asp:BoundColumn>
154                      <asp:BoundColumn DataField="stor_nm" HeaderText="Store Name">
155                                     <ItemStyle HorizontalAlign="Center" Width="8%"></ItemStyle>
156                     </asp:BoundColumn>
157                     <asp:BoundColumn DataField="stor_addr" HeaderText="Store Addr">
158                                     <ItemStyle HorizontalAlign="Center" Width="8%"></ItemStyle>
159                     </asp:BoundColumn>
160                      <asp:BoundColumn DataField="brnd_nm" HeaderText="Store Brand">
161                                     <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle>
162                     </asp:BoundColumn>
163                      <asp:BoundColumn DataField="xoem_store_tier" HeaderText="Store Tier">
164                                     <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle>
165                     </asp:BoundColumn>
166                      <asp:BoundColumn DataField="remark" HeaderText="Store Bis Type">
167                                     <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle>
168                     </asp:BoundColumn>
169                      <asp:BoundColumn DataField="oem_stor_id" HeaderText="OEM Store ID">
170                                     <ItemStyle HorizontalAlign="Center" Width="4%"></ItemStyle>
171                     </asp:BoundColumn>
172                      <asp:BoundColumn DataField="rep_nm" HeaderText="Store Manager">
173                                     <ItemStyle HorizontalAlign="Center" Width="3%"></ItemStyle>
174                     </asp:BoundColumn>
175                        <asp:BoundColumn DataField="slsprs_nm" HeaderText="SR Owner">

以上是关于C# 2008(.net)百分跪求高手关于自定义的类转换byte[]问题!的主要内容,如果未能解决你的问题,请参考以下文章

c# lock static对象, 跪求高手。

求解,C#里面已经知道窗口句柄,怎么隐藏这个窗口呢?跪求高手指点!

跪求高手指教ASP.NET内容页中添加一个.HTML表单页面???急急急

C#:以自定义百分比创建 CPU 使用率

跪求一道AutoCAD2008用visual studio2005二次开发的题目答案!!悬赏分多多的!!

C# System.Net.WebException 检索自定义 404 消息