私はValueInjecterを理解しようとしていたので、自家製の小さなORMで使用できます。DataRowとDataTableのマッピングをサポートする必要があるため、このタイプのマッパーを実装しようとしています。そして正直なところ、ドキュメントは十分ではなく、私はそれを試してみたかったのです。たぶん、Omuまたはこのライブラリの他のユーザーが答えるでしょう。
これが私のDataRowインジェクターです
public class DataRowInjection: KnownSourceValueInjection<DataRow>
{
protected override void Inject(DataRow source, object target)
{
for (var i = 0; i < source.ItemArray.Count(); i++)
{
//TODO: Read from attributes or target type
var activeTarget = target.GetProps().GetByName(source.Table.Columns[i].ToString(), true);
if (activeTarget == null) continue;
var value = source.ItemArray[i];
if (value == DBNull.Value) continue;
activeTarget.SetValue(target, value);
}
}
}
これはかなりうまくいきます。これが、DataTableにこれを実装して、IenumarableまたはIListを返す方法についての質問です。私が期待するコードスニペットはのようなものです。
public class DataTableInjector : ?????
{
protected override void Inject(DataTable source, IList<T> target) where T : class
{
// Do My Staff
foreach (var row in source.Rows)
{
target[i].InjectFrom<DataRowInjection>(row);
}
//return IList?
}
}
どうすればこれを達成できますか。ありがとうございました
~~~~~~ここに私がOmuの助けを借りて書いた完全なコードがあります
public class DataTableInjection<T> : ValueInjection where T : new()
{
protected override void Inject(object source, object target)
{
var dt = source as DataTable;
var t = target as IList<T>;
foreach (DataRow dr in dt.Rows)
{
var t2 = new T();
t2.InjectFrom<DataRowInjection>(dr);
t.Add(t2);
}
}
}