(C#) 타입 비교

Unity3D/C# 2019. 4. 5. 15:41
반응형

 

 

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
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace ConsoleApp2
{
    class App
    {
        public App() { }
 
        Dictionary<int, WeaponData> dicWeaponData;
        Dictionary<int, CharacterData> dicChracterData;
 
        public void Start()
        {
            this.dicWeaponData = new Dictionary<int, WeaponData>();
            this.dicChracterData = new Dictionary<int, CharacterData>();
 
            this.ReadFileAndAddToDictionary<WeaponData>("./data/weapon_data.json", dicWeaponData);
            this.ReadFileAndAddToDictionary<CharacterData>("./data/character_data.json", dicChracterData);
 
            this.ConsoleFromDictionary(this.dicWeaponData);
            this.ConsoleFromDictionary(this.dicChracterData);
        }
 
        // 파일이있는 경로(문자열)를 전달하면 사전에 등록 해줬으면...
        private void ReadFileAndAddToDictionary<T>(string path, Dictionary<int, T> dic) where T : RawData
        {
            //파일 읽기
            var json = File.ReadAllText(path);
 
            //역직렬화 
            var arrDatas = JsonConvert.DeserializeObject<T[]>(json);
 
            foreach (var data in arrDatas)
            {
                dic.Add(data.id, data);
            }
        }
 
        //내가 원하는 사전에 있는 내용들이 출력되었으면..
        private void ConsoleFromDictionary<T>(Dictionary<int, T> dic)
        {
            Console.WriteLine("ConsoleFromDictionary: {0}", dic);
 
            foreach (var element in dic)
            {
                var type = element.Value.GetType();
                if (type == typeof(WeaponData))
                {
 
                }
                if (element.Value is WeaponData)
                {
                    // 처리로직
                }
 
 
                //if (element.Value.GetType() is WeaponData)
                //{
                //    var weaponData = element.Value as WeaponData;
                //    Console.WriteLine("{0}, {1}, {2}", weaponData.id, weaponData.name, weaponData.max_level);
                //}
                //else if (element.Value.GetType() is CharacterData)
                //{
                //    var characterData = element.Value as CharacterData;
                //    Console.WriteLine("{0}, {1}", characterData.id, characterData.name);
                //}
            }
        }
 
    }
}
 
 
 

https://docko.tistory.com/entry/C-%ED%81%B4%EB%9E%98%EC%8A%A4-%ED%83%80%EC%9E%85-%EB%B9%84%EA%B5%90

 

C# 클래스 타입 비교

C#에서 클래스 타입을 비교하는 2가지 방법입니다. 1 2 3 4 5 6 7 8 9 10 11 string str = "문자"; if (str.GetType() == typeof(string)) { // 처리 로직 } if (str is string) { // 처리로직 } Colored by Color..

docko.tistory.com

 

반응형

'Unity3D > C#' 카테고리의 다른 글

visual studio code 에서 c# 디버깅 하기  (0) 2019.08.14
(C#) resolving-method-overloads  (0) 2019.04.10
(C#) Array to Dictionary (Linq)  (0) 2019.04.05
인벤토리 만들기 (제너릭)  (0) 2019.04.02
referencesource microsoft  (0) 2019.04.02
: