固定長のテキストをクラスのプロパティに代入していくプログラムを作成中です。
LINQを通したPropertyInfoは下記エラーが表示されて、setValueが使えず苦戦しています。
エラー 73 実行不可能なメンバー 'AnonymousType#2.SetValue' をメソッドのように使用することはできません。
どなたか下記について御指摘をお願いいたします。
lang
1public class Person: FixDataEntity 2{ 3 [FixedData(Order = 1, FixedLength = 20, PadChar = ' ')] 4 public string name { set; get; } 5 6 [FixedData(Order = 2, FixedLength = 3, PadChar = '0')] 7 public string age { set; get; } 8 9 [FixedData(Order = 3, FixedLength = 1, PadChar = ' ')] 10 public string sex { set; get; } 11 12 public Person(string data) { 13 int count = 0; 14 PropertyInfo[] pi = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); 15 16 FixedDataAttribute attrTmp = null; 17 var q = pi.Where(p => 18 { 19 FixedDataAttribute[] attr = (FixedDataAttribute[])p.GetCustomAttributes(typeof(FixedDataAttribute), true); 20 if (attr.Length > 0) 21 { 22 attrTmp = attr[0]; 23 return true; 24 } 25 return false; 26 }).Select(pi => 27 new { FixedDefine = attrTmp } 28 ).OrderBy(e => 29 e.FixedDefine.Order); 30 31 foreach (var item in q.Array()) 32 { 33 item.SetValue(data.Substring(count, item.FixedDefine.FixedLength)); 34 count += item.FixedDefine.FixedLength; 35 } 36 } 37}
FixedDataAttributeについてはブログを参考にしています。
よろしくお願いいたします。
ベストアンサーの方の御指摘頂いた通り、LINQのSelect句に漏れがありました。
FixedDefineという拡張プロパティしか存在していなかったため、TargetPropを追加します。
lang
1 }).Select(pi => 2 new { TargetProp = pi, FixedDefine = attrTmp } 3 ).OrderBy(e => 4 e.FixedDefine.Order);
TargetPropのSetValueメソッドで代入可能でした。
lang
1 foreach (var item in q.Array()) 2 { 3 item.TargetProp.SetValue(this, data.Substring(count, item.FixedDefine.FixedLength)); 4 count += item.FixedDefine.FixedLength; 5 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/03/07 03:05