리플렉션 개체는 런타임에 형식 정보를 얻는 데 사용됩니다. 실행 중인 프로그램의 메타데이터에 대한 액세스를 제공하는 클래스는 System.Reflection 네임스페이스에 있습니다.
시스템의 MemberInfo 개체입니다. 리플렉션 클래스는 클래스와 관련된 속성을 검색하기 위해 초기화되어야 합니다.
아래 예에서 우리는 대상 클래스의 개체를 설정했습니다 -
System.Reflection.MemberInfo info = typeof(MyClass);
다음은 예입니다 -
예시
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : System.Attribute {
public readonly string Url;
public string Topic { // Topic is a named parameter
get {
return topic;
}
set {
topic = value;
}
}
public HelpAttribute(string url) { // url is a positional parameter
this.Url = url;
}
private string topic;
}
[HelpAttribute("Information on the class MyClass")]
class MyClass {
}
namespace AttributeAppl {
class Program {
static void Main(string[] args) {
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);
for (int i = 0; i < attributes.Length; i++) {
System.Console.WriteLine(attributes[i]);
}
Console.ReadKey();
}
}
}