C#。使用反射设置成员对象值

发布于 2024-12-15 09:55:44 字数 1835 浏览 2 评论 0原文

我需要您的帮助来完成下面的代码。基本上我有一个名为“Job”的类,它有一些公共字段。我将两个参数“job_in”和“job_filters”传递给我的方法“ApplyFilter”。第一个参数包含实际数据,第二个参数包含指令(如果有)。我需要迭代“job_in”对象,读取它的数据,通过读取“job_filters”应用任何指令,修改数据(如果需要)并将其返回到新的“job_out”对象中。一切正常,直到我需要将数据存储在“job_out”对象中:

    public class Job
    {
        public string job_id = "";
        public string description = "";
        public string address = "";
        public string details = "";
    }

...

    private Job ApplyFilters(Job job_in, Job job_filters)
    {

        Type type = typeof(Job);
        Job job_out = new Job();
        FieldInfo[] fields = type.GetFields();

        // iterate through all fields of Job class
        for (int i = 0; i < fields.Length; i++)
        {
            List<string> filterslist = new List<string>();
            string filters = (string)fields[i].GetValue(job_filters);

            // if job_filters contaisn magic word "$" for the field, then do something with a field, otherwise just copy it to job_out object
            if (filters.Contains("$"))
            {
                filters = filters.Substring(filters.IndexOf("$") + 1, filters.Length - filters.IndexOf("$") - 1);
                // MessageBox.Show(filters);
                // do sothing useful...
            }
            else
            {
                // this is my current field value 
                var str_value = fields[i].GetValue(job_in);
                // this is my current filed name
                var field_name = fields[i].Name;

                //  I got stuck here :(
                // I need to save (copy) data "str_value" from job_in.field_name to job_out.field_name
                // HELP!!!

            }
        }
        return job_out;
    }

请帮忙。我已经看到了一些使用属性的示例,但我很确定也可以对字段执行相同的操作。谢谢!

I need your help with the following code below. Basically I have a class called "Job" which has some public fields. I'm passing to my method "ApplyFilter" two parameters "job_in" and "job_filters". First parameter contains actual data, and the second one has instructions (if any). I need to iterate through "job_in" object, read it's data, apply any instructions by reading "job_filters", modify data (if needed) and return it in a new "job_out" object. Everything works fine till i need to store my data in "job_out" object:

    public class Job
    {
        public string job_id = "";
        public string description = "";
        public string address = "";
        public string details = "";
    }

...

    private Job ApplyFilters(Job job_in, Job job_filters)
    {

        Type type = typeof(Job);
        Job job_out = new Job();
        FieldInfo[] fields = type.GetFields();

        // iterate through all fields of Job class
        for (int i = 0; i < fields.Length; i++)
        {
            List<string> filterslist = new List<string>();
            string filters = (string)fields[i].GetValue(job_filters);

            // if job_filters contaisn magic word "$" for the field, then do something with a field, otherwise just copy it to job_out object
            if (filters.Contains("$"))
            {
                filters = filters.Substring(filters.IndexOf("$") + 1, filters.Length - filters.IndexOf("$") - 1);
                // MessageBox.Show(filters);
                // do sothing useful...
            }
            else
            {
                // this is my current field value 
                var str_value = fields[i].GetValue(job_in);
                // this is my current filed name
                var field_name = fields[i].Name;

                //  I got stuck here :(
                // I need to save (copy) data "str_value" from job_in.field_name to job_out.field_name
                // HELP!!!

            }
        }
        return job_out;
    }

Please help. I've seen a few samples by using properties, but i'm pretty sure it is possible to do the same with fields as well. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-12-22 09:55:44

试试这个

public static void MapAllFields(object source, object dst)
{
    System.Reflection.FieldInfo[] ps = source.GetType().GetFields();
    foreach (var item in ps)
    {
        var o = item.GetValue(source);
        var p = dst.GetType().GetField(item.Name);
        if (p != null)
        {
            Type t = Nullable.GetUnderlyingType(p.FieldType) ?? p.FieldType;
            object safeValue = (o == null) ? null : Convert.ChangeType(o, t);
            p.SetValue(dst, safeValue);
        }
    }
}

Try this

public static void MapAllFields(object source, object dst)
{
    System.Reflection.FieldInfo[] ps = source.GetType().GetFields();
    foreach (var item in ps)
    {
        var o = item.GetValue(source);
        var p = dst.GetType().GetField(item.Name);
        if (p != null)
        {
            Type t = Nullable.GetUnderlyingType(p.FieldType) ?? p.FieldType;
            object safeValue = (o == null) ? null : Convert.ChangeType(o, t);
            p.SetValue(dst, safeValue);
        }
    }
}
多彩岁月 2024-12-22 09:55:44

fields[i].SetValue(job_out, str_value)

fields[i].SetValue(job_out, str_value)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文