'type'에 해당되는 글 2건

  1. 2022.07.20 C# Type Class 타입 클래스
  2. 2021.12.15 C# typeof - 타입 정보 확인

C# Type Class 타입 클래스

C# 2022. 7. 20. 00:12 |
반응형

Type 클래스를 사용해 보자.

 

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
using System;
using System.Reflection;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Type t = typeof(String);
            //Type t = Type.GetType("System.String");
            String str = "";
            Type t = str.GetType();
            // Gets a Type object that represents the specified type.
 
            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo method in methods)
            {
                // String 클래스의 Substring 함수는 2개의 오버로딩 함수가 있다.
                if (method.Name == "Substring")
                {
                    Console.WriteLine("- Method: " + method.Name);
 
                    ParameterInfo[] parameters = method.GetParameters();
                    foreach (ParameterInfo parameter in parameters)
                    {
                        Console.WriteLine("Parameter: " + parameter.Name);
                    }
                }
            }
 
            Console.WriteLine();
 
            MethodInfo substr = t.GetMethod("Substring"new Type[] { typeof(int), typeof(int) });
            // Searches for the specified method whose parameters match the specified generic
            // parameter count, argument types and modifiers, using the specified binding constraints.
 
            Object result = substr.Invoke("Hello, World!"new Object[] { 75 });
            // Invokes the method or constructor represented by the current instance, using the
            // specified parameters.
 
            Console.WriteLine("{0} returned \"{1}\".", substr, result);
        }
    }
}
 

 

소스를 빌드하고 실행한다.

 

 

반응형
Posted by J-sean
:

C# typeof - 타입 정보 확인

C# 2021. 12. 15. 14:36 |
반응형

타입의 정보를 알 수 있는 typeof 연산자는 System.Type 객체를 리턴한다.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.Reflection;
 
namespace CS
{
    using static System.Console;
 
    class Program
    {
        static void Main(string[] args)
        {
            Test testInstance = new Test();
            Type t = typeof(Test);
            MemberInfo[] members = t.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
 
            WriteLine($"Class Full Name: {t.FullName}\n");
            foreach (MemberInfo member in members)
            {
                WriteLine($"Member: {member.ToString()}"); // == WriteLine($"({member})");
                WriteLine($"Name: {member.Name},\tType: {member.MemberType},\tClass: {member.DeclaringType}\n");
            }
 
            WriteLine($"testInstance.GetType(): {testInstance.GetType()}");
            WriteLine($"typeof(Test): {typeof(Test)}");
            WriteLine($"testInstance.GetType() == typeof(Test): {testInstance.GetType() == typeof(Test)}");
        }
    }
 
    class Test
    {
        int _a;
        int _b;
 
        public Test()
        {
            _a = 1;
            _b = 2;
        }
 
        public Test(int a, int b)
        {
            _a = a;
            _b = b;
        }
 
        private void Info()
        {
            WriteLine("Test Info.");
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

타입의 정보가 표시된다.

 

반응형
Posted by J-sean
: