C# 클래스의 프로퍼티 List 가져오기 및 해당 프로퍼티 값 설정, 가져오기 (Get Property LIsts, Get Method Lists)
Web 2011. 8. 28. 19:22값을 지정하거나 가져올 수 있다.
1.네임스페이스 선언
using System.Reflection;
2.소스코드 (콘솔 어플리케이션)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StaticGetProPertyList();
StaticGetMethodList();
Console.WriteLine(Environment.NewLine);
InstanceAddress address = new InstanceAddress();
InstanceGetProPertyList(address);
}
public static void StaticGetProPertyList()
{
PropertyInfo[] propertyinfos ;
propertyinfos = typeof(StaticAddress).GetProperties(BindingFlags.Public | BindingFlags.Static);
Console.WriteLine("----- StaticAddress's Property List -----");
foreach (PropertyInfo info in propertyinfos)
{
Console.WriteLine(string.Format("Property Name: {0} type:{1}", info.Name, info.PropertyType.Name));
}
Console.WriteLine(Environment.NewLine);
StaticSetAge(20);
StaticGetAge();
StaticSetAge(0);
StaticGetAge();
}
public static void StaticGetMethodList()
{
MethodInfo[] methodinfos;
methodinfos = typeof(StaticAddress).GetMethods(BindingFlags.Public | BindingFlags.Static);
Console.WriteLine("----- StaticAddress's Method List -----");
foreach (MethodInfo info in methodinfos)
{
Console.WriteLine(string.Format("Method Name: {0} Return type:{1}", info.Name, info.ReturnType));
}
}
public static void StaticSetAge(int age)
{
PropertyInfo ageProperty = typeof(StaticAddress).GetProperty("age");
ageProperty.SetValue(null, age, null);
}
public static void StaticGetAge()
{
PropertyInfo ageProperty = typeof(StaticAddress).GetProperty("age");
Console.WriteLine("Age is: " + ageProperty.GetValue(null, null));
}
public static void InstanceGetProPertyList(InstanceAddress instance)
{
PropertyInfo[] propertyinfos;
propertyinfos =typeof(InstanceAddress).GetProperties(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("----- InstanceAddress's Property List -----");
foreach (PropertyInfo info in propertyinfos)
{
Console.WriteLine(string.Format("Property Name: {0} type:{1}", info.Name, info.PropertyType.Name));
}
Console.WriteLine(Environment.NewLine);
InstanceSetAge(50, instance);
InstanceGetAge(instance);
InstanceSetAge(0, instance);
InstanceGetAge(instance);
}
public static void InstanceGetMethodList(InstanceAddress instance)
{
MethodInfo[] methodinfos;
methodinfos = typeof(InstanceAddress).GetMethods(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("----- InstanceAddress's Method List -----");
foreach (MethodInfo info in methodinfos)
{
Console.WriteLine(string.Format("Method Name: {0} Return type:{1}", info.Name, info.ReturnType));
}
Console.WriteLine(Environment.NewLine);
}
public static void InstanceSetAge(int age,InstanceAddress instance)
{
PropertyInfo ageProperty = instance.GetType().GetProperty("instanceage");
ageProperty.SetValue(instance, age, null);
}
public static void InstanceGetAge(InstanceAddress instance)
{
PropertyInfo ageProperty = instance.GetType().GetProperty("instanceage");
Console.WriteLine("Age is: " + ageProperty.GetValue(instance, null));
}
}
public class StaticAddress
{
private static int _age;
public static string name { get; set; }
public static string address { get; set; }
public static int age
{ get
{
return _age;
}
set{
if(value<= 0)
{
Console.WriteLine("Input Error. Set Default value is 10");
_age = 10;
return;
}
_age = value;
}
}
public static string GetFullName()
{
return string.Format("name: {0}, address: {1}, age: {2}", name, address, age);
}
}
public class InstanceAddress
{
private int _instanceAge;
public string instancename { get; set; }
public string instanceaddress { get; set; }
public int instanceage
{
get
{
return _instanceAge;
}
set
{
if (value <= 0)
{
Console.WriteLine("Input Error. Set Default value is 10");
_instanceAge = 10;
return;
}
_instanceAge = value;
}
}
public string instanceGetFullName()
{
return string.Format("name: {0}, address: {1}, age: {2}", instancename, instanceaddress, instanceage);
}
}
}
Static Class 클래스의 경우는 SetValue나 GetValue에서 첫번째 파라메터인 object가 null로 들어간다. (#58, #65 라인)
Instance된 클래스의 경우는 SetValue나 GetValue에서 첫번째 마라메터에는 인스턴스된 객체가 들어간다. (#113, #120 라인)
3. 실행 결과