私が書いているプログラムでは、他のデータベースではなく自分のデータ型を使用することを決めました。さまざまなオブジェクトを扱う方法を一般化する
データはcsvファイルとして保存され、.csvから単純な文字列[、]に変換する方法があります。
各クラスXはそれ自身の文字列で、[]はリストコンバータです。 最初の3つのクラスの後、ジェネリックとリフレクションを始めました。 一般的なListをstring [、]に変換するのに成功しましたが、その逆をやって見栄えが出ます。
私は文字列にリストを実装する方法[、]は、次のとおりです。
public string[,] ToArray(object[] sender)
{
if (sender.Length==0)
{
return null;
}
string[,] ret;
PropertyInfo[] props;
if (sender.Length > 0)
{
props = sender[0].GetType().GetProperties();
ret = new string[props.Length, sender.Length];
}
else
{
return null;
}
for (int i=0;i<sender.Length;i++)
{
for (int p = 0; p < props.Length; p++)
{
object value=props[p].GetValue(sender[i], null);
if (value!=null) ret[p, i] = value.ToString();
}
}
return ret;
}
とするためには、言うことができますクラスのWindows(文字列名、ダブルサイズ、ブールブラインド)私は、配列を変換
[、]へこのようなWindowsの(非常に一般的):
public static List<Windows> ToList(string[,] arra)
{
List<Windows> ret = new List<Windows>(); // change Windows to anything
int col = array.GetLength(1);
int row = array.GetLength(0);
PropertyInfo[] props=PropArray(new Windows());
int propslen=props.Length;
for (int c = 0; c < col; c++)
{
Windows entry=new Windows();
for (int r = 0; r < propslen; r++)
{
Type pt = props[r].PropertyType;
if (pt==typeof(string))
props[r].SetValue(entry,array[r,c],null);
else
if (pt==typeof(int))
{
int i=0;int.TryParse(array[r,c],out i);
props[r].SetValue(entry,i,null);
}
else
if (pt==typeof(bool))
{
bool i = false; bool.TryParse(array[r, c], out i);
props[r].SetValue(entry,i,null);
}
else
if (pt == typeof(double))
{
double i = 0; double.TryParse(array[r, c], out i);
props[r].SetValue(entry, i, null);
}
else
if (pt==typeof(DateTime))
{
DateTime i = DateTime.MinValue; DateTime.TryParse(array[r, c], out i);
props[r].SetValue(entry,i,null);
}
}
ret.Add(entry);
}
return ret;
}
私がやらなければならないことは、他のデータ型にFINDREPLACE語の「Windows」で、それが動作します。
本当の質問は、型を受け取ってインスタンスをすべてそれ自身で作成するために一般化できるのですか?
それは可能ですか?事前に
おかげで、 ガブリエル
新しいアンケートのためのアンダーズ(変更は遅すぎますが、常に新しいプロジェクトがあります)])。そして、私が探していた答えのためのmadd0! –