9

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!

Gary
  • 143
  • 1
  • 2
  • 6

2 Answers2

14

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);
        }
    }
}
devklick
  • 2,000
  • 3
  • 30
  • 47
DeveloperX
  • 4,633
  • 17
  • 22
  • 4
    You probably don't need the second null check on 'o'. There's some nice defensive coding here, though. – JRoughan Nov 13 '11 at 18:19
  • 1
    @JRoughan Is right; you're handling the case of `o == null` in your assignment of `safeValue`, so there's no need to check above. – Adam Robinson Nov 13 '11 at 18:22
11
fields[i].SetValue(job_out, str_value);
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
JRoughan
  • 1,635
  • 12
  • 32