Net6 ConfigurationBinder configuration转为对象(未完成)

复制代码
绑定集合

var source = new Dictionary<string, string>
{
["0:gender"] = "Male",
//["0:age"] = "18",
//["0:contactInfo:emailAddress"] = "foo@outlook.com",
//["0:contactInfo:phoneNo"] = "123",

//["1:gender"] = "Male",
//["1:age"] = "25",
//["1:contactInfo:emailAddress"] = "bar@outlook.com",
//["1:contactInfo:phoneNo"] = "456",

//["2:gender"] = "Female",
//["2:age"] = "36",
//["2:contactInfo:emailAddress"] = "baz@outlook.com",
//["2:contactInfo:phoneNo"] = "789"
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(source)
.Build();

var list = configuration.Get<IList<Profile>>();
复制代码

 

此处在configuration 存储为字典后

到ConfigurationBinder 里查找出值

在下面get哪里执行到ConfigurationProvider的GetChildKeys(IEnumerable<string> earlierKeys, string parentPath)
最开始parentPath 为null ,则遍历对象里的data,并存储到集合里并通过 Segment 获取值前面的index 0 或1
获取到的键为0(Segment 中用 : 做分割判断符号,这也是一个常量 )

 

 

 

然后添加早些时候的key(这里还不太明白)
排序返回,然后到 root.GetSection() 在ConfigurationSection
通过 _root = root;
_path = path;
根据键获取值 和path
获取 Key ,Path,Value

获取到数据


然后返回的binder,FindOpenGenericInterface()判断是否字典还是集合
通过字典或者集合绑定数据
如果不是字典,集合,则绑定属性
以绑定集合为例

 

在 BindCollection里获取 集合里的参数  Profile 这个对象,后续用这个对象传入BindInstance 里去创建

在获取config.GetChildren() 并遍历
到BindInstance里去绑定
获取 IConfigurationSection下的值
为null在去GetChildKeys 去获取,用前面获取到的index
这是获取到的键为gender.
然后到 root.GetSection() 在ConfigurationSection
根据键获取值 和path
然后
bindingPoint.SetValue(CreateInstance(type, config, options))
CreateInstance 创建泛型里那个参数的对象

在通过BindProperty 通过 绑定属性值和对象
然后又是 根据键去获取值在绑定


并返回

 

posted @   孤海飞雁  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示