(C#) 37. Reflection (리플렉션)
Syntax (문법)
(C#) 37. Reflection (리플렉션)
Reflection () : Syntax ()
- 최초 작성일: 2021년 3월 21일(월)
##
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Reflection;
namespace reflection
{
class Program
{
class Important : System.Attribute
{
string message;
public Important(string message) { this.message = message; }
}
class Monster
{
// hp입니다. 중요한 정보입니다. // 일반적으로, 주석은 컴파일도 안됨.
[Important("Very Important")] // 컴퓨터가 인식할 수 있는 주석의 개념.
public int hp;
protected int attack;
private float speed;
void Attack() { }
}
static void Main(string[] args)
{
// Reflection : X-Ray
Monster monster = new Monster();
Type type = monster.GetType();
var fields = type.GetFields(System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
string access = "protected";
if (field.IsPublic)
access = "public";
else if (field.IsPrivate)
access = "private";
var attributes = field.GetCustomAttributes();
Console.WriteLine($"{access} {field.FieldType.Name} {field.Name}");
}
}
}
}
</br>
Result
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.
