// C#
class MyClass
{
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
SetDefaultName(myClass);
Console.WriteLine("MyClass Name : " + myClass.Name);
}
static void SetDefaultName(Object myObject)
{
// Name이라는 Property가 있는지?
// 없으면 null
// method를 확인하고 싶다면 MethodInfo
// field를 확인하고 싶다면 FieldInfo
PropertyInfo propertyInfo = myObject.GetType().GetProperty("Name");
// Name이라는 Property가 있으면 값 설정
if (propertyInfo != null)
{
// SetValue 요약:
// 지정된 개체의 속성 값을 설정합니다.
//
// 매개 변수:
// obj:
// 속성 값이 설정될 개체입니다.
//
// value:
// 새 속성 값입니다.
propertyInfo.SetValue(myObject, "Kim");
// 메소드는 Invoke함수 사용
}
}
}
Output:
MyClass Name : Kim
// C#
class Class1
{
public void Run()
{
// 네임스페이스.클래스명
// Type을 얻어옴
Type customerType = Type.GetType("MyNamespace.Customer");
// Type으로부터 클래스 객체 생성
// 기존에는 new로 instance를 생성했었다면 Reflection할 때는 Activator로 Instance 생성
Object obj = Activator.CreateInstance(customerType);
// obj를 Customer로 캐스팅 후 Name에 접근
string name = ((Customer)obj).Name;
Console.WriteLine(name);
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public Customer()
{
this.Name = "None";
}
}
// C#
public class AttributesEX
{
// Attributes
[Obsolete("이 메소드는 더 이상 사용되지 않습니다.")]
public static int Add(int a, int b)
{
return (a + b);
}
}
// Attribute를 상속받음
class DeveloperAttribute : Attribute
{
private string name;
private string level;
private bool reviewed;
public DeveloperAttribute(string name, string level)
{
this.name = name;
this.level = level;
this.reviewed = false;
}
public virtual string Name
{
get { return name; }
}
public virtual string Level
{
get { return level; }
}
public virtual bool Reviewed
{
get { return reviewed; }
set { reviewed = value; }
}
}
[Developer("Kang", "20", Reviewed = true)]
class Program
{
static void Main(string[] args)
{
// Program class의 타입을 받음
GetAttribute(typeof(Program));
}
public static void GetAttribute(Type t)
{
// GetCustomAttribute(MemberInfo element, Type attributeType)
// 요약:
// 형식의 멤버에 적용 된 사용자 지정 특성을 검색 합니다. 매개 변수를 검색할 사용자 지정 특성의 유형 및 멤버를 지정 합니다.
//
// 매개 변수:
// element:
// 파생 된 개체는 System.Reflection.MemberInfo 클래스의 생성자, 이벤트, 필드, 메서드 또는 속성 멤버를 설명 하는
// 클래스입니다.
//
// attributeType:
// 형식 또는 검색할 사용자 지정 특성의 기본 형식입니다.
//
// 반환 값:
// 형식의 단일 사용자 지정 특성에 대 한 참조를 attributeType 에 적용 되는 element, 또는 null 이러한 특성이 없는 경우.
DeveloperAttribute developerAttribute =
(DeveloperAttribute)Attribute.GetCustomAttribute(t, typeof(DeveloperAttribute));
if(developerAttribute == null)
{
Console.WriteLine("해당 Attribute를 찾을 수 없습니다.");
return;
}
Console.WriteLine("name of developerAttribute : " + developerAttribute.Name);
Console.WriteLine("Level of developerAttribute : " + developerAttribute.Level);
Console.WriteLine("Reviewed of developerAttribute : " + developerAttribute.Reviewed);
}
}
// C#
class Important : Attribute
{
string message;
public Important(string message)
{
this.message = message;
}
}
class Monster
{
[Important("very important")]
public int hp;
protected int attck;
private float speed;
}
class Program
{
static void Main(string[] args)
{
Monster monster = new Monster();
Type type = monster.GetType();
// public으로 되어있는 hp만 받아옴
FieldInfo[] fields = type.GetFields();
var attribute = fields[0].GetCustomAttributes();
}
}