开发者

Dynamically loading Dictionary KVPs into Anonymous types

开发者 https://www.devze.com 2023-04-06 14:52 出处:网络
I want to load all the KeyValuePairs in a Dictiona开发者_运维知识库ry<string, string> into anonymous types during runtime. There will be no knowledge ahead of time of the contents of the Diction

I want to load all the KeyValuePairs in a Dictiona开发者_运维知识库ry<string, string> into anonymous types during runtime. There will be no knowledge ahead of time of the contents of the Dictionary.

Dictionary<string, string> callParams = new Dictionary<string, string>();

logger.WithParams(() => new { Val1= val1, Val2= val2, Val3= val3 });

How can I add the KVPs of callParams to the set of anonymous types?


It's not really clear what you mean - but if you're trying to get the property names to depend on the keys in the dictionary, you're out of luck: anonymous types are created for you by the compiler at compile-time, with the property names that you specify. If you don't know the keys until execution time, you can't have the anonymous types...

If you're using .NET 4 you could potentially use ExpandoObject and dynamic typing... but what's the bigger picture here? Why can't you just pass the dictionary around as it is?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号