本文整理汇总了C#中System.Runtime.Serialization.Formatters.Binary.BinaryFormatter类的典型用法代码示例。如果您正苦于以下问题:C# System.Runtime.Serialization.Formatters.Binary.BinaryFormatter类的具体用法?C# System.Runtime.Serialization.Formatters.Binary.BinaryFormatter怎么用?C# System.Runtime.Serialization.Formatters.Binary.BinaryFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter类属于命名空间,在下文中一共展示了System.Runtime.Serialization.Formatters.Binary.BinaryFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadQuestions
public void LoadQuestions(String fileName)
{
Cursor.Current = Cursors.WaitCursor;
try
{
using (System.IO.Stream stream = System.IO.File.Open(fileName, System.IO.FileMode.Open))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
m_questions = (List<Question>)bformatter.Deserialize(stream);
}
using (System.IO.Stream stream = System.IO.File.Open(fileName+"p", System.IO.FileMode.Open))
{
Passage p = new Passage();
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
m_passage = (Passage)bformatter.Deserialize(stream);
}
}
catch (Exception e)
{
Cursor.Current = Cursors.Default;
MessageBox.Show("failed to load the file \"" + fileName + "\"\n\r" + e.Message, "Question Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
m_currentPage = 0;
m_totalPages = m_questions.Count / m_questionsPerPage;
this.RefreshQuestions();
Cursor.Current = Cursors.Default;
}
示例2: SerializeBinary
/// <summary>
/// 序列化为二进制字节数组
/// </summary>
/// <param name="request">要序列化的对象 </param>
/// <returns>字节数组 </returns>
public static byte[] SerializeBinary(object request)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
serializer.Serialize(memStream, request);
return memStream.GetBuffer();
}
示例3: MapFormatter
public MapFormatter()
{
serial = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
serial.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
serial.TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways;
serial.FilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
}
示例4: Read
public LocationCacheRecord Read(string key)
{
byte[] buffer = cache.Read(key);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
LocationCacheRecord lcr = (LocationCacheRecord)formatter.Deserialize(new System.IO.MemoryStream(buffer));
return lcr;
}
示例5: MainWindow
public MainWindow()
{
InitializeComponent();
//if (System.IO.Directory.Exists(cardPath) == false)
//{
// System.IO.Directory.CreateDirectory(cardPath);
//}
if (System.IO.File.Exists(cardPath + "cardList") == false)
{
DownloadAllCardSet();
}
else
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter reader = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream file = System.IO.File.OpenRead(cardPath + "cardList");
card.cardList = (List<card>)reader.Deserialize(file);
file.Close();
}
catch (Exception)
{
MessageBox.Show("Your card database seems to be corrupted, we will redownload it");
DownloadAllCardSet();
}
}
if (System.IO.File.Exists(cardPath + "cardCollection") == true)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter reader = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.FileStream file = System.IO.File.OpenRead(cardPath + "cardCollection");
card.MyCollection = (List<card>)reader.Deserialize(file);
file.Close();
}
}
示例6: ByteArrayToObject
/// <summary>
/// 扩展方法:将二进制byte[]数组反序列化为对象-通过系统提供的二进制流来完成反序列化
/// </summary>
/// <param name="SerializedObj">待反序列化的byte[]数组</param>
/// <param name="ThrowException">是否抛出异常</param>
/// <returns>返回结果</returns>
public static object ByteArrayToObject(this byte[] SerializedObj, bool ThrowException)
{
if (SerializedObj == null)
{
return null;
}
else
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream stream = new System.IO.MemoryStream(SerializedObj);
object obj = formatter.Deserialize(stream);
return obj;
}
catch (System.Exception ex)
{
if (ThrowException)
{
throw ex;
}
else
{
return null;
}
}
}
}
示例7: GetStock
public Stock GetStock(StockName stockName, DateTime startDate, DateTime endDate)
{
string dir = String.Format(@"..\..\StockData\Yahoo");
string filename = String.Format("{0}.stock", stockName);
var fullPath = Path.Combine(dir, filename);
List<IStockEntry> rates;
if (!File.Exists(fullPath))
{
rates = GetStockFromRemote(stockName, startDate, endDate);
Directory.CreateDirectory(dir);
using (Stream stream = File.Open(fullPath, FileMode.Create))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bformatter.Serialize(stream, rates);
}
}
else
{
using (Stream stream = File.Open(fullPath, FileMode.Open))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
rates = (List<IStockEntry>) bformatter.Deserialize(stream);
}
}
var stock = new Stock(stockName, rates);
return stock;
}
示例8: handler
///<summary>
///Forwards incoming clientdata to PacketHandler.
///</summary>
public void handler()
{
clientStream = new SslStream(tcpClient.GetStream(), true);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Kettler_X7_Lib.Objects.Packet pack = null;
X509Certificate serverCertificate = serverControl.getCertificate();
try
{
clientStream.AuthenticateAsServer(serverCertificate,false, SslProtocols.Tls, false);
}
catch (Exception)
{
Console.WriteLine("Authentication failed");
disconnect();
}
for (; ; )
{
try
{
pack = formatter.Deserialize(clientStream) as Kettler_X7_Lib.Objects.Packet;
PacketHandler.getPacket(serverControl, this, pack);
}
catch
{
disconnect();
}
Thread.Sleep(10);
}
}
示例9: Application_AcquireRequestState
//void global_asax_AcquireRequestState(object sender, EventArgs e)
void Application_AcquireRequestState(object sender, EventArgs e)
{
// Code that runs when a new session is started
// get the security cookie
HttpCookie cookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);
if (cookie != null)
{
// we got the cookie, so decrypt the value
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
if (ticket.Expired)
{
// the ticket has expired - force user to login
FormsAuthentication.SignOut();
//Response.Redirect("~/Security/Login.aspx");
}
else
{
// ticket is valid, set HttpContext user value
System.IO.MemoryStream buffer = new System.IO.MemoryStream(Convert.FromBase64String(ticket.UserData));
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
HttpContext.Current.User = (System.Security.Principal.IPrincipal)formatter.Deserialize(buffer);
}
}
}
示例10: Save
/// <summary>
/// Sauvegarde tous les joueurs.
/// </summary>
/// <param name="joueurs">La liste de joueurs a sauvegarder</param>
public static void Save(List<Joueur> joueurs)
{
try
{
using (Stream stream = File.Open(PATH_TO_DAT_FILE, FileMode.Create))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
if (joueurs.Any())
{
bformatter.Serialize(stream, joueurs);
}
}
}
catch (ArgumentException)
{
MessageBox.Show("Le fichier est invalide.");
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("Répertoire introuvable");
}
catch (FileNotFoundException)
{
MessageBox.Show("Le fichier n'existe pas.");
}
catch (IOException)
{
MessageBox.Show("Problème lors de la lecture du fichier.");
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show(e.Message);
}
}
示例11: TestSerializeSchemaPropertyValueCollection
public void TestSerializeSchemaPropertyValueCollection()
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
SchemaPropertyValueCollection obj1 = new SchemaPropertyValueCollection();
SCGroup group = SCObjectGenerator.PrepareGroupObject();
foreach (string key in group.Properties.GetAllKeys())
{
obj1.Add(group.Properties[key]);
}
bf.Serialize(ms, obj1);
ms.Seek(0, System.IO.SeekOrigin.Begin);
var obj2 = (SchemaPropertyValueCollection)bf.Deserialize(ms);
Assert.AreEqual(obj1.Count, obj2.Count);
var keys1 = obj1.GetAllKeys();
foreach (var key in keys1)
{
Assert.IsTrue(obj2.ContainsKey(key));
Assert.AreEqual(obj1[key].StringValue, obj2[key].StringValue);
}
}
示例12: Load
private const string PATH_TO_DAT_FILE = "./joueurs.dat"; //Répertoire courrant.
#endregion Fields
#region Methods
/// <summary>
/// Charge un fichier contenant tous les joueurs.
/// </summary>
/// <returns>La liste de joueurs s'il y en a, sinon null.</returns>
public static List<Joueur> Load()
{
List<Joueur> joueurs = null;
if (File.Exists(PATH_TO_DAT_FILE))
{
try
{
using (Stream stream = File.Open(PATH_TO_DAT_FILE, FileMode.Open))
{
stream.Position = 0;
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
joueurs = (List<Joueur>)bformatter.Deserialize(stream);
}
}
catch (ArgumentException)
{
MessageBox.Show("Le fichier est invalide.");
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("Répertoire introuvable");
}
catch (FileNotFoundException)
{
MessageBox.Show("Le fichier n'existe pas.");
}
catch (IOException)
{
MessageBox.Show("Problème lors de la lecture du fichier.");
}
}
return joueurs;
}
示例13: Compress
public static byte[] Compress(System.Runtime.Serialization.ISerializable obj)
{
// infile.Length];
IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, obj);
byte[] buffer = ms.ToArray();
//Stream stream = new MemoryStream();
// FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
//MyObject obj = (MyObject)formatter.Deserialize(stream);
//stream.Close();
// Use the newly created memory stream for the compressed data.
MemoryStream msOutput = new MemoryStream();
GZipStream compressedzipStream = new GZipStream(msOutput, CompressionMode.Compress, true);
compressedzipStream.Write(buffer, 0, buffer.Length);
// Close the stream.
compressedzipStream.Close();
return msOutput.ToArray();
}
示例14: SettingManager
public SettingManager()
{
_cookieFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
_imageHashSerializer = new System.Xml.Serialization.XmlSerializer(typeof(HashSet<string>));
//設定ファイル読み込み
EmailAddress = GPlusImageDownloader.Properties.Settings.Default.EmailAddress;
Password = GPlusImageDownloader.Properties.Settings.Default.Password;
ImageSaveDirectory = new System.IO.DirectoryInfo(
string.IsNullOrEmpty(GPlusImageDownloader.Properties.Settings.Default.ImageSaveDirectory)
? Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\testFolder"
: GPlusImageDownloader.Properties.Settings.Default.ImageSaveDirectory);
Cookies = DeserializeCookie();
ImageHashList = DeserializeHashes();
if (!ImageSaveDirectory.Exists)
try
{
ImageSaveDirectory.Create();
ImageSaveDirectory.Refresh();
}
catch (System.IO.IOException)
{ IsErrorNotFoundImageSaveDirectory = !ImageSaveDirectory.Exists; }
else
IsErrorNotFoundImageSaveDirectory = false;
}
示例15: Update
/// <summary>
/// Update a business object.
/// </summary>
/// <param name="request">The request parameter object.</param>
public byte[] Update(byte[] req)
{
var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Csla.Server.Hosts.WcfBfChannel.UpdateRequest request;
using (var buffer = new System.IO.MemoryStream(req))
{
request = (Csla.Server.Hosts.WcfBfChannel.UpdateRequest)formatter.Deserialize(buffer);
}
Csla.Server.DataPortal portal = new Csla.Server.DataPortal();
object result;
try
{
result = portal.Update(request.Object, request.Context);
}
catch (Exception ex)
{
result = ex;
}
var response = new WcfResponse(result);
using (var buffer = new System.IO.MemoryStream())
{
formatter.Serialize(buffer, response);
return buffer.ToArray();
}
}