質問編集履歴
5
軽微な修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
**解決方法**
|
18
18
|
|
19
|
-
|
19
|
+
---
|
20
20
|
|
21
21
|
```
|
22
22
|
|
4
軽微な修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,232 +18,234 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
+
```
|
22
|
+
|
23
|
+
var duplicationCustomers = CreateCustomers().GroupBy(x => x.Id).Where(x => x.Count() > 1).SelectMany(x => x);
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
foreach (var customer in duplicationCustomers )
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
Debug.LogFormat("{0}:{1}", customer.Id, customer.GetType().Name);
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
**前提条件**
|
40
|
+
|
41
|
+
---
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
こういったクラスを多数作った際にIdの重複が無いかをチェックしたいです。
|
48
|
+
|
49
|
+
|
50
|
+
|
21
51
|
```ここに言語を入力
|
22
52
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
53
|
+
class Program
|
54
|
+
|
55
|
+
{
|
56
|
+
|
57
|
+
interface ICustomer
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
string Id { get; }
|
62
|
+
|
63
|
+
void Action();
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
public class CustomerA : ICustomer
|
70
|
+
|
71
|
+
{
|
72
|
+
|
73
|
+
public string Id => "A";
|
74
|
+
|
75
|
+
public voic ACtion()
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
//個別処理
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
public class CustomerB : ICustomer
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
public string Id => "B";
|
92
|
+
|
93
|
+
public voic ACtion()
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
//個別処理
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
public class CustomerC : ICustomer
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
public string Id => "C";
|
110
|
+
|
111
|
+
public voic ACtion()
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
//個別処理
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
static void Main(string[] args)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
string id = GetId();
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
ICustomer _customer = CreateCustomer(id)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
if(_customer != null)
|
136
|
+
|
137
|
+
_customer.Action();
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
static string GetId()
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
return ("サーバーから取得したID");
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
static ICustomer CreateCustomer(string id)
|
154
|
+
|
155
|
+
{
|
156
|
+
|
157
|
+
//swich(id)
|
158
|
+
|
159
|
+
//{
|
160
|
+
|
161
|
+
// case id = "A":
|
162
|
+
|
163
|
+
// return new CustomerA();
|
164
|
+
|
165
|
+
// break;
|
166
|
+
|
167
|
+
// case id = "B":
|
168
|
+
|
169
|
+
// return new CustomerB();
|
170
|
+
|
171
|
+
// break;
|
172
|
+
|
173
|
+
// case id = "C":
|
174
|
+
|
175
|
+
// return new CustomerC();
|
176
|
+
|
177
|
+
// break;
|
178
|
+
|
179
|
+
// ・
|
180
|
+
|
181
|
+
// ・
|
182
|
+
|
183
|
+
// ・
|
184
|
+
|
185
|
+
// 各CustomerクラスにIDを紐付け、Customerクラスが増えたときに、
|
186
|
+
|
187
|
+
// Caseが増えるのを避けたい
|
188
|
+
|
189
|
+
// case id = "D":
|
190
|
+
|
191
|
+
// return new CustomerD();
|
192
|
+
|
193
|
+
// break;
|
194
|
+
|
195
|
+
// default:
|
196
|
+
|
197
|
+
// //エラー処理
|
198
|
+
|
199
|
+
// break;
|
200
|
+
|
201
|
+
//}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
//そのため、switchCaseを使わずに、以下のような実装を検討している
|
206
|
+
|
207
|
+
return
|
208
|
+
|
209
|
+
CreateCustomers().FirstOrDefault(x => x.id == id);
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
```
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
Reflectionの機能を用いた以下のようなメソッドにより、ICustomerを継承しているクラスの一覧を取得した後に
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```
|
224
|
+
|
225
|
+
private static IEnumerable<ICustomer> CreateCustomers()
|
226
|
+
|
227
|
+
{
|
228
|
+
|
229
|
+
return Assembly.GetExecutingAssembly().GetTypes()
|
230
|
+
|
231
|
+
.Where(x => x.IsClass && x.BaseType == typeof(ICustomer))
|
232
|
+
|
233
|
+
.Select(x => (ICustomer)System.Activator.CreateInstance(x));
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
```
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
Linqを使ってうまく書きたいのですが、うまい書き方があれば教えてください。
|
242
|
+
|
243
|
+
よろしくお願いいたします。
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
**環境**
|
38
248
|
|
39
249
|
---
|
40
250
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
こういったクラスを多数作った際にIdの重複が無いかをチェックしたいです。
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
```ここに言語を入力
|
50
|
-
|
51
|
-
class Program
|
52
|
-
|
53
|
-
{
|
54
|
-
|
55
|
-
interface ICustomer
|
56
|
-
|
57
|
-
{
|
58
|
-
|
59
|
-
string Id { get; }
|
60
|
-
|
61
|
-
void Action();
|
62
|
-
|
63
|
-
}
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
public class CustomerA : ICustomer
|
68
|
-
|
69
|
-
{
|
70
|
-
|
71
|
-
public string Id => "A";
|
72
|
-
|
73
|
-
public voic ACtion()
|
74
|
-
|
75
|
-
{
|
76
|
-
|
77
|
-
//個別処理
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
public class CustomerB : ICustomer
|
86
|
-
|
87
|
-
{
|
88
|
-
|
89
|
-
public string Id => "B";
|
90
|
-
|
91
|
-
public voic ACtion()
|
92
|
-
|
93
|
-
{
|
94
|
-
|
95
|
-
//個別処理
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
public class CustomerC : ICustomer
|
104
|
-
|
105
|
-
{
|
106
|
-
|
107
|
-
public string Id => "C";
|
108
|
-
|
109
|
-
public voic ACtion()
|
110
|
-
|
111
|
-
{
|
112
|
-
|
113
|
-
//個別処理
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
static void Main(string[] args)
|
122
|
-
|
123
|
-
{
|
124
|
-
|
125
|
-
string id = GetId();
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
ICustomer _customer = CreateCustomer(id)
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
if(_customer != null)
|
134
|
-
|
135
|
-
_customer.Action();
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
static string GetId()
|
142
|
-
|
143
|
-
{
|
144
|
-
|
145
|
-
return ("サーバーから取得したID");
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
static ICustomer CreateCustomer(string id)
|
152
|
-
|
153
|
-
{
|
154
|
-
|
155
|
-
//swich(id)
|
156
|
-
|
157
|
-
//{
|
158
|
-
|
159
|
-
// case id = "A":
|
160
|
-
|
161
|
-
// return new CustomerA();
|
162
|
-
|
163
|
-
// break;
|
164
|
-
|
165
|
-
// case id = "B":
|
166
|
-
|
167
|
-
// return new CustomerB();
|
168
|
-
|
169
|
-
// break;
|
170
|
-
|
171
|
-
// case id = "C":
|
172
|
-
|
173
|
-
// return new CustomerC();
|
174
|
-
|
175
|
-
// break;
|
176
|
-
|
177
|
-
// ・
|
178
|
-
|
179
|
-
// ・
|
180
|
-
|
181
|
-
// ・
|
182
|
-
|
183
|
-
// 各CustomerクラスにIDを紐付け、Customerクラスが増えたときに、
|
184
|
-
|
185
|
-
// Caseが増えるのを避けたい
|
186
|
-
|
187
|
-
// case id = "D":
|
188
|
-
|
189
|
-
// return new CustomerD();
|
190
|
-
|
191
|
-
// break;
|
192
|
-
|
193
|
-
// default:
|
194
|
-
|
195
|
-
// //エラー処理
|
196
|
-
|
197
|
-
// break;
|
198
|
-
|
199
|
-
//}
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
//そのため、switchCaseを使わずに、以下のような実装を検討している
|
204
|
-
|
205
|
-
return
|
206
|
-
|
207
|
-
CreateCustomers().FirstOrDefault(x => x.id == id);
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
}
|
212
|
-
|
213
|
-
```
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
Reflectionの機能を用いた以下のようなメソッドにより、ICustomerを継承しているクラスの一覧を取得した後に
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
```
|
222
|
-
|
223
|
-
private static IEnumerable<ICustomer> CreateCustomers()
|
224
|
-
|
225
|
-
{
|
226
|
-
|
227
|
-
return Assembly.GetExecutingAssembly().GetTypes()
|
228
|
-
|
229
|
-
.Where(x => x.IsClass && x.BaseType == typeof(ICustomer))
|
230
|
-
|
231
|
-
.Select(x => (ICustomer)System.Activator.CreateInstance(x));
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
```
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
Linqを使ってうまく書きたいのですが、うまい書き方があれば教えてください。
|
240
|
-
|
241
|
-
よろしくお願いいたします。
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
**環境**
|
246
|
-
|
247
|
-
---
|
248
|
-
|
249
251
|
C# 4.0
|
3
解決方法を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,7 +14,23 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
|
17
|
+
**解決方法**
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```ここに言語を入力
|
22
|
+
|
23
|
+
var duplicationCustomers = CreateCustomers().GroupBy(x => x.Id).Where(x => x.Count() > 1).SelectMany(x => x);
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
foreach (var customer in duplicationCustomers )
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
Debug.LogFormat("{0}:{1}", customer.Id, customer.GetType().Name);
|
32
|
+
|
33
|
+
}```
|
18
34
|
|
19
35
|
|
20
36
|
|
2
本来の実装方針を記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,44 +32,172 @@
|
|
32
32
|
|
33
33
|
```ここに言語を入力
|
34
34
|
|
35
|
-
|
35
|
+
class Program
|
36
36
|
|
37
37
|
{
|
38
38
|
|
39
|
+
interface ICustomer
|
40
|
+
|
41
|
+
{
|
42
|
+
|
43
|
+
string Id { get; }
|
44
|
+
|
45
|
+
void Action();
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
public class CustomerA : ICustomer
|
52
|
+
|
53
|
+
{
|
54
|
+
|
39
|
-
|
55
|
+
public string Id => "A";
|
56
|
+
|
57
|
+
public voic ACtion()
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
//個別処理
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
public class CustomerB : ICustomer
|
70
|
+
|
71
|
+
{
|
72
|
+
|
73
|
+
public string Id => "B";
|
74
|
+
|
75
|
+
public voic ACtion()
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
//個別処理
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
public class CustomerC : ICustomer
|
88
|
+
|
89
|
+
{
|
90
|
+
|
91
|
+
public string Id => "C";
|
92
|
+
|
93
|
+
public voic ACtion()
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
//個別処理
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
static void Main(string[] args)
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
string id = GetId();
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
ICustomer _customer = CreateCustomer(id)
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
if(_customer != null)
|
118
|
+
|
119
|
+
_customer.Action();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
static string GetId()
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
return ("サーバーから取得したID");
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
static ICustomer CreateCustomer(string id)
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
//swich(id)
|
140
|
+
|
141
|
+
//{
|
142
|
+
|
143
|
+
// case id = "A":
|
144
|
+
|
145
|
+
// return new CustomerA();
|
146
|
+
|
147
|
+
// break;
|
148
|
+
|
149
|
+
// case id = "B":
|
150
|
+
|
151
|
+
// return new CustomerB();
|
152
|
+
|
153
|
+
// break;
|
154
|
+
|
155
|
+
// case id = "C":
|
156
|
+
|
157
|
+
// return new CustomerC();
|
158
|
+
|
159
|
+
// break;
|
160
|
+
|
161
|
+
// ・
|
162
|
+
|
163
|
+
// ・
|
164
|
+
|
165
|
+
// ・
|
166
|
+
|
167
|
+
// 各CustomerクラスにIDを紐付け、Customerクラスが増えたときに、
|
168
|
+
|
169
|
+
// Caseが増えるのを避けたい
|
170
|
+
|
171
|
+
// case id = "D":
|
172
|
+
|
173
|
+
// return new CustomerD();
|
174
|
+
|
175
|
+
// break;
|
176
|
+
|
177
|
+
// default:
|
178
|
+
|
179
|
+
// //エラー処理
|
180
|
+
|
181
|
+
// break;
|
182
|
+
|
183
|
+
//}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
//そのため、switchCaseを使わずに、以下のような実装を検討している
|
188
|
+
|
189
|
+
return
|
190
|
+
|
191
|
+
CreateCustomers().FirstOrDefault(x => x.id == id);
|
192
|
+
|
193
|
+
}
|
40
194
|
|
41
195
|
}
|
42
196
|
|
43
|
-
|
44
|
-
|
45
|
-
public class CustomerB : ICustomer
|
46
|
-
|
47
|
-
{
|
48
|
-
|
49
|
-
string id = "B";
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
public class CustomerC : ICustomer
|
56
|
-
|
57
|
-
{
|
58
|
-
|
59
|
-
string id = "C";
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
//以後、同様のクラスが複数存在する
|
64
|
-
|
65
197
|
```
|
66
198
|
|
67
199
|
|
68
200
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
201
|
Reflectionの機能を用いた以下のようなメソッドにより、ICustomerを継承しているクラスの一覧を取得した後に
|
74
202
|
|
75
203
|
|
1
誤植修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,7 +92,7 @@
|
|
92
92
|
|
93
93
|
|
94
94
|
|
95
|
-
Linqを使ってうまく書きたいのですが、
|
95
|
+
Linqを使ってうまく書きたいのですが、うまい書き方があれば教えてください。
|
96
96
|
|
97
97
|
よろしくお願いいたします。
|
98
98
|
|