gpt4 book ai didi

reflection - 反射(reflection) ExpandoObject

转载 作者:行者123 更新时间:2023-12-04 15:49:00 29 4
gpt4 key购买 nike

我写了一个漂亮的函数,它可以接受 system.object ,反射(reflect)其属性并将对象序列化为 JSON 字符串。它看起来像这样:

public class JSONSerializer
{

public string Serialize(object obj)

现在,我希望能够执行此操作来序列化动态/ExpandoObject,但是由于我的序列化程序使用反射,因此无法执行此操作。解决方法是什么?
public class Test
{
public dynamic MakeDynamicCat()
{
dynamic newCat = new ExpandoObject();
newCat.Name = "Polly";
newCat.Pedigree = new ExpandoObject();
newCat.Pedigree.Breed = "Whatever";

return newCat;
}

public void SerializeCat()
{
new JSONSerializer().Serialize(MakeDynamicCat());
}
}

最佳答案

我认为,这个问题非常相似:How do I reflect over the members of dynamic object?

至少答案也应该对您有所帮助。

关于reflection - 反射(reflection) ExpandoObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3004965/

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