Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Configuration .NET核心配置序列化_Configuration_Asp.net Core_.net Core - Fatal编程技术网

Configuration .NET核心配置序列化

Configuration .NET核心配置序列化,configuration,asp.net-core,.net-core,Configuration,Asp.net Core,.net Core,有没有一种方法可以序列化一个对象,这样它就可以被.Net核心配置绑定器重新水化 基本上,我想通过这个测试: [Test] public void Can_Serialize_And_Rehydrate() { var foo = new Foo{ Prop1 = 42; Prop2 = "Test" } Dictionary<string, string> serialized = Serialize(Foo); var deserializedFoo = ne

有没有一种方法可以序列化一个对象,这样它就可以被.Net核心配置绑定器重新水化

基本上,我想通过这个测试:

[Test]
public void Can_Serialize_And_Rehydrate()
{
   var foo = new Foo{ Prop1 = 42; Prop2 = "Test" }

   Dictionary<string, string> serialized = Serialize(Foo);

   var deserializedFoo = new Foo();

    new ConfigurationBuilder()
        .AddInMemoryCollection(serialized)
        .Build()
        .Bind(deserializedFoo);

   Assert.AreEqual(deserializedFoo.Prop1, 42);
   Assert.AreEqual(deserializedFoo.Prop2, "Test");
}
[测试]
public void可以序列化和再水化()
{
var foo=new foo{Prop1=42;Prop2=“Test”}
字典序列化=序列化(Foo);
var deserializedFoo=new Foo();
新的ConfigurationBuilder()
.AddInMemoryCollection(序列化)
.Build()
.Bind(反序列化的foo);
AreEqual(反序列化的foo.Prop1,42);
AreEqual(反序列化的foo.Prop2,“Test”);
}

是否有现成的序列化程序,或者我需要编写自己的
Serialize()
方法?

AddInMemoryCollection
的签名如下所示,那么为什么要在此处序列化词典?你可以照原样使用它

public static IConfigurationBuilder AddInMemoryCollection(
            this IConfigurationBuilder configurationBuilder,
            IEnumerable<KeyValuePair<string, string>> initialData)
公共静态IConfigurationBuilder AddInMemoryCollection(
此IConfigurationBuilder配置生成器,
IEnumerable(可数初始值数据)
如果您想了解更多有关如何测试自定义配置的信息,我建议您查看以下内容:

我通过“劫持”一个
JSONConfiguration Provider
并将序列化的Json直接插入其中,实现了这一点。不确定这是否是最好的方法,但确实有效:

public class ConfigurationSerializer 
{
    private class CustomJsonProvider : JsonConfigurationProvider
    {
        public CustomJsonProvider() : base(new JsonConfigurationSource())
        {
        }

        public IDictionary<string, string> GetData(Stream s)
        {
            Load(s);
            // Return the Configuration Dictionary
            return Data;
        }
    }

    public Dictionary<string, string> Serialize(object o)
    {
        var serialized = 
            JsonConvert.SerializeObject(
                o,
                new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});

        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized)))
        {
            var jsonProvider = new CustomJsonProvider();

            return jsonProvider
                .GetData(ms)
                .ToDictionary(key => key.Key, value => value.Value);
        }
    }
}
公共类配置序列化程序
{
私有类CustomJsonProvider:JSONConfiguration Provider
{
public CustomJsonProvider():基(新JsonConfigurationSource())
{
}
公共IDictionary GetData(流)
{
装载量;
//返回配置字典
返回数据;
}
}
公共字典序列化(对象o)
{
变量序列化=
JsonConvert.SerializeObject(
啊,,
新的JsonSerializerSettings{NullValueHandling=NullValueHandling.Ignore});
使用(var ms=new MemoryStream(Encoding.UTF8.GetBytes(序列化)))
{
var jsonProvider=new CustomJsonProvider();
返回jsonProvider
.GetData(毫秒)
.ToDictionary(key=>key.key,value=>value.value);
}
}
}

您是否看过文档,特别是关于使用选项和配置对象的部分@JoeAudette-我看过文档,是的。我没有看到任何关于将对象转换为配置的内容(只有相反的方式),从我在源代码中看到的测试来看,我也没有看到任何“序列化器”。是的,我想做的有点非正统。“为什么tyring不在这里序列化你的字典”-我不是想序列化字典,我是想序列化
Foo
到字典中。是的,这有点非正统:)。我正在尝试使用ConfigurationBuilder对消息传递系统中的项进行序列化/反序列化,因为ConfigKey结构在构建类型架构时起到了很好的作用。