gpt4 book ai didi

c# 从字符串创建一个对象的实例

转载 作者:行者123 更新时间:2023-11-30 13:42:46 32 4
gpt4 key购买 nike

我有一个字符串变量包含:

string classCode = "public class Person { public string Name{get;set;} }";

如何从 classCode 创建对象的实例?喜欢

object obj = CreateAnInstanceAnObject(classCode);

最佳答案

您需要使用 CodeDom 编译内存中程序集,然后使用反射创建类型。

这是一个 sample article on MSDN that walks through the process of code generation .

编译代码后,您可以使用 Activator.CreateInstance创建它的一个实例。

关于c# 从字符串创建一个对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2022301/

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