2021-04-08 20:09:59 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Bson.IO;
|
|
|
|
|
using MongoDB.Bson.Serialization;
|
|
|
|
|
using MongoDB.Bson.Serialization.Serializers;
|
|
|
|
|
|
|
|
|
|
namespace ET
|
|
|
|
|
{
|
|
|
|
|
public class StructBsonSerialize<TValue> : StructSerializerBase<TValue> where TValue : struct
|
|
|
|
|
{
|
|
|
|
|
private readonly List<PropertyInfo> propertyInfo = new List<PropertyInfo>();
|
|
|
|
|
|
|
|
|
|
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
|
|
|
|
|
{
|
|
|
|
|
Type nominalType = args.NominalType;
|
|
|
|
|
var fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public);
|
|
|
|
|
var propsAll = nominalType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
|
|
|
|
|
|
|
|
|
propertyInfo.Clear();
|
2021-04-11 19:50:39 +08:00
|
|
|
|
foreach (PropertyInfo prop in propsAll)
|
2021-04-08 20:09:59 +08:00
|
|
|
|
{
|
|
|
|
|
if (prop.CanWrite)
|
|
|
|
|
{
|
|
|
|
|
if (!prop.Name.Equals("Item", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
propertyInfo.Add(prop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 19:50:39 +08:00
|
|
|
|
IBsonWriter bsonWriter = context.Writer;
|
2021-04-08 20:09:59 +08:00
|
|
|
|
|
|
|
|
|
bsonWriter.WriteStartDocument();
|
|
|
|
|
|
2021-04-11 19:50:39 +08:00
|
|
|
|
foreach (FieldInfo field in fields)
|
2021-04-08 20:09:59 +08:00
|
|
|
|
{
|
|
|
|
|
bsonWriter.WriteName(field.Name);
|
|
|
|
|
BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value));
|
|
|
|
|
}
|
2021-04-11 19:50:39 +08:00
|
|
|
|
foreach (PropertyInfo prop in propertyInfo)
|
2021-04-08 20:09:59 +08:00
|
|
|
|
{
|
|
|
|
|
bsonWriter.WriteName(prop.Name);
|
|
|
|
|
BsonSerializer.Serialize(bsonWriter, prop.PropertyType, prop.GetValue(value, null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bsonWriter.WriteEndDocument();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
|
|
|
|
|
{
|
|
|
|
|
//boxing is required for SetValue to work
|
2021-04-11 19:50:39 +08:00
|
|
|
|
object obj = (object)(new TValue());
|
|
|
|
|
Type actualType = args.NominalType;
|
|
|
|
|
IBsonReader bsonReader = context.Reader;
|
2021-04-08 20:09:59 +08:00
|
|
|
|
|
|
|
|
|
bsonReader.ReadStartDocument();
|
|
|
|
|
|
|
|
|
|
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
|
|
|
|
|
{
|
2021-04-11 19:50:39 +08:00
|
|
|
|
string name = bsonReader.ReadName(Utf8NameDecoder.Instance);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
|
2021-04-11 19:50:39 +08:00
|
|
|
|
FieldInfo field = actualType.GetField(name);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
if (field != null)
|
|
|
|
|
{
|
2021-04-11 19:50:39 +08:00
|
|
|
|
object value = BsonSerializer.Deserialize(bsonReader, field.FieldType);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
field.SetValue(obj, value);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 19:50:39 +08:00
|
|
|
|
PropertyInfo prop = actualType.GetProperty(name);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
if (prop != null)
|
|
|
|
|
{
|
2021-04-11 19:50:39 +08:00
|
|
|
|
object value = BsonSerializer.Deserialize(bsonReader, prop.PropertyType);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
prop.SetValue(obj, value, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bsonReader.ReadEndDocument();
|
|
|
|
|
|
|
|
|
|
return (TValue)obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|