728x90
using System;
using System.Collections.Generic;
namespace Csharp
{
class Program
{
// Reflection : X-Ray
// 기능 : class가 가지고 있는 모든 정보들을 runtime(프로그램이 실행되는 도중에) 다 뜯어보고 분석을 할 수 있다.
class Monster
{
public int hp;
protected int attack;
private float speed;
void Attack() { }
}
static void Main(string[] args)
{
Monster monster = new Monster();
// C#에서 우리가 만드는 모든 객체들은 다 object 객체에서 파생되어 나오는 것. 그래서 다들 GetType()이란 것을 들고있다.
monster.GetType();
}
}
}
여기서 F12를 눌리면 자세한 정보가 나온다.
여기서 Ctrl + z 를 눌리면,
펼침 기능이 활성화 된다.
using System;
using System.Collections.Generic;
namespace Csharp
{
class Program
{
// Reflection : X-Ray
// 기능 : class가 가지고 있는 모든 정보들을 runtime(프로그램이 실행되는 도중에) 다 뜯어보고 분석을 할 수 있다.
class Monster
{
public int hp;
protected int attack;
private float speed;
void Attack() { }
}
static void Main(string[] args)
{
Monster monster = new Monster();
// C#에서 우리가 만드는 모든 객체들은 다 object 객체에서 파생되어 나오는 것. 그래서 다들 GetType()이란 것을 들고있다.
monster.GetType();
//GetType()을 이용하면, type을 빼올 수가 있다.
var type = monster.GetType();
}
}
}
var 위에 마우스를 올려보면 Type인 것을 알 수있다.
그러므로 이렇게 고친다.
using System;
using System.Collections.Generic;
namespace Csharp
{
class Program
{
// Reflection : X-Ray
// 기능 : class가 가지고 있는 모든 정보들을 runtime(프로그램이 실행되는 도중에) 다 뜯어보고 분석을 할 수 있다.
class Monster
{
public int hp;
protected int attack;
private float speed;
void Attack() { }
}
static void Main(string[] args)
{
Monster monster = new Monster();
// C#에서 우리가 만드는 모든 객체들은 다 object 객체에서 파생되어 나오는 것. 그래서 다들 GetType()이란 것을 들고있다.
monster.GetType();
//GetType()을 이용하면, type을 빼올 수가 있다.
Type type = monster.GetType();
// type을 빼온다는 것의 뜻은?
// ex) class Monster가 가지고 있는 hp, attack, speed 변수, 게다가 Attack() 함수까지 정보들을 다 빼올 수 있는 것
}
}
}
Bin까지 입력하면 나오는 긴 것을 선택한다.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Csharp
{
class Program
{
// Reflection : X-Ray
// 기능 : class가 가지고 있는 모든 정보들을 runtime(프로그램이 실행되는 도중에) 다 뜯어보고 분석을 할 수 있다.
class Monster
{
public int hp;
protected int attack;
private float speed;
void Attack() { }
}
static void Main(string[] args)
{
Monster monster = new Monster();
// C#에서 우리가 만드는 모든 객체들은 다 object 객체에서 파생되어 나오는 것. 그래서 다들 GetType()이란 것을 들고있다.
monster.GetType();
//GetType()을 이용하면, type을 빼올 수가 있다.
Type type = monster.GetType();
// type을 빼온다는 것의 뜻은?
// ex) class Monster가 가지고 있는 hp, attack, speed 변수, 게다가 Attack() 함수까지 정보들을 다 빼올 수 있는 것
//어떤 정보를 빼 올지 입력
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)
{
//protected 인지, private인지, public인지 확인해보도록 하자
string access = "protected";
if(field.IsPublic)
{
access = "public";
}
else if(field.IsPrivate)
{
access = "private";
}
//class Monster의 hp, attack, speed 변수들을 긁어오도록 해보자.
Console.WriteLine($" {access} {field.FieldType.Name} {field.Name} ");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Csharp
{
class Program
{
// Reflection : X-Ray
// 기능 : class가 가지고 있는 모든 정보들을 runtime(프로그램이 실행되는 도중에) 다 뜯어보고 분석을 할 수 있다.
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)
{
Monster monster = new Monster();
// C#에서 우리가 만드는 모든 객체들은 다 object 객체에서 파생되어 나오는 것. 그래서 다들 GetType()이란 것을 들고있다.
monster.GetType();
//GetType()을 이용하면, type을 빼올 수가 있다.
Type type = monster.GetType();
// type을 빼온다는 것의 뜻은?
// ex) class Monster가 가지고 있는 hp, attack, speed 변수, 게다가 Attack() 함수까지 정보들을 다 빼올 수 있는 것
//어떤 정보를 빼 올지 입력
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)
{
//protected 인지, private인지, public인지 확인해보도록 하자
string access = "protected";
if(field.IsPublic)
{
access = "public";
}
else if(field.IsPrivate)
{
access = "private";
}
//class Monster의 hp, attack, speed 변수들을 긁어오도록 해보자.
Console.WriteLine($" {access} {field.FieldType.Name} {field.Name} ");
var attributes = field.GetCustomAttributes();
}
}
}
}
728x90
'● > 섹션 7. 유용한 문법' 카테고리의 다른 글
09.Nullable(널러블) (0) | 2021.07.15 |
---|---|
07.Exception(예외처리) (0) | 2021.07.14 |
06.Lambda(람다) (0) | 2021.07.14 |
05.event(이벤트) (0) | 2021.07.14 |
04.델리게이트(delegate) (0) | 2021.07.14 |