Guest User

Untitled

a guest
Jul 18th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. public class Job
  2. {
  3. public string job_id = "";
  4. public string description = "";
  5. public string address = "";
  6. public string details = "";
  7. }
  8.  
  9. private Job ApplyFilters(Job job_in, Job job_filters)
  10. {
  11.  
  12. Type type = typeof(Job);
  13. Job job_out = new Job();
  14. FieldInfo[] fields = type.GetFields();
  15.  
  16. // iterate through all fields of Job class
  17. for (int i = 0; i < fields.Length; i++)
  18. {
  19. List<string> filterslist = new List<string>();
  20. string filters = (string)fields[i].GetValue(job_filters);
  21.  
  22. // if job_filters contaisn magic word "$" for the field, then do something with a field, otherwise just copy it to job_out object
  23. if (filters.Contains("$"))
  24. {
  25. filters = filters.Substring(filters.IndexOf("$") + 1, filters.Length - filters.IndexOf("$") - 1);
  26. // MessageBox.Show(filters);
  27. // do sothing useful...
  28. }
  29. else
  30. {
  31. // this is my current field value
  32. var str_value = fields[i].GetValue(job_in);
  33. // this is my current filed name
  34. var field_name = fields[i].Name;
  35.  
  36. // I got stuck here :(
  37. // I need to save (copy) data "str_value" from job_in.field_name to job_out.field_name
  38. // HELP!!!
  39.  
  40. }
  41. }
  42. return job_out;
  43. }
  44.  
  45. public static void MapAllFields(object source, object dst)
  46. {
  47. System.Reflection.FieldInfo[] ps = source.GetType().GetFields();
  48. foreach (var item in ps)
  49. {
  50. var o = item.GetValue(source);
  51. var p = dst.GetType().GetField(item.Name);
  52. if (p != null)
  53. {
  54. Type t = Nullable.GetUnderlyingType(p.FieldType) ?? p.FieldType;
  55. object safeValue = (o == null) ? null : Convert.ChangeType(o, t);
  56. p.SetValue(dst, safeValue);
  57. }
  58. }
  59. }
  60.  
  61. fields[i].SetValue(job_out, str_value);
Add Comment
Please, Sign In to add comment