(C#) Array to Dictionary (Linq)

Unity3D/C# 2019. 4. 5. 13:52
반응형

T[]에는 ToDictionary에 대한 정의가 포함되어 있지 않고 가장적합한 확장 메서드 오버로드 Enumerable.ToDictionary<int, T>(IEnumerable, Func<int, T> 에는 IEnumerable형식의 수신기가 필요합니다.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    public class App
    {
        private DataLoader dataLoader;      //데이터 로드 담당 
        private DataStorage dataStorage;    //데이터 자장 담당 
 
        public App()
        {
            this.dataLoader = new DataLoader();
            this.dataStorage = new DataStorage();
        }
 
        public void Start()
        {
            this.DisplayDataValues<WeaponData>("./data/weapon_data.json");
            Console.WriteLine();
            this.DisplayDataValues<CharacterData>("./data/character_data.json");
        }
 
        public void DisplayDataValues<T>(string path) where T : RawData
        {
            //데이터 로드 (파일 읽기)
            var arrWeaponDatas = this.dataLoader.LoadData<T>(path);
            this.dataStorage.Store<T>(arrWeaponDatas);
            //불러오기 테스트 
            T[] values = dataStorage.GetData<T>();
            Dictionary<int, T> dictionary = values.ToDictionary(data => data.id);
 
            //T[]에는 ToDictionary에 대한 정의가 포함되어 있지 않고 가장적합한 확장 메서드 오버로드 Enumerable.ToDictionary<int, T>(IEnumerable, Func<int, T> 에는 IEnumerable형식의 수신기가 필요합니다.
            //var dictionary = values.ToDictionary<int, T>(data => data.id);
 
 
            // Display all keys and values.
            foreach (KeyValuePair<int, T> pair in dictionary)
            {
                Console.WriteLine("{0}"pair.Value.id);
            }
        }
    }
}
 
 
 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new App();
            app.Start();
 
            //var test = new Test();
            //test.Test3();
        }
    }
}
 
 
 

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace ConsoleApp1
{
    //데이터 로드 클래스 
    public class DataLoader
    {
        public DataLoader()
        {
 
        }
 
        //데이터 로드 메서드 
        public T[] LoadData<T>(string path)
        {
            //파일 읽기 
            var json = File.ReadAllText(path);
            //역직렬화 
            var arrDatas = JsonConvert.DeserializeObject<T[]>(json);
            return arrDatas;
        }
    }
}
 
 
 

 

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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
 
namespace ConsoleApp1
{
    //데이터 적재 클래스 
    public class DataStorage
    {
        private ArrayList arrayList;
 
        public DataStorage()
        {
            this.arrayList = new ArrayList();
        }
 
        //저장 
        public void Store<T>(T[] arrDatas) where T : RawData
        {
            arrayList.Add(arrDatas);
        }
 
        //불러오기 
        public T[] GetData<T>()
        {
            T[] rtnArrDatas = null;
 
            foreach (var arr in this.arrayList)
            {
                if (arr is T[])
                {
                    rtnArrDatas = arr as T[];
                    break;
                }
            }
            return rtnArrDatas;
        }
    }
}
 
 
 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    //데이터 매핑 클래스 (바인딩클래스)
    public class WeaponData : RawData
    {
        public string name;
        public int max_level;
        public float damage_min;
        public float damage_max;
    }
}
 
 
 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    public class RawData
    {
        public int id;
    }
}
 
 
 

 

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
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp1
{
    public class Test
    {
        public Test()
        {
        }
 
        public void Test1()
        {
            // Example integer array.
            int[] values = new int[]
            {
            1,
            3,
            5,
            7
            };
 
            // First argument is the key, second the value.
            Dictionary<intbool> dictionary =
                values.ToDictionary(v => v, v => true);
 
            // Display all keys and values.
            foreach (KeyValuePair<intbool> pair in dictionary)
            {
                Console.WriteLine(pair);
            }
        }
 
        public void Test2()
        {
            // Example integer array.
            WeaponData[] values = new WeaponData[] { };
 
            // First argument is the key, second the value.
            Dictionary<int, WeaponData> dictionary =
                values.ToDictionary(data => data.id);
 
            // Display all keys and values.
            foreach (KeyValuePair<int, WeaponData> pair in dictionary)
            {
                Console.WriteLine(pair);
            }
        }
 
        public void Test3()
        {
            var dataLoader = new DataLoader();
            var dataStorage = new DataStorage();
 
 
 
            //데이터 로드 (파일 읽기)
            var arrWeaponDatas = dataLoader.LoadData<WeaponData>("./data/weapon_data.json");
            dataStorage.Store<WeaponData>(arrWeaponDatas);
 
            // Example integer array.
            WeaponData[] values = dataStorage.GetData<WeaponData>();
 
            // First argument is the key, second the value.
            Dictionary<int, WeaponData> dictionary =
                values.ToDictionary(data => data.id);
 
            // Display all keys and values.
            foreach (KeyValuePair<int, WeaponData> pair in dictionary)
            {
                Console.WriteLine("{0} {1}"pair.Value.id, pair.Value.name);
            }
        }
    }
}
 
 
 
반응형

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

(C#) resolving-method-overloads  (0) 2019.04.10
(C#) 타입 비교  (0) 2019.04.05
인벤토리 만들기 (제너릭)  (0) 2019.04.02
referencesource microsoft  (0) 2019.04.02
IEnumerator, IEnumerable 상속받은 Inventory 구현 (인덱서)  (0) 2019.04.01
: