teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

軽微な修正

2018/02/26 11:07

投稿

necos
necos

スコア52

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
  - 重複があれば、そのIDと該当するクラス名をLogに出力したい
8
8
 
9
9
  **解決方法**
10
-
10
+ ---
11
11
  ```
12
12
  var duplicationCustomers = CreateCustomers().GroupBy(x => x.Id).Where(x => x.Count() > 1).SelectMany(x => x);
13
13
 

4

軽微な修正

2018/02/26 11:07

投稿

necos
necos

スコア52

title CHANGED
File without changes
body CHANGED
@@ -8,13 +8,14 @@
8
8
 
9
9
  **解決方法**
10
10
 
11
- ```ここに言語を入力
11
+ ```
12
12
  var duplicationCustomers = CreateCustomers().GroupBy(x => x.Id).Where(x => x.Count() > 1).SelectMany(x => x);
13
13
 
14
14
  foreach (var customer in duplicationCustomers )
15
15
  {
16
16
  Debug.LogFormat("{0}:{1}", customer.Id, customer.GetType().Name);
17
+ }
17
- }```
18
+ ```
18
19
 
19
20
  **前提条件**
20
21
  ---

3

解決方法を追記

2018/02/26 11:07

投稿

necos
necos

スコア52

title CHANGED
File without changes
body CHANGED
@@ -6,8 +6,16 @@
6
6
  - 重複が無ければ、何もしない
7
7
  - 重複があれば、そのIDと該当するクラス名をLogに出力したい
8
8
 
9
+ **解決方法**
9
10
 
11
+ ```ここに言語を入力
12
+ var duplicationCustomers = CreateCustomers().GroupBy(x => x.Id).Where(x => x.Count() > 1).SelectMany(x => x);
10
13
 
14
+ foreach (var customer in duplicationCustomers )
15
+ {
16
+ Debug.LogFormat("{0}:{1}", customer.Id, customer.GetType().Name);
17
+ }```
18
+
11
19
  **前提条件**
12
20
  ---
13
21
 

2

本来の実装方針を記載

2018/02/26 11:06

投稿

necos
necos

スコア52

title CHANGED
File without changes
body CHANGED
@@ -15,25 +15,89 @@
15
15
  こういったクラスを多数作った際にIdの重複が無いかをチェックしたいです。
16
16
 
17
17
  ```ここに言語を入力
18
- public class CustomerA : ICustomer
18
+ class Program
19
19
  {
20
+ interface ICustomer
21
+ {
20
- string id = "A";
22
+ string Id { get; }
23
+ void Action();
21
- }
24
+ }
22
25
 
23
- public class CustomerB : ICustomer
26
+ public class CustomerA : ICustomer
24
- {
27
+ {
25
- string id = "B";
28
+ public string Id => "A";
29
+ public voic ACtion()
30
+ {
31
+ //個別処理
26
- }
32
+ }
33
+ }
27
34
 
35
+ public class CustomerB : ICustomer
36
+ {
37
+ public string Id => "B";
38
+ public voic ACtion()
39
+ {
40
+ //個別処理
41
+ }
42
+ }
43
+
28
- public class CustomerC : ICustomer
44
+ public class CustomerC : ICustomer
29
- {
45
+ {
30
- string id = "C";
46
+ public string Id => "C";
47
+ public voic ACtion()
48
+ {
49
+ //個別処理
50
+ }
51
+ }
52
+
53
+ static void Main(string[] args)
54
+ {
55
+ string id = GetId();
56
+
57
+ ICustomer _customer = CreateCustomer(id)
58
+
59
+ if(_customer != null)
60
+ _customer.Action();
61
+ }
62
+
63
+ static string GetId()
64
+ {
65
+ return ("サーバーから取得したID");
66
+ }
67
+
68
+ static ICustomer CreateCustomer(string id)
69
+ {
70
+ //swich(id)
71
+ //{
72
+ // case id = "A":
73
+ // return new CustomerA();
74
+ // break;
75
+ // case id = "B":
76
+ // return new CustomerB();
77
+ // break;
78
+ // case id = "C":
79
+ // return new CustomerC();
80
+ // break;
81
+ // ・
82
+ // ・
83
+ // ・
84
+ // 各CustomerクラスにIDを紐付け、Customerクラスが増えたときに、
85
+ // Caseが増えるのを避けたい
86
+ // case id = "D":
87
+ // return new CustomerD();
88
+ // break;
89
+ // default:
90
+ // //エラー処理
91
+ // break;
92
+ //}
93
+
94
+ //そのため、switchCaseを使わずに、以下のような実装を検討している
95
+ return
96
+ CreateCustomers().FirstOrDefault(x => x.id == id);
97
+ }
31
98
  }
32
- //以後、同様のクラスが複数存在する
33
99
  ```
34
100
 
35
-
36
-
37
101
  Reflectionの機能を用いた以下のようなメソッドにより、ICustomerを継承しているクラスの一覧を取得した後に
38
102
 
39
103
  ```

1

誤植修正

2018/02/26 08:47

投稿

necos
necos

スコア52

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,7 @@
45
45
  }
46
46
  ```
47
47
 
48
- Linqを使ってうまく書きたいのですが、書き方があれば教えてください。
48
+ Linqを使ってうまく書きたいのですが、うまい書き方があれば教えてください。
49
49
  よろしくお願いいたします。
50
50
 
51
51
  **環境**