c# - 如何 Activator.CreateInstance 一个没有构造函数的类型?

标签 c#

例如:

class TestType
{
   public int a;
   public int b;
}

TestType obj = Activator.CreateInstance(typeof(TestType), 1, 2) as TestType;

那么 obj.a==1obj.b==2 呢?有人知道如何解决我的问题吗?

最佳答案

不可能,试试吧

TestType obj = Activator.CreateInstance(typeof(TestType)) as TestType;
obj.a = 1;
obj.b = 2;

关于c# - 如何 Activator.CreateInstance 一个没有构造函数的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54006665/

相关文章:

c# - 如果类型参数是结构或类,则选择泛型实现

c# - 瓦廷评估问题

c# - Visual Studio "0 of 4 errors"

c# - 在通过网络发送之前压缩位图

c# - 在控制台中捕获没有焦点的击键

c# - 如何将 PDF 保存为只读/扁平化?

c# - 如何初始化反射数据的线程安全静态缓存

c# - 有没有办法检查 MSMQ 队列中有多少消息?

c# - Atata - 如何使用 Atata 框架切换到新的浏览器窗口?

c# - Live HighChart 不显示数据