こんにちは。
Windows10でWPFのアプリケーションを開発しています。
Visual Studio 2017 proを使っています。
###前提・実現したいこと
クラスのfieldの名前とtypeとvalueをtab区切りで出力したいです。
###試したこと
下記コード。
###input cells
1 2 int PrimaryKey
2 4 int SecondaryKey
3 5 int ThirdKey
###output log
1 2 int PrimaryKey
2 4 int SecondaryKey
3 5 int ThirdKey
###発生している問題・エラーメッセージ
propertyがnull
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.Reflection; 4 5namespace Table 6{ 7 public class TableDefinitionDocument 8 { 9 public TableDefinitionDocument(List<string> cells) 10 { 11 this.PrimaryKeyNumber = int.Parse(cells[1]); 12 this.SequenceKeyNumber = int.Parse(cells[2]); 13 this.Type = cells[3]; 14 this.Description = cells[4]; 15 FieldToString(); 16 } 17 18 public string FieldToString() 19 { 20 string log = ""; 21 string errorlog = ""; 22 var members = typeof(TableDefinitionDocument).GetMembers( 23 BindingFlags.Public | BindingFlags.NonPublic | 24 BindingFlags.Instance | BindingFlags.Static | 25 BindingFlags.DeclaredOnly); 26 foreach(var member in members) 27 { 28 try 29 { 30 Type type = member.GetType(); 31 PropertyInfo property = type.GetProperty(member.Name);//<-これがnull 32 var value = (String)property.GetValue(member, null); 33 log += member.Name + "\t" + member.GetType().ToString() + "\t" + value + "\r\n"; 34 } 35 catch (Exception exception) 36 { 37 errorlog += exception.Message; 38 } 39 } 40 41 return log; 42 } 43 44 public int Id { get; set; } 45 public int PrimaryKeyNumber { get; set; } 46 public int SequenceKeyNumber { get; set; } 47 public string Type { get; set; } 48 public string Description { get; set; } 49 } 50} 51
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio pro 2017
Microsoft .NET Framework
Version 4.6.01586
です。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/26 09:03