namespace ConsoleApp2
{
class Profile
{
private string name;
private string phone;
public Profile()
{
name = ""; phone = "";
}
public Profile(string name, string phone)
{
this.name = name;
this.phone = phone;
}
public void Print()
{
Console.WriteLine("Name : {0}, Phone : {1}", name, phone);
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Phone
{
get { return phone; }
set { phone = value; }
}
}
class Program
{
static void Main(string[] args)
{
Type type = Type.GetType("DynamicInstance.Profile");
MethodInfo methodInfo = type.GetMethod("Print");
PropertyInfo nameProperty = type.GetProperty("Name");
PropertyInfo phoneProperty = type.GetProperty("Phone");
object profile = Activator.CreateInstance(type, "박상현", "512-1234");
methodInfo.Invoke(profile, null);
profile = Activator.CreateInstance(type);
nameProperty.SetValue(profile, "박찬호", null);
phoneProperty.SetValue(profile, "1234-5678", null);
Console.WriteLine("Name : {0}, Phone : {1}", nameProperty.GetValue(profile, null), phoneProperty.GetValue(profile, null));
}
}
}
책에 나온 예제 그대로 따라했는데 개체 참조가 개체의 인스턴스로 설정되지 않았다고 뜸
틀린부분 있나 몇번 검토했는데 완전 똑같았음.. 그냥 책쓴사람이 실수한건가
그리고 생성자 저렇게 많이 넣은 이유도 알수 있을가요? name { get; set; } phone { get; set; }만 해도 되지않나
댓글 0