Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用简单对象对复杂C#对象进行自定义序列化_C#_.net_Mongodb_Serialization_Mongodb .net Driver - Fatal编程技术网

使用简单对象对复杂C#对象进行自定义序列化

使用简单对象对复杂C#对象进行自定义序列化,c#,.net,mongodb,serialization,mongodb-.net-driver,C#,.net,Mongodb,Serialization,Mongodb .net Driver,我正在使用QuickGraph中一个名为AdjacencyGraph的实用程序类,该类具有复杂的内部结构 我不关心复杂类的所有内部属性,也不能将其更改为nuget包中的属性-在我的数据库中,我最多只关心存储一个可用于重建AdacencyGraph的依赖项对象数组: public class Dependency { public Dependency(string source, string target) { this.Source = source;

我正在使用QuickGraph中一个名为
AdjacencyGraph
的实用程序类,该类具有复杂的内部结构

我不关心复杂类的所有内部属性,也不能将其更改为nuget包中的属性-在我的数据库中,我最多只关心存储一个可用于重建AdacencyGraph的依赖项对象数组:

public class Dependency
{
    public Dependency(string source, string target)
    {
        this.Source = source;
        this.Target = target;
    }

    public string Source { get; set; }
    public string Target { get; set; }
}
关于序列化,我只需要执行以下代码:

void Serialize(Dependencies toSerialize)
{
    var toBeStored = toSerialize.GetAllDependencies();
    // write this to mongodb somehow
}
和反序列化生成依赖项对象:

Dependencies Deserialize(IEnumerable<Dependency> hydrateFrom)
{
    var dependencies = new Dependencies();

    foreach(var d in hydrateFrom)
    {
        dependencies.SetRequiresFor(d.Source, d.Target);
    }
}
依赖项反序列化(IEnumerable-eFrom)
{
var依赖项=新依赖项();
foreach(水化物中的变量d)
{
dependencies.SetRequiresFor(d.Source,d.Target);
}
}
问题

我将如何拦截序列化过程和输出

其他信息

我已经将邻接图包装在一个类中,该类列出了所有依赖对象,并且也可以接受依赖对象的列表

class Dependencies
{
    private AdjacencyGraph<string, Edge<string>> relationshipGraph = new AdjacencyGraph<string, Edge<string>>();

    public void SetRequiresFor(SkuId on, SkuId requires)
    {
        var toAdd = new Edge<string>(on.Value, requires.Value);
        this.relationshipGraph.AddVerticesAndEdge(toAdd);

    }

    public IEnumerable<Dependency> GetAllDependencies()
    {
        foreach(var edge in this.relationshipGraph.Edges)
        {
            yield return new Dependency(edge.Source, edge.Target);
        }
    }
}
类依赖关系
{
私有邻接图关系图=新邻接图();
public void SetRequiresFor(SkuId打开,SkuId需要)
{
var toAdd=新边(on.Value,requires.Value);
此.relationshipGraph.AddVerticesAndEdge(toAdd);
}
公共IEnumerable GetAllDependencies()
{
foreach(此.relationshipGraph.Edges中的var edge)
{
返回新的依赖项(edge.Source、edge.Target);
}
}
}

您需要做三件事:

  • 实施
    b串行化器
  • 实现一个
    BsonSerializationProvider
    ,它将
    依赖项
    指向序列化程序*
  • 在应用程序的初始化中注册提供程序
  • BsonSerializer BsonSerializationProvider 登记
    *您可以使用
    BsonSerializer.RegisterSerializer
    剪切提供程序并直接注册序列化程序,但我仍然建议将所有序列化程序分组到一个提供程序中

    public class DependenciesBsonSerializer : BsonBaseSerializer
    {
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            // implement using bsonWriter
        }
    
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            // implement using bsonReader
        }
    }
    
    public sealed class BsonSerializationProvider : IBsonSerializationProvider
    {
        public IBsonSerializer GetSerializer(Type type)
        {
            if (type == typeof(Dependncies)
            {
                return new DependenciesBsonSerializer ();
            }
    
            return null;
        }
    }
    
    BsonSerializer.RegisterSerializationProvider(new BsonSerializationProvider());