本文整理汇总了C#中Formatter.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# Formatter.Serialize方法的具体用法?C# Formatter.Serialize怎么用?C# Formatter.Serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Formatter
的用法示例。
在下文中一共展示了Formatter.Serialize方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Serialize_TypeWithoutNull_Throws
public void Serialize_TypeWithoutNull_Throws()
{
var subject = new Formatter(
_binarySerializerMock.Object,
_unityJsonSerializerMock.Object,
_formatSelectorMock.Object);
var type = new TypeWithoutAssemblyQualifiedName();
// ReSharper disable once AssignNullToNotNullAttribute
Assert.Throws<ArgumentException>(() => subject.Serialize(type, new object()));
}
示例2: Serialize_TypeNull_Throws
public void Serialize_TypeNull_Throws()
{
var subject = new Formatter(
_binarySerializerMock.Object,
_unityJsonSerializerMock.Object,
_formatSelectorMock.Object);
// ReSharper disable once AssignNullToNotNullAttribute
Assert.Throws<ArgumentNullException>(() => subject.Serialize(null, new object()));
}
示例3: Serialize_SerializationFormatNull_NullFormatted
public void Serialize_SerializationFormatNull_NullFormatted()
{
var subject = new Formatter(
_binarySerializerMock.Object,
_unityJsonSerializerMock.Object,
_formatSelectorMock.Object);
_formatSelectorMock.Setup(mock => mock.GetFormat(It.IsAny<Type>(), It.IsAny<object>()))
.Returns(SerializationFormat.Null);
var result = subject.Serialize(typeof(TestData), null);
Assert.IsNotNull(result);
Assert.AreEqual(typeof(TestData).AssemblyQualifiedName, result.AssemblyQualifiedName);
Assert.AreEqual(SerializationFormat.Null, result.Format);
}
示例4: Serialize_NotSupportedFormat_Throws
public void Serialize_NotSupportedFormat_Throws()
{
var subject = new Formatter(
_binarySerializerMock.Object,
_unityJsonSerializerMock.Object,
_formatSelectorMock.Object);
_formatSelectorMock.Setup(mock => mock.GetFormat(It.IsAny<Type>(), It.IsAny<object>()))
.Returns((SerializationFormat)int.MaxValue);
Assert.Throws<NotSupportedException>(() => subject.Serialize(typeof(TestData), new TestData()));
}
示例5: SerializeGeneric_SerializationFormatJson_JsonFormatted
public void SerializeGeneric_SerializationFormatJson_JsonFormatted()
{
var subject = new Formatter(
_binarySerializerMock.Object,
_unityJsonSerializerMock.Object,
_formatSelectorMock.Object);
const string serializedData = "12345";
_formatSelectorMock.Setup(mock => mock.GetFormat(It.IsAny<Type>(), It.IsAny<object>()))
.Returns(SerializationFormat.UnityJson);
_unityJsonSerializerMock.Setup(mock => mock.Serialize(It.IsAny<object>()))
.Returns(serializedData);
var result = subject.Serialize<TestData>(null);
Assert.IsNotNull(result);
Assert.AreEqual(typeof(TestData).AssemblyQualifiedName, result.AssemblyQualifiedName);
Assert.AreEqual(SerializationFormat.UnityJson, result.Format);
Assert.AreEqual(serializedData, result.JsonData);
}
示例6: SerializeGeneric_SerializationFormatBinary_BinaryFormatted
public void SerializeGeneric_SerializationFormatBinary_BinaryFormatted()
{
var subject = new Formatter(
_binarySerializerMock.Object,
_unityJsonSerializerMock.Object,
_formatSelectorMock.Object);
var serializedData = new byte[] { 1, 2, 3, 42, 2 };
_formatSelectorMock.Setup(mock => mock.GetFormat(It.IsAny<Type>(), It.IsAny<object>()))
.Returns(SerializationFormat.BinaryFormatter);
_binarySerializerMock.Setup(mock => mock.Serialize(It.IsAny<object>()))
.Returns(serializedData);
var result = subject.Serialize<TestData>(null);
Assert.IsNotNull(result);
Assert.AreEqual(typeof(TestData).AssemblyQualifiedName, result.AssemblyQualifiedName);
Assert.AreEqual(SerializationFormat.BinaryFormatter, result.Format);
Assert.AreEqual(serializedData, result.BinaryData);
}