言いたいことを正確に伝えるのは難しいです。特定のシナリオに応じて、次のいずれかを実行できます。
メソッドに構築関数を渡すことができます。
public static void fillObject(object [] obs, DataTable dt, Func<object> builder)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
DataRow dr = dt.Rows[j];
if (obs[j] == null)
obs[j] = builder();
fillObject(obs[j], dr);
}
}
または、タイプがわかっている場合は、構築関数の辞書を使用できます。
Dictionary<Type, Func<object>> sBuilders = new Dictionary<Type, Func<object>>
{
{ typeof(Type1), () => new Type1() },
{ typeof(Type2), () => new Type2() },
// etc...
};
public static void fillObject(object [] obs, DataTable dt, Type pType)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
DataRow dr = dt.Rows[j];
if (obs[j] == null)
obs[j] = sBuilders[pType]();
fillObject(obs[j], dr);
}
}