例如:public void TestTypeOfKeyWord(object o)
{
string TypeString=typeof(o).ToString();//程序编译就会出错,提示说找不到o对象,是否缺少引用,这是为啥咧,我的o不是在上面定义了吗?
}
{
string TypeString=typeof(o).ToString();//程序编译就会出错,提示说找不到o对象,是否缺少引用,这是为啥咧,我的o不是在上面定义了吗?
}
public void TestTypeOfKeyWord(object o)
{
string TypeString = typeof(object).ToString();
}或者
public void TestTypeOfKeyWord(object o)
{
string TypeString = o.GetType().ToString();
}
不要动不动就来论坛问
上一次那个贴里,我用你的Code有点问题:*.cs文件源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;namespace TestApplication.SerializeTest
{
public partial class SerializeClassToBinary : Form
{
public SerializeClassToBinary()
{
InitializeComponent();
} #region 二进制序列化/反序列化
/// <summary>
/// 序列化对象到BYTE
/// <param name="p_Object"> </param>
/// <returns> </returns>
public static byte[] ObjectToBytes(object p_Object)
{
BinaryFormatter _Formatter = new BinaryFormatter();
MemoryStream _Memory = new MemoryStream();
try
{
_Formatter.Serialize(_Memory, p_Object);
}
catch (Exception ex)
{
}
return _Memory.GetBuffer();
} /// <summary>
/// 反序列 bytes到对象
/// </summary>
/// <param name="p_ObjectBytes"> </param>
/// <returns> </returns>
public static object BytesToObject(byte[] p_ObjectBytes)
{
BinaryFormatter _Formatter = new BinaryFormatter();
MemoryStream _MemStr = new MemoryStream(p_ObjectBytes);
try
{
return _Formatter.Deserialize(_MemStr);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
#endregion #region XML序列化/反序列化
public static XmlSerializer ObjectToXML(Type MyType)//将对象XML序列化
{
XmlSerializer _XML = new XmlSerializer(MyType);
//MemoryStream _Memory = new MemoryStream();
//_XML.Serialize(_Memory, new MyClass());
return _XML;
//_Memory.Position = 0;
//object a = _XML.Deserialize(_Memory);
} public static MemoryStream XMLToMeoryStream(XmlSerializer XML,object o)//将XML序例化内存流化
{
MemoryStream _Memory = new MemoryStream();
XML.Serialize(_Memory, o);
return _Memory;
} public static object XMLToObject(XmlSerializer XML,MemoryStream Memory)//将XML序例化对象化
{
Memory.Position = 0;
object a = XML.Deserialize(Memory);
return a;
} public static MemoryStream BtyesToMemoryStream(byte[] bytes)//将二进制内存流化
{
MemoryStream _Memory = new MemoryStream();
_Memory.Read(bytes, 0, bytes.Length);
return _Memory;
} public static byte[] MemoryStreamToBytes(MemoryStream Memory)//将内存流二进制化
{
byte[] bytes=new byte[256];
Memory.Write(bytes, 0, bytes.Length);
return bytes;
} public static string BytesToMyFormatString(byte[] bytes)
{
string result = "";
foreach (byte b in bytes)
{
result += b.ToString() + ",";
}
return result;
} public static byte[] SpecialStringToBytes(string sourceString,char splitChar)
{
string[] signeStrings = sourceString.Split(new char[] { splitChar });
int Count = signeStrings.Length - 1;
byte[] bytes = new byte[Count];
for (int i = 0; i < Count; i++)
{
bytes[i] = Convert.ToByte(signeStrings[i]);
}
return bytes;
}
#endregion [Serializable]
class MyClass
{
public MyClass()
{
Name = "Jave";
NickName = "JaveLin";
Age = 22;
} private string _name; public string Name
{
get { return _name; }
set { _name = value; }
} private string _nickName; public string NickName
{
get { return _nickName; }
set { _nickName = value; }
} private int _age; public int Age
{
get { return _age; }
set { _age = value; }
} } public class MyNewClass
{
public MyNewClass()
{
Name = "Jave";
NickName = "JaveLin";
Age = 22;
} private string _name; public string Name
{
get { return _name; }
set { _name = value; }
} private string _nickName; public string NickName
{
get { return _nickName; }
set { _nickName = value; }
} private int _age; public int Age
{
get { return _age; }
set { _age = value; }
} } #region 普通序列化
private void button_ObjectSerializeToBinary_Click(object sender, EventArgs e)
{
//对象二进制化
MyClass myclass = new MyClass();
myclass.Name = "Tony";
myclass.NickName = "TonyLin";
myclass.Age = 20;
byte[] bytes = ObjectToBytes(myclass);
this.richTextBox1_Message.Text = "MyClass 序列化成功。结果显示在右边的RichTextBox里。";
foreach (byte b in bytes)
{
this.richTextBox1_Result.Text += b.ToString() + ",";
}
} private void button_BinaryToObject_Click(object sender, EventArgs e)
{
//二进制对象化
string Content = this.richTextBox1_Result.Text;
int Count = 0;
string[] Contents = Content.Split(new char[] { ',' });
Count = Contents.Length - 1;
byte[] bytes = new byte[Count];
for (int i = 0; i < Count; i++)
{
bytes[i] = Convert.ToByte(Contents[i]);
}
object myClass = BytesToObject(bytes);
MyClass one = myClass as MyClass;
string content = one.Name + " is my name." + "\r\n" +
one.NickName + " is my nickname." + "\r\n" +
one.Age + " is my age.";
this.richTextBox2_Message.Text = "MyClass 反序列化成功。结果显示在右边的RichTextBox里。";
this.richTextBox2_Result.Text = content;
}
#endregion #region XML序列化 MyNewClass myNewClass = new MyNewClass(); private void button_XMLSerialize_Click(object sender, EventArgs e)
{
XmlSerializer XML = ObjectToXML(myNewClass.GetType());
MemoryStream memoryStream = XMLToMeoryStream(XML, myNewClass);
byte[] bytes = MemoryStreamToBytes(memoryStream);
string result = BytesToMyFormatString(bytes);
string sourceClass =
myNewClass.Name + " is my name." + "\r\n" +
myNewClass.NickName + " is my nickname." + "\r\n" +
myNewClass.Age + " is my age.";
this.richTextBox_SourceClass.Text = sourceClass;
this.richTextBox_XMLSerializeInfo.Text = result;
} private void button_XMLSerializeRestore_Click(object sender, EventArgs e)
{
XmlSerializer XML = ObjectToXML(myNewClass.GetType());
byte[] bytes = SpecialStringToBytes(this.richTextBox_XMLSerializeInfo.Text, ',');
MemoryStream memoryStream = BtyesToMemoryStream(bytes);
MyNewClass temp_myNewClass = XMLToObject(XML, memoryStream) as MyNewClass;
string restoreClass =
temp_myNewClass.Name + " is my name." + "\r\n" +
temp_myNewClass.NickName + " is my nickname." + "\r\n" +
temp_myNewClass.Age + " is my age.";
this.richTextBox_RestoreClass.Text = restoreClass;
}
#endregion
}
}
#region Windows Form Designer generated code/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button_ObjectSerializeToBinary = new System.Windows.Forms.Button();
this.richTextBox1_Result = new System.Windows.Forms.RichTextBox();
this.richTextBox1_Message = new System.Windows.Forms.RichTextBox();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button_BinaryToObject = new System.Windows.Forms.Button();
this.richTextBox2_Result = new System.Windows.Forms.RichTextBox();
this.richTextBox2_Message = new System.Windows.Forms.RichTextBox();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.button_XMLSerializeRestore = new System.Windows.Forms.Button();
this.button_XMLSerialize = new System.Windows.Forms.Button();
this.richTextBox_RestoreClass = new System.Windows.Forms.RichTextBox();
this.richTextBox_XMLSerializeInfo = new System.Windows.Forms.RichTextBox();
this.richTextBox_SourceClass = new System.Windows.Forms.RichTextBox();
this.groupBox1.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.tabPage2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button_ObjectSerializeToBinary);
this.groupBox1.Controls.Add(this.richTextBox1_Result);
this.groupBox1.Controls.Add(this.richTextBox1_Message);
this.groupBox1.Location = new System.Drawing.Point(6, 6);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(548, 196);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "对象二进制化";
//
// button_ObjectSerializeToBinary
//
this.button_ObjectSerializeToBinary.Location = new System.Drawing.Point(262, 91);
this.button_ObjectSerializeToBinary.Name = "button_ObjectSerializeToBinary";
this.button_ObjectSerializeToBinary.Size = new System.Drawing.Size(33, 23);
this.button_ObjectSerializeToBinary.TabIndex = 1;
this.button_ObjectSerializeToBinary.Text = ">>";
this.button_ObjectSerializeToBinary.UseVisualStyleBackColor = true;
this.button_ObjectSerializeToBinary.Click += new System.EventHandler(this.button_ObjectSerializeToBinary_Click);
//
// richTextBox1_Result
//
this.richTextBox1_Result.Location = new System.Drawing.Point(301, 21);
this.richTextBox1_Result.Name = "richTextBox1_Result";
this.richTextBox1_Result.Size = new System.Drawing.Size(241, 169);
this.richTextBox1_Result.TabIndex = 0;
this.richTextBox1_Result.Text = "";
//
// richTextBox1_Message
//
this.richTextBox1_Message.Location = new System.Drawing.Point(7, 21);
this.richTextBox1_Message.Name = "richTextBox1_Message";
this.richTextBox1_Message.Size = new System.Drawing.Size(249, 169);
this.richTextBox1_Message.TabIndex = 0;
this.richTextBox1_Message.Text = "";
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(555, 468);
this.tabControl1.TabIndex = 2;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.groupBox2);
this.tabPage1.Controls.Add(this.groupBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 21);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(547, 443);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "对象二进制序列化";
this.tabPage1.UseVisualStyleBackColor = true;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button_BinaryToObject);
this.groupBox2.Controls.Add(this.richTextBox2_Result);
this.groupBox2.Controls.Add(this.richTextBox2_Message);
this.groupBox2.Location = new System.Drawing.Point(8, 208);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(548, 196);
this.groupBox2.TabIndex = 0;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "对象二进制化";
//
// button_BinaryToObject
//
this.button_BinaryToObject.Location = new System.Drawing.Point(262, 91);
this.button_BinaryToObject.Name = "button_BinaryToObject";
this.button_BinaryToObject.Size = new System.Drawing.Size(33, 23);
this.button_BinaryToObject.TabIndex = 1;
this.button_BinaryToObject.Text = "<<";
this.button_BinaryToObject.UseVisualStyleBackColor = true;
this.button_BinaryToObject.Click += new System.EventHandler(this.button_BinaryToObject_Click);
//
// richTextBox2_Result
//
this.richTextBox2_Result.Location = new System.Drawing.Point(301, 21);
this.richTextBox2_Result.Name = "richTextBox2_Result";
this.richTextBox2_Result.Size = new System.Drawing.Size(241, 169);
this.richTextBox2_Result.TabIndex = 0;
this.richTextBox2_Result.Text = "";
//
// richTextBox2_Message
//
this.richTextBox2_Message.Location = new System.Drawing.Point(7, 21);
this.richTextBox2_Message.Name = "richTextBox2_Message";
this.richTextBox2_Message.Size = new System.Drawing.Size(249, 169);
this.richTextBox2_Message.TabIndex = 0;
this.richTextBox2_Message.Text = "";
//
// tabPage2
//
this.tabPage2.Controls.Add(this.button_XMLSerializeRestore);
this.tabPage2.Controls.Add(this.button_XMLSerialize);
this.tabPage2.Controls.Add(this.richTextBox_RestoreClass);
this.tabPage2.Controls.Add(this.richTextBox_XMLSerializeInfo);
this.tabPage2.Controls.Add(this.richTextBox_SourceClass);
this.tabPage2.Location = new System.Drawing.Point(4, 21);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(547, 443);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "对象XML序列化";
this.tabPage2.UseVisualStyleBackColor = true;
//
// button_XMLSerializeRestore
//
this.button_XMLSerializeRestore.Location = new System.Drawing.Point(229, 259);
this.button_XMLSerializeRestore.Name = "button_XMLSerializeRestore";
this.button_XMLSerializeRestore.Size = new System.Drawing.Size(75, 23);
this.button_XMLSerializeRestore.TabIndex = 1;
this.button_XMLSerializeRestore.Text = "<<";
this.button_XMLSerializeRestore.UseVisualStyleBackColor = true;
this.button_XMLSerializeRestore.Click += new System.EventHandler(this.button_XMLSerializeRestore_Click);
//
// button_XMLSerialize
//
this.button_XMLSerialize.Location = new System.Drawing.Point(229, 143);
this.button_XMLSerialize.Name = "button_XMLSerialize";
this.button_XMLSerialize.Size = new System.Drawing.Size(75, 23);
this.button_XMLSerialize.TabIndex = 1;
this.button_XMLSerialize.Text = ">>";
this.button_XMLSerialize.UseVisualStyleBackColor = true;
this.button_XMLSerialize.Click += new System.EventHandler(this.button_XMLSerialize_Click);
//
// richTextBox_RestoreClass
//
this.richTextBox_RestoreClass.Location = new System.Drawing.Point(3, 232);
this.richTextBox_RestoreClass.Name = "richTextBox_RestoreClass";
this.richTextBox_RestoreClass.Size = new System.Drawing.Size(214, 205);
this.richTextBox_RestoreClass.TabIndex = 0;
this.richTextBox_RestoreClass.Text = "";
//
// richTextBox_XMLSerializeInfo
//
this.richTextBox_XMLSerializeInfo.Location = new System.Drawing.Point(310, 107);
this.richTextBox_XMLSerializeInfo.Name = "richTextBox_XMLSerializeInfo";
this.richTextBox_XMLSerializeInfo.Size = new System.Drawing.Size(214, 209);
this.richTextBox_XMLSerializeInfo.TabIndex = 0;
this.richTextBox_XMLSerializeInfo.Text = "";
//
// richTextBox_SourceClass
//
this.richTextBox_SourceClass.Location = new System.Drawing.Point(8, 6);
this.richTextBox_SourceClass.Name = "richTextBox_SourceClass";
this.richTextBox_SourceClass.Size = new System.Drawing.Size(202, 177);
this.richTextBox_SourceClass.TabIndex = 0;
this.richTextBox_SourceClass.Text = "";
//
// SerializeClassToBinary
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(555, 468);
this.Controls.Add(this.tabControl1);
this.Name = "SerializeClassToBinary";
this.Text = "SerializeClassToBinary";
this.groupBox1.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button_ObjectSerializeToBinary;
private System.Windows.Forms.RichTextBox richTextBox1_Result;
private System.Windows.Forms.RichTextBox richTextBox1_Message;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button button_XMLSerializeRestore;
private System.Windows.Forms.Button button_XMLSerialize;
private System.Windows.Forms.RichTextBox richTextBox_RestoreClass;
private System.Windows.Forms.RichTextBox richTextBox_XMLSerializeInfo;
private System.Windows.Forms.RichTextBox richTextBox_SourceClass;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button_BinaryToObject;
private System.Windows.Forms.RichTextBox richTextBox2_Result;
private System.Windows.Forms.RichTextBox richTextBox2_Message;
程序运行后,点“对象XML序列化”的标签里。再点一下“>>”按钮,再一点一下“<<”按钮就会出错了。
你怎么知道我没看MSDN里的说明呢?正是因为我理解能力没你的强。所以我才要向各位大哥再拿一些通俗化一点的回答。MSDN里有些翻译太蒙人。或是说我理解能力不行。这些我都承认。MSDN我慢慢来适应一下。