【发布时间】:2019-06-23 11:06:33
【问题描述】:
我的代码抛出异常,我不知道为什么。也许有人可以帮助我。
例外:
System.MissingMethodException: '类型的构造函数 'System.Reflection.RuntimePropertyInfo' 未找到。'
我的代码:
class Program
{
static void Main(string[] args)
{
Test t1 = new Test();
Test1 t = new Test1();
typeof(Test1).GetProperty("Name").SetValue(t, "tetst");
// throws error:
var c = Activator.CreateInstance(typeof(Test).GetProperty("Test1test").GetType());
typeof(Test).GetProperty("Test1test").SetValue(c, t);
Console.Read();
}
}
public class Test
{
public Test()
{
Test1test = new Test1();
}
public int Id { get; set; }
public string Name { get; set; }
public Test1 Test1test { get; set; }
}
public class Test1
{
public Test1()
{ }
public int Id { get; set; }
public string Name { get; set; }
}
【问题讨论】:
-
你为什么不用
Activator.CreateInstance(typeof(Test1))?
标签: c# .net reflection