2012-04-12 12 views
0

入れ子になったプロパティ(オブジェクト)を持つオブジェクトをcsvファイルにエクスポートするには、sloutionを使用しています。ネストされたプロパティと値を持つオブジェクトをエクスポートします。

ここでコード:ここで

/// <summary> 
    /// Exports a CSV 
    /// </summary> 
    /// <param name="csv">The CSV data as a string</param> 
    /// <param name="filename">The filename for the exported file</param> 
    public static void ExportCSV(string csv, string filename) 
    { 
     StreamWriter writer = new StreamWriter(filename); 
     try 
     { 
      writer.Write(csv); 
     } 
     catch (FileNotFoundException ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      writer.Close(); 
     } 
    } 

    /// <summary> 
    /// Generate the CSV data as a string using reflection on the objects in the list 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="list">The generic list</param> 
    /// <returns></returns> 
    public static string GetCSV<T>(this List<T> list) 
    { 
     StringBuilder sb = new StringBuilder(); 

     for (int i = 0; i < list.Count; i++) 
     { 
      sb.AppendLine(GetPropertiesString<T>(list[i])); 
     } 

     return sb.ToString(); 
    } 


    public static string GetPropertiesString<T>(T item) 
    { 
     StringBuilder sb = new StringBuilder(); 
     PropertyInfo[] propInfos = typeof(T).GetProperties(); 

     for (int i = 0; i <= propInfos.Length - 1; i++) 
     { 
      Type propertyType = propInfos[i].PropertyType; 

      if (propertyType != typeof(string) && propertyType != typeof(int) && propertyType != typeof(double) && propertyType != typeof(float) && propertyType != typeof(decimal)) 
      { 
       string test = GetPropertiesString(propertyType); 
      } 

      sb.Append(propInfos[i].Name); 

      if (i < propInfos.Length - 1) 
      { 
       sb.Append(","); 
      } 
     } 

     sb.AppendLine(); 

     for (int j = 0; j <= propInfos.Length - 1; j++) 
     { 
      object o = item.GetType().GetProperty(propInfos[j].Name).GetValue(item, null); 

      if (o != null) 
      { 
       string value = o.ToString(); 

       //Check if the value contans a comma and place it in quotes if so 
       if (value.Contains(",")) 
       { 
        value = string.Concat("\"", value, "\""); 
       } 

       //Replace any \r or \n special characters from a new line with a space 
       if (value.Contains("\r")) 
       { 
        value = value.Replace("\r", " "); 
       } 
       if (value.Contains("\n")) 
       { 
        value = value.Replace("\n", " "); 
       } 

       sb.Append(value); 
      } 

      if (j < propInfos.Length - 1) 
      { 
       sb.Append(","); 
      } 
     } 

     return sb.ToString(); 
    } 
} 

}

私のオブジェクト

/// <summary> 
/// Basic Person object 
/// </summary> 
public class Person 
{ 
    public string Forename { get; set; } 
    public string Surname { get; set; } 
    public int Age { get; set; } 
    public Adress Adress { get; set; } 
} 

public class Adress 
{ 
    public string Place { get; set; } 
    public int PLZ { get; set; } 
} 

所望のcsvファイル形式:

header: Forename, Surename, Age, Place, PLZ 
values: Joe, Stevens, 30,Town1, 11111 

Iは、メソッド再帰的に呼び出すことを試みましたしかし、 PropertyInfoの結果propInfos = typeof(T).GetProperties()に奇妙な項目がありますか?

ネストされたオブジェクトが深いオブジェクトをエクスポートすることは可能です。

提案がありますか?

おかげで、 トルステン

答えて

0

あなたは右のタイプの再帰的メソッドを呼び出していないので、あなたのコードが期待どおりに動作しません。

GetPropertiesString(propertyType); 

は、このように呼ばれるべき:

dynamic ob = Activator.CreateInstance(propertyType); 
string test = GetPropertiesString(ob); 
+0

やあ、ありがとうございました。私はあなたに成功なしの例を試みた。プロパティがネストされたオブジェクトであるかどうかを確認する方法を教えてください。現時点で私は単純なif(propertyType!= typeof(string))でチェックしますが、これは正しくないか便利だと思います。 – tro

+0

問題は、あなたがSystem.TypeでGetPropertiesStringを呼び出していることです。そのため、あなたは他のプロパティを取得しています。したがって、そのコードを私のものに置き換えて、デバッガをステップ実行して、ネストされた型のプロパティを取得することを確認してください。あなたはコードにいくつかの問題がありますが、メソッドを再帰的に呼び出すときにStringBuilderを元に戻すので、以前の結果が失われるためです – ionden

関連する問題