本文整理汇总了C#中System.Runtime.Serialization.NetDataContractSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# NetDataContractSerializer.Serialize方法的具体用法?C# NetDataContractSerializer.Serialize怎么用?C# NetDataContractSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.Serialization.NetDataContractSerializer
的用法示例。
在下文中一共展示了NetDataContractSerializer.Serialize方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Serialize
public override byte[] Serialize(object obj)
{
var formatter = new NetDataContractSerializer();
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, obj);
return ms.ToArray();
}
}
示例2: Clone
/// <summary>
/// Creates a new instance of the class with the same value as instance.
/// </summary>
/// <returns>Returns cloned object.</returns>
public virtual object Clone()
{
IFormatter formatter = new NetDataContractSerializer();
using(Stream stream = new MemoryStream())
{
formatter.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(stream);
}
}
示例3: SholdSerializeOperator
public void SholdSerializeOperator()
{
// Given
var entity = new FilterTestEntity { StringField = "Abc", IntField = 5 };
// Какое-то сложное логическое выражение
var specification = new FilterSpecification<FilterTestEntity>(
b => b.Or(
c => c
.And(a => a.Or(o => o
.Null(i => i.StringField)
.Equal(i => i.StringField, string.Empty))
.Equal(i => i.IntField, 0))
.And(a => a
.NotNull(i => i.StringField)
.NotEqual(i => i.StringField, string.Empty)
.Or(o => o
.In(i => i.StringField.ToLower(), new[] { "a", "b", "c" })
.Contains(i => i.StringField.ToLower(), "abc")
.StartsWith(i => i.StringField, "1")
.EndsWith(i => i.StringField, "5"))
.Or(o => o
.And(a2 => a2
.GreaterOrEqual(i => i.IntField, 0)
.LessOrEqual(i => i.IntField, 10))
.And(a2 => a2
.Greater(i => i.IntField, 10)
.Less(i => i.IntField, 20))
.Between(i => i.IntField, 20, 30)))));
IFilterOperator operatorFromStream;
IFilterOperator operatorToStream = specification.Operator;
// When
using (var stream = new MemoryStream())
{
// Сериализация
var serializer = new NetDataContractSerializer();
serializer.Serialize(stream, operatorToStream);
stream.Position = 0;
// Десериализация
operatorFromStream = (IFilterOperator)serializer.ReadObject(stream);
}
// Then
Assert.IsNotNull(operatorFromStream);
Assert.AreEqual(operatorToStream.IsSatisfiedBy(entity), operatorFromStream.IsSatisfiedBy(entity));
}
示例4: Serialize
private void Serialize(NetDataContractSerializer serializer, XElement parent, string name, object value)
{
XElement element = new XElement(name);
using(MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, value);
stream.Position = 0;
element.Add(XElement.Load(stream));
}
parent.Add(element);
}
示例5: SerializeObject
public XmlElement SerializeObject(string elementName, object o, XmlDocument doc)
{
var serializer = new NetDataContractSerializer();
var newElement = doc.CreateElement(elementName);
using (var memoryStream = new MemoryStream())
{
serializer.Serialize(memoryStream, o);
memoryStream.Position = 0;
var rdr = new StreamReader(memoryStream);
newElement.InnerXml = rdr.ReadToEnd();
}
return newElement;
}
示例6: SerializeObject
/// <summary>
/// The serialize object.
/// </summary>
/// <param name="elementName">The element name.</param>
/// <param name="objectToSerialize">The object to serialize.</param>
/// <param name="xmlDocument">The xml document.</param>
/// <returns>The <see cref="XmlElement" />.</returns>
private static XmlElement SerializeObject(string elementName, object objectToSerialize, XmlDocument xmlDocument)
{
var netDataContractSerializer = new NetDataContractSerializer();
var newElement = xmlDocument.CreateElement(elementName);
var memoryStream = new MemoryStream();
netDataContractSerializer.Serialize(memoryStream, objectToSerialize);
memoryStream.Position = 0;
var rdr = new StreamReader(memoryStream);
newElement.InnerXml = rdr.ReadToEnd();
return newElement;
}
示例7: SerializeObject
XmlElement SerializeObject(string elementName, object o, XmlDocument doc)
{
NetDataContractSerializer s = new NetDataContractSerializer();
XmlElement newElement = doc.CreateElement(elementName);
MemoryStream stm = new MemoryStream();
s.Serialize(stm, o);
stm.Position = 0;
StreamReader rdr = new StreamReader(stm);
newElement.InnerXml = rdr.ReadToEnd();
return newElement;
}
示例8: ProvideFault
public void ProvideFault(Exception error, NDceRpc.ServiceModel.Channels.MessageVersion version, ref NDceRpc.ServiceModel.Channels.Message fault)
{
if (!HandleError(error))
{
return;
}
var serializer = new NetDataContractSerializer();
var stream = new MemoryStream();
serializer.Serialize(stream, error);
stream.Position = 0;
fault.Fault.Detail = stream.ToArray();
}
示例9: Main
static void Main()
{
var orig = new Game
{
Finished = true,
GameGUID = Guid.NewGuid(),
GameID = 12345,
GameSetup = false,
MaximumCardsInDeck = 20,
Player = new Player { Name = "Fred" },
Player1 = new Player { Name = "Barney" },
Player1Connected = true,
Player1EnvironmentSetup = true,
Player1ID = 12345,
Player1Won = 3,
Player2Connected = true,
Player2EnvironmentSetup = true,
Player2ID = 23456,
Player2Won = 0,
Round = 4,
RoundsToWin = 5,
Started = true,
StateXML = "not really xml",
TimeEnded = null,
TimeLimitPerTurn = 500,
TimeStamp = new byte[] { 1, 2, 3, 4, 5, 6 },
TimeStarted = DateTime.Today
};
const int LOOP = 50000;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
using (var ms = new MemoryStream())
{
var ser = new BinaryFormatter();
Console.WriteLine();
Console.WriteLine(ser.GetType().Name);
ser.Serialize(ms, orig);
Console.WriteLine("Length: " + ms.Length);
ms.Position = 0;
ser.Deserialize(ms);
var watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
ms.Position = 0;
ms.SetLength(0);
ser.Serialize(ms, orig);
}
watch.Stop();
Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
ms.Position = 0;
ser.Deserialize(ms);
}
watch.Stop();
Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
}
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
using (var ms = new MemoryStream())
{
var ser = new XmlSerializer(typeof(Game));
Console.WriteLine();
Console.WriteLine(ser.GetType().Name);
ser.Serialize(ms, orig);
Console.WriteLine("Length: " + ms.Length);
ms.Position = 0;
ser.Deserialize(ms);
var watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
ms.Position = 0;
ms.SetLength(0);
ser.Serialize(ms, orig);
}
watch.Stop();
Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
ms.Position = 0;
ser.Deserialize(ms);
}
watch.Stop();
Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
}
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
using (var ms = new MemoryStream())
{
var ser = new DataContractSerializer(typeof(Game));
Console.WriteLine();
Console.WriteLine(ser.GetType().Name);
ser.WriteObject(ms, orig);
//.........这里部分代码省略.........
示例10: ValidateSerialization
private static JsonValue ValidateSerialization(JsonValue beforeSerialization)
{
Assert.NotNull(beforeSerialization);
NetDataContractSerializer serializer = new NetDataContractSerializer();
using (MemoryStream memStream = new MemoryStream())
{
serializer.Serialize(memStream, beforeSerialization);
memStream.Position = 0;
JsonValue afterDeserialization = (JsonValue)serializer.Deserialize(memStream);
Assert.Equal(beforeSerialization.ToString(), afterDeserialization.ToString());
return afterDeserialization;
}
}
示例11: SerializeDeserialize
SignedHeaderResponse SerializeDeserialize(SignedHeaderResponse authResponse)
{
var serializer = new NetDataContractSerializer();
var stream = new MemoryStream();
serializer.Serialize(stream, authResponse);
stream.Position = 0;
var result = (SignedHeaderResponse)serializer.Deserialize(stream);
return result;
}