리플렉션 예제

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Reflection;
 
namespace EX12_Reflection
{
    class General
    {
        public int exp;
 
        public string Name { get; set; }        
        public int Power { get; set; }
        public int HP { get; set; }
        
        public General() {
            exp = 0;
        }
        
        public General(string name, int power, int hp) : this()
        {
            Name = name;
            Power = power;
            HP = hp;
        }
 
        public void Print() {
            Console.WriteLine("장수명 : {0} POW: {1} HP : {2} Exp : {3}",
                              Name, Power, HP, exp);
        }
    }
 
    class MainClass
    {
        public static void Main (string[] args)
        {
            Type type = Type.GetType("EX12_Reflection.General");
 
            // Query Class Info
            Console.WriteLine("***** Field List *****");
            FieldInfo[] fList = type.GetFields();
            foreach (FieldInfo f in fList) {
                Console.WriteLine(f);
            }
 
            Console.WriteLine("***** Property List *****");
            PropertyInfo[] pList = type.GetProperties ();
            foreach (PropertyInfo p in pList) {
                Console.WriteLine(p);
            }
 
            Console.WriteLine("***** Method List *****");
            MethodInfo[] mList = type.GetMethods ();
            foreach (MethodInfo m in mList) {
                Console.WriteLine(m);
            }
 
 
            // Generate New Instance
            Console.WriteLine("***** Generate New Instance *****");
            MethodInfo mdInfo = type.GetMethod("Print");
            PropertyInfo nameProp = type.GetProperty("Name");
            PropertyInfo powerProp = type.GetProperty("Power");
            PropertyInfo hpProp = type.GetProperty("HP");
            
            object general = Activator.CreateInstance(type, "관우,"99100);
            mdInfo.Invoke(general, null);
            
            general = Activator.CreateInstance(type);
            nameProp.SetValue(general,"여포",null);
            powerProp.SetValue(general, 100null);
            hpProp.SetValue(general, 100null);
            mdInfo.Invoke(general, null);
        }
    }
}
 
cs


람다식 예제

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
using System;
using System.Linq;
 
namespace EX10_LamdaExpression
{
    delegate int Operator(int a, int b);
    delegate void OperatorCB(int a, int b);
 
    class MainClass
    {
        public static Operator calculate;
        public static OperatorCB calculateCB;
 
        public static void Main (string[] args)
        {
            // Lamda Expression
            calculate = (int a, int b) => a + b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            calculate = (int a, int b) => a - b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            calculate = (int a, int b) => a * b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            calculate = (int a, int b) => a / b;
            Console.WriteLine ("{0} + {1} = {2}"8334, calculate (8334));
 
            // Lamda Expression Statement
            calculateCB = (int a, int b) => {
                int result = a % b;
                Console.WriteLine ("{0} % {1} = {2}", a, b, result);
            };
            calculateCB(8334);
 
            // Lamda Expression in LINQ
            int[] numbers = { 54139867201315 };
            int oddNumberCount = numbers.Where (n => n % 2 != 0).Count();
            Console.WriteLine (oddNumberCount);
        }
    }
}
cs


LINQ 예제

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
using System;
using System.Linq;
 
namespace EX10_LINQ
{
    class General
    {
        public string Name { get; set; }
        public int Power { get; set; }
        public int HP { get; set; }
    }
 
    class MainClass
    {
        public static void Main (string[] args)
        {
            General[] generals = {
                new General() {Name = "유비", Power = 75, HP = 100},
                new General() {Name = "관우", Power = 98, HP = 100},
                new General() {Name = "장비", Power = 99, HP = 100},
                new General() {Name = "간옹", Power = 45, HP = 49},
                new General() {Name = "미축", Power = 32, HP = 30}
            };
 
            var listGenerals = from general in generals
                                where general.HP > 50
                                orderby general.Power descending
                                select general;
            
            foreach (var g in listGenerals)
            {
                Console.WriteLine("장수명 : {0} HP : {1}", g.Name, g.HP);
            }
        }
    }
}
 
cs


+ Recent posts