Commit dbf60efb authored by 阿星Plus's avatar 阿星Plus

序列化扩展

parent b48125db
using Newtonsoft.Json;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;
/// <summary>
/// Extensions
/// </summary>
public static class Extensions
{
public static string SerializeBinary<T>(this T @this)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, @this);
return Encoding.Default.GetString(memoryStream.ToArray());
}
}
public static string SerializeBinary<T>(this T @this, Encoding encoding)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, @this);
return encoding.GetString(memoryStream.ToArray());
}
}
public static string SerializeToJson(this object input)
{
return JsonConvert.SerializeObject(input);
}
public static string SerializeXml(this object @this)
{
XmlSerializer xmlSerializer = new XmlSerializer(@this.GetType());
using (StringWriter stringWriter = new StringWriter())
{
xmlSerializer.Serialize(stringWriter, @this);
using (StringReader stringReader = new StringReader(stringWriter.GetStringBuilder().ToString()))
{
return stringReader.ReadToEnd();
}
}
}
public static T DeserializeBinary<T>(this string @this)
{
using (MemoryStream serializationStream = new MemoryStream(Encoding.Default.GetBytes(@this)))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
return (T)binaryFormatter.Deserialize(serializationStream);
}
}
public static T DeserializeBinary<T>(this string @this, Encoding encoding)
{
using (MemoryStream serializationStream = new MemoryStream(encoding.GetBytes(@this)))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
return (T)binaryFormatter.Deserialize(serializationStream);
}
}
public static T DeserializeFromJson<T>(this string input)
{
return JsonConvert.DeserializeObject<T>(input);
}
public static T DeserializeFromJson<T>(this object input)
{
return JsonConvert.DeserializeObject<T>(input.ToString());
}
public static T DeserializeXml<T>(this string @this)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader textReader = new StringReader(@this);
return (T)xmlSerializer.Deserialize(textReader);
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
......@@ -17,19 +17,21 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution-items", "solution-
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus", "src\Plus\Plus.csproj", "{7C852EAF-CCB3-48D1-9F11-E8606C3D4A2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.EntityFramework", "src\Plus.EntityFramework\Plus.EntityFramework.csproj", "{6B20C811-655C-41DF-A97B-CA40A69B72AB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.EntityFramework", "src\Plus.EntityFramework\Plus.EntityFramework.csproj", "{6B20C811-655C-41DF-A97B-CA40A69B72AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.AutoMapper", "src\Plus.AutoMapper\Plus.AutoMapper.csproj", "{FA94B550-907C-4EAD-B668-BB9549FBE835}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.AutoMapper", "src\Plus.AutoMapper\Plus.AutoMapper.csproj", "{FA94B550-907C-4EAD-B668-BB9549FBE835}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Extensions", "src\Plus.Extensions\Plus.Extensions.csproj", "{57C2FEB2-A915-47CE-81CE-C1399C392C3B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Extensions", "src\Plus.Extensions\Plus.Extensions.csproj", "{57C2FEB2-A915-47CE-81CE-C1399C392C3B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Log4Net", "src\Plus.Log4Net\Plus.Log4Net.csproj", "{F4631747-89D2-4EA8-8684-9217D463D4C6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Log4Net", "src\Plus.Log4Net\Plus.Log4Net.csproj", "{F4631747-89D2-4EA8-8684-9217D463D4C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.MongoDB", "src\Plus.MongoDB\Plus.MongoDB.csproj", "{289760B5-2E69-4D9F-8F1F-2D80EA1FC36D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.MongoDB", "src\Plus.MongoDB\Plus.MongoDB.csproj", "{289760B5-2E69-4D9F-8F1F-2D80EA1FC36D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.RedisCache", "src\Plus.RedisCache\Plus.RedisCache.csproj", "{BBCB7D18-8BA5-461A-B3B2-3E9A79EDA6FB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.RedisCache", "src\Plus.RedisCache\Plus.RedisCache.csproj", "{BBCB7D18-8BA5-461A-B3B2-3E9A79EDA6FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Dapper", "src\Plus.Dapper\Plus.Dapper.csproj", "{33696E1A-23DA-4FF2-9DD9-2FA8C6822606}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plus.Dapper", "src\Plus.Dapper\Plus.Dapper.csproj", "{33696E1A-23DA-4FF2-9DD9-2FA8C6822606}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plus.Extensions.Serialization", "Plus.Extensions.Serialization\Plus.Extensions.Serialization.csproj", "{209BF4EB-311F-45AD-AED6-F314A49D83C7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -69,6 +71,10 @@ Global
{33696E1A-23DA-4FF2-9DD9-2FA8C6822606}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33696E1A-23DA-4FF2-9DD9-2FA8C6822606}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33696E1A-23DA-4FF2-9DD9-2FA8C6822606}.Release|Any CPU.Build.0 = Release|Any CPU
{209BF4EB-311F-45AD-AED6-F314A49D83C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{209BF4EB-311F-45AD-AED6-F314A49D83C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{209BF4EB-311F-45AD-AED6-F314A49D83C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{209BF4EB-311F-45AD-AED6-F314A49D83C7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -82,6 +88,7 @@ Global
{289760B5-2E69-4D9F-8F1F-2D80EA1FC36D} = {CC63505F-AC28-4DCE-93F6-82062FA5EDC3}
{BBCB7D18-8BA5-461A-B3B2-3E9A79EDA6FB} = {CC63505F-AC28-4DCE-93F6-82062FA5EDC3}
{33696E1A-23DA-4FF2-9DD9-2FA8C6822606} = {CC63505F-AC28-4DCE-93F6-82062FA5EDC3}
{209BF4EB-311F-45AD-AED6-F314A49D83C7} = {CC63505F-AC28-4DCE-93F6-82062FA5EDC3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {44529F52-F5EE-4330-BB68-E09EB8B967E3}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment