2

参照:リフレクション - 返された obj のタイプを設定しますか? いくつかのプロパティを持つオブジェクト Call Jobcard があります。そのうちの 1 つは独自のプロパティを持つ Customer という別のオブジェクトで、そのうちの 1 つは Address という別のネストされたオブジェクトです。

これら 2 つの関数は、他のオブジェクト タイプも処理します。

private T PopulateObject<T>(T dataObj, System.Data.DataRow dataRow)
{

    //Type type = dataObj.GetType();
    System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();

    foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
    {
        if(propertyitem.Name != "")
        //s += propertyitem.Name + ":" + (propertyitem.GetValue(dataObj,null)).ToString() + "\r\n";
            try
            {
                propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("does not belong to table"))
                {
                   propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
                }
                else
                throw;
            } 
    }
    return dataObj;
}



private object PopulateChildObject(object dataObj, System.Data.DataRow dataRow)
{

    System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();

    foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
    {
        if(propertyitem.Name != "")
            try
            {
                propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
            }
            catch (Exception ex)
            {           
             if (ex.Message.Contains("does not belong to table"))
                {
                   propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
                }
                else
                throw;
            } 
    }
    return dataObj;
}

問題は、PropertyInfo リストが渡された childObj のリストではないため、PopulateChildObject 関数が機能しないことです。ウォッチで PopulateChildObject に渡された dataObj を見ると、属性が 0 になっています。また、PopChildObj() に渡される dataObj のタイプは、Customer ではなく System.Reflection.RuntimePropertyInfo' です。私は何が欠けていますか?

4

2 に答える 2

3

propertyitemですPropertyInfo; プロパティからの値を渡す必要があります-つまり

propertyItem.GetValue(dataObj, null);

この子オブジェクトが親に​​よって作成されている場合は、「設定」する必要はありません。下にあるオブジェクトを更新するだけです。

PopulateChildObject(propertyitem.GetValue(dataObj, null), dataRow);

子オブジェクト(通常は)を作成する必要がある場合あります。その場合は、次を呼び出す必要があります。Activator.CreateInstanceSetValue

object child = propertyitem.GetValue(dataObj, null);
if(child == null) {
    child = Activator.CreateInstance(propertyitem.PropertyType);
    propertyitem.SetValue(dataObj, child, null);
}
PopulateChildObject(child, dataRow);

PopulateObjectでも、との間に本当に違いはあるのだろうPopulateChildObjectか?それらは同じものである可能性があるように感じますか?

于 2009-05-26T09:27:24.913 に答える
0

Nest プロパティを取得します。例: Developer.Project.Name

private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName)
            {
                if (t.GetType().GetProperties().FirstOrDefault(p => p.Name == PropertName.Split('.')[0]) == null)
                    throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString()));
                if (PropertName.Split('.').Length == 1)
                    return t.GetType().GetProperty(PropertName);
                else
                    return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1]);
            }
于 2013-11-26T18:36:40.533 に答える