質問編集履歴

5

回答の追記

2015/03/07 03:11

投稿

OJI
OJI

スコア6

test CHANGED
File without changes
test CHANGED
@@ -95,3 +95,45 @@
95
95
  FixedDataAttributeについては[ブログ](http://blog.livedoor.jp/rizriver/archives/23360716.html)を参考にしています。
96
96
 
97
97
  よろしくお願いいたします。
98
+
99
+
100
+
101
+ ---
102
+
103
+
104
+
105
+ ベストアンサーの方の御指摘頂いた通り、LINQのSelect句に漏れがありました。
106
+
107
+ FixedDefineという拡張プロパティしか存在していなかったため、TargetPropを追加します。
108
+
109
+
110
+
111
+ ```lang-C#
112
+
113
+ }).Select(pi =>
114
+
115
+ new { TargetProp = pi, FixedDefine = attrTmp }
116
+
117
+ ).OrderBy(e =>
118
+
119
+ e.FixedDefine.Order);
120
+
121
+ ```
122
+
123
+
124
+
125
+ TargetPropのSetValueメソッドで代入可能でした。
126
+
127
+ ```lang-C#
128
+
129
+ foreach (var item in q.Array())
130
+
131
+ {
132
+
133
+ item.TargetProp.SetValue(this, data.Substring(count, item.FixedDefine.FixedLength));
134
+
135
+ count += item.FixedDefine.FixedLength;
136
+
137
+ }
138
+
139
+ ```

4

誤字の修正

2015/03/07 03:11

投稿

OJI
OJI

スコア6

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- > エラー 73 実行不可能なメンバー 'AnonymousType#2\.SetValue' をメソッドのように使用することはできません。
7
+ > エラー 73 実行不可能なメンバー 'AnonymousType#2.SetValue' をメソッドのように使用することはできません。
8
8
 
9
9
 
10
10
 
@@ -18,45 +18,45 @@
18
18
 
19
19
  {
20
20
 
21
- \[FixedData\(Order = 1, FixedLength = 20, PadChar = ' '\)\]
21
+ [FixedData(Order = 1, FixedLength = 20, PadChar = ' ')]
22
22
 
23
23
  public string name { set; get; }
24
24
 
25
25
 
26
26
 
27
- \[FixedData\(Order = 2, FixedLength = 3, PadChar = '0'\)\]
27
+ [FixedData(Order = 2, FixedLength = 3, PadChar = '0')]
28
28
 
29
29
  public string age { set; get; }
30
30
 
31
31
 
32
32
 
33
- \[FixedData\(Order = 3, FixedLength = 1, PadChar = ' '\)\]
33
+ [FixedData(Order = 3, FixedLength = 1, PadChar = ' ')]
34
34
 
35
35
  public string sex { set; get; }
36
36
 
37
37
 
38
38
 
39
- public Person\(string data\) {
39
+ public Person(string data) {
40
40
 
41
41
  int count = 0;
42
42
 
43
- PropertyInfo\[\] pi = this\.GetType\(\)\.GetProperties\(BindingFlags\.Instance | BindingFlags\.Public | BindingFlags\.NonPublic\);
43
+ PropertyInfo[] pi = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
44
44
 
45
45
 
46
46
 
47
47
  FixedDataAttribute attrTmp = null;
48
48
 
49
- var q = pi\.Where\(p =>
49
+ var q = pi.Where(p =>
50
50
 
51
51
  {
52
52
 
53
- FixedDataAttribute\[\] attr = \(FixedDataAttribute\[\]\)p\.GetCustomAttributes\(typeof\(FixedDataAttribute\), true\);
53
+ FixedDataAttribute[] attr = (FixedDataAttribute[])p.GetCustomAttributes(typeof(FixedDataAttribute), true);
54
54
 
55
- if \(attr\.Length > 0\)
55
+ if (attr.Length > 0)
56
56
 
57
57
  {
58
58
 
59
- attrTmp = attr\[0\];
59
+ attrTmp = attr[0];
60
60
 
61
61
  return true;
62
62
 
@@ -64,23 +64,23 @@
64
64
 
65
65
  return false;
66
66
 
67
- }\)\.Select\(pi =>
67
+ }).Select(pi =>
68
68
 
69
69
  new { FixedDefine = attrTmp }
70
70
 
71
- \)\.OrderBy\(e =>
71
+ ).OrderBy(e =>
72
72
 
73
- e\.FixedDefine\.Order\);
73
+ e.FixedDefine.Order);
74
74
 
75
75
 
76
76
 
77
- foreach \(var item in q\.Array\(\)\)
77
+ foreach (var item in q.Array())
78
78
 
79
79
  {
80
80
 
81
- item\.SetValue\(data\.Substring\(count, item\.FixedDefine\.FixedLength\)\);
81
+ item.SetValue(data.Substring(count, item.FixedDefine.FixedLength));
82
82
 
83
- count \+= item\.FixedDefine\.FixedLength;
83
+ count += item.FixedDefine.FixedLength;
84
84
 
85
85
  }
86
86
 

3

誤字の修正

2015/03/06 13:08

投稿

OJI
OJI

スコア6

test CHANGED
@@ -1 +1 @@
1
- \[C#\]固定長テキストをクラスのプロパティに代入していくプログラム
1
+ [C#]固定長テキストをクラスのプロパティに代入していくプログラム
test CHANGED
File without changes

2

誤字の修正

2015/03/06 12:49

投稿

OJI
OJI

スコア6

test CHANGED
File without changes
test CHANGED
@@ -92,6 +92,6 @@
92
92
 
93
93
 
94
94
 
95
- FixedDataAttributeについては\[ブログ\]\(http://blog\.livedoor\.jp/rizriver/archives/23360716\.html\)を参考にしています。
95
+ FixedDataAttributeについては[ブログ](http://blog.livedoor.jp/rizriver/archives/23360716.html)を参考にしています。
96
96
 
97
97
  よろしくお願いいたします。

1

誤字の修正

2015/03/06 12:48

投稿

OJI
OJI

スコア6

test CHANGED
@@ -1 +1 @@
1
- [C#]固定長テキストをクラスのプロパティに代入していくプログラム
1
+ \[C#\]固定長テキストをクラスのプロパティに代入していくプログラム
test CHANGED
@@ -1,14 +1,10 @@
1
1
  固定長のテキストをクラスのプロパティに代入していくプログラムを作成中です。
2
2
 
3
- LINQを通したPropertyInfoはsetValueが使えず苦戦しています。
3
+ LINQを通したPropertyInfoは下記エラーが表示されて、setValueが使えず苦戦しています。
4
4
 
5
5
 
6
6
 
7
- >*************************************************************
8
-
9
- >エラー 73 実行不可能なメンバー 'AnonymousType#2.SetValue' をメソッドのように使用することはできません。
7
+ > エラー 73 実行不可能なメンバー 'AnonymousType#2\.SetValue' をメソッドのように使用することはできません。
10
-
11
- >*************************************************************
12
8
 
13
9
 
14
10
 
@@ -22,45 +18,45 @@
22
18
 
23
19
  {
24
20
 
25
- [FixedData(Order = 1, FixedLength = 20, PadChar = ' ')]
21
+ \[FixedData\(Order = 1, FixedLength = 20, PadChar = ' '\)\]
26
22
 
27
23
  public string name { set; get; }
28
24
 
29
25
 
30
26
 
31
- [FixedData(Order = 2, FixedLength = 3, PadChar = '0')]
27
+ \[FixedData\(Order = 2, FixedLength = 3, PadChar = '0'\)\]
32
28
 
33
29
  public string age { set; get; }
34
30
 
35
31
 
36
32
 
37
- [FixedData(Order = 3, FixedLength = 1, PadChar = ' ')]
33
+ \[FixedData\(Order = 3, FixedLength = 1, PadChar = ' '\)\]
38
34
 
39
35
  public string sex { set; get; }
40
36
 
41
37
 
42
38
 
43
- public Person(string data) {
39
+ public Person\(string data\) {
44
40
 
45
41
  int count = 0;
46
42
 
47
- PropertyInfo[] pi = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
43
+ PropertyInfo\[\] pi = this\.GetType\(\)\.GetProperties\(BindingFlags\.Instance | BindingFlags\.Public | BindingFlags\.NonPublic\);
48
44
 
49
45
 
50
46
 
51
47
  FixedDataAttribute attrTmp = null;
52
48
 
53
- var q = pi.Where(p =>
49
+ var q = pi\.Where\(p =>
54
50
 
55
51
  {
56
52
 
57
- FixedDataAttribute[] attr = (FixedDataAttribute[])p.GetCustomAttributes(typeof(FixedDataAttribute), true);
53
+ FixedDataAttribute\[\] attr = \(FixedDataAttribute\[\]\)p\.GetCustomAttributes\(typeof\(FixedDataAttribute\), true\);
58
54
 
59
- if (attr.Length > 0)
55
+ if \(attr\.Length > 0\)
60
56
 
61
57
  {
62
58
 
63
- attrTmp = attr[0];
59
+ attrTmp = attr\[0\];
64
60
 
65
61
  return true;
66
62
 
@@ -68,23 +64,23 @@
68
64
 
69
65
  return false;
70
66
 
71
- }).Select(pi =>
67
+ }\)\.Select\(pi =>
72
68
 
73
69
  new { FixedDefine = attrTmp }
74
70
 
75
- ).OrderBy(e =>
71
+ \)\.OrderBy\(e =>
76
72
 
77
- e.FixedDefine.Order);
73
+ e\.FixedDefine\.Order\);
78
74
 
79
75
 
80
76
 
81
- foreach (var item in q.Array())
77
+ foreach \(var item in q\.Array\(\)\)
82
78
 
83
79
  {
84
80
 
85
- item.SetValue(data.Substring(count, item.FixedDefine.FixedLength));
81
+ item\.SetValue\(data\.Substring\(count, item\.FixedDefine\.FixedLength\)\);
86
82
 
87
- count += item.FixedDefine.FixedLength;
83
+ count \+= item\.FixedDefine\.FixedLength;
88
84
 
89
85
  }
90
86
 
@@ -96,6 +92,6 @@
96
92
 
97
93
 
98
94
 
99
- FixedDataAttributeについては[ブログ](http://blog.livedoor.jp/rizriver/archives/23360716.html)を参考にしています。
95
+ FixedDataAttributeについては\[ブログ\]\(http://blog\.livedoor\.jp/rizriver/archives/23360716\.html\)を参考にしています。
100
96
 
101
97
  よろしくお願いいたします。