gpt4 book ai didi

c# - ExpandoObject 便利工厂?

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:42 24 4
gpt4 key购买 nike

ExpandoObject 是否有方便的工厂方法?有点像,我不知道,

dynamic disney = new ExpandoObject("First", "Donald", "Last", "Duck");

最佳答案

不,没有,但是你可以在几分钟内写出来。 :)给你:

C#

class Program
{
static void Main(string[] args)
{
dynamic ex = ExpandoFactory.Create("First", "Donald", "Last", "Duck");
Console.WriteLine(ex.First);
Console.WriteLine(ex.Last);
}
}

static class ExpandoFactory
{
internal static ExpandoObject Create(params string[] items)
{
//safety checks omitted for brevity
IDictionary<string, object> result = new ExpandoObject();
for (int i = 0; i < items.Length; i+=2)
{
result[items[i]] = items[i + 1];
}
return result as ExpandoObject;
}
}

当然,你应该事先检查数组的基数。我希望这会有所帮助。

关于c# - ExpandoObject 便利工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36863010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com