Tag: CSharp/メタプログラミング
typeof(クラス)で取得できる。
typeof(Window)
objectクラスのインスタンスメソッドGetType()で取得できる
Type type = obj.GetType()
Activator.CreateInstance(type)で可能。
Object o = Activator.CreateInstance(t);
public class Person { public string Name { get; set; } } [TestClass] public class PropertyInfoTest { [TestMethod] public void TestSetGet() { Person person = new Person() { Name = "中田英寿" }; Assert.AreEqual("中田英寿", person.Name); Type type = typeof(Person); PropertyInfo pi = type.GetProperty("Name"); pi.SetValue(person, "中村俊輔", null); Assert.AreEqual("中村俊輔", pi.GetValue(person, null)); Assert.AreEqual("中村俊輔", person.Name); } }