反射 - 设置返回 obj 的类型? [英] Reflection - setting Type of returned obj?

查看:23
本文介绍了反射 - 设置返回 obj 的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用数据行填充不同类型的对象,相关对象的每个属性 = 到数据行中的类似名称字段.

I am populating different types objects with datarows with each attrivute of the relevant object = to the similiarly name field in the datarow.

我想使用一个通用函数来做到这一点.如何从泛型函数强制返回对象的类型.我还不知道 <T> 语法实际上意味着什么:PopulateObject 未返回类型,因为我收到编译器错误 - 无法将类型对象"隐式转换为JobCard"在下面查看我的代码

I'd like to use a generic function to do this. How do I force the Type of the return object from the generic function. I don't yet know what the <T> syntax actually means: PopulateObject<T> does not return the type as I get compiler error - Cannot implicitly convert type 'object' to 'JobCard' See my code below

public JobCard AcceptJobCard(Guid jobCardGuid, Guid userGuid)
{
    try
    {
        JobCard jc= new JobCard();
        DL_ISMS.DataSets.JobCardDS.View_JobcardDataTable dtJC = BL_ISMS.Meter.JobCard_CB.FetchJobCard(jobCardGuid);
        DL_ISMS.DataSets.JobCardDS.View_JobcardRow jcRow = dtJC[0];



        DateTime dateAccept = DateTime.Now;
        bool res = BL_ISMS.Meter.JobCard_CB.UpdatePdaJobCard(userGuid, jobCardGuid, null, null, null, JobCardStatus.Accepted.GetHashCode(), null, null, null, null, "", "", "", "", "", "", null, dateAccept, null, null, null, null, null, null);

        if (res)
        {                
            jc = PopulateObject<JobCard>(jc, jcRow);

            return jc;
        }
        else
        return jc;
    }
    catch (Exception ex )
    {
        Trace.WriteException(ex);
        throw;
    }
}

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

    Type type = dataObj.GetType();
    System.Reflection.PropertyInfo[] proplist = type.GetProperties();
    string s = "";
    foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
    {
        s += propertyitem.Name + ":" + (propertyitem.GetValue(dataObj,null)).ToString() + "\r\n";
        propertyitem.SetValue(dataObj, dataRow["propertyitem.Name"], null);
    }
    return (T)dataObj;
}

----在第二个回答后更新----

----updated after 2nd answer----

使用此代码:私有 T PopulateObject(T dataObj, System.Data.DataRow dataRow){

using this code: private T PopulateObject(T 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, PopulateObject<propertyitem.GetType()>(propertyitem, dataRow), null);
                }
                else
                throw;
            } 
    }
    return dataObj;
}

不过,我遇到了另一个障碍.其中一个属性实际上是另一个名为 Customer 的对象,它有 11 个自己的属性.我怀疑还有更多的嵌套对象仍然潜伏着.我将如何处理这些嵌套对象的填充,数据行中只有 1 个字段?

I have hit another roadblock though. One of the attributes is actually another object called Customer, with 11 attributes of it's own. I suspect there even are more nested objects still lurking. How would I handle the populating of these nested objects, for whom there is only 1 field in the datarow?.

要处理这些对象:- 我必须在父 Objdata 中提取 ChildObj 类型- 调用我传递 ChildObjType 和完整数据行的 ProcessChildObj() 函数- 并在 ProcessChildObj() 中进行名称匹配,并设置该属性?

To handle these objects: - I'd have to extract the ChildObj type in the parent Objdata - call a ProcessChildObj() function to which I pass the ChildObjType, and the complete datarow - and in ProcessChildObj() do a name match, and set that attribute?

或(如上面的代码)- 递归调用 PopulateObject.然而,这给我带来了一个问题,因为编译器抱怨我尝试将 obj 类型传递给递归调用:

or (as in the code above) - Call the PopulateObject recursively. This however presents me with a problem as the compiler complains where I try to pass the obj type into the recursive call:

propertyitem.SetValue(dataObj, PopulateObject(propertyitem, dataRow), null);//导致编译器 msg "Operator '<'不能应用于方法组"和System.type"类型的操作数"

propertyitem.SetValue(dataObj, PopulateObject(propertyitem, dataRow), null); //causes compiler msg "Operator '<' cannot be applied to operands of type 'method group' and 'System.type'"

如何提取嵌套 childObj 的类型作为参数传递?

How do I extract the type of the nested childObj to pass the type as a parameter?

推荐答案

将您的方法的签名更改为:

Change your method's signature to:

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

另外,我认为您不需要返回任何对象,因为您只更新现有对象.您可以使用:

Also, I think you don't need to return any object because your only updating an existing object. You can just use:

private void PopulateObject(object dataObj, System.Data.DataRow dataRow)

这篇关于反射 - 设置返回 obj 的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆