First and primarily, a general advice: do not use empty catch clauses in your try
-catch
constructs (with the exception of so-called vexing exceptions, but i digress). If you try your hardest to ignore and discard any exception your code throws unseen, you will have a very hard time knowing what's going wrong with your code.
If you were to print out any caught exception instead of silently discarding it, the exception would tell you what is going wrong with your code (i.e., what needs to be fixed in your code).
Anyways, with this line:
tmp.GetType().GetProperty(pi.Name).SetValue(...)
you are trying to obtain a property info from your CodeRecord class (tmp is an instance of CodeRecord). Now, look at your CodeRecord class. There are no properties defined in CodeRecord, only fields. If you want to populate the fields in CodeRecord through reflection, you will need to get a FieldInfo
and not a PropertyInfo
:
tmp.GetType().GetField(pi.Name).SetValue(tmp, pi.GetValue(obj, null));
Some unrelated observation:
What the heck is Activator.CreateInstance(Type.GetType(typeof(T).ToString()))
??? You have the type object for T via typeof(T)
. You then get its name string. With its name string you try to find the type object of T again which you already had with typeof(T)
. I don't know what that is, but me thinks you need some sleep ;-P You could just have used Activator.CreateInstance(typeof(T))
. Well, hold on, i take that back, you could just have used Activator.CreateInstance<T>()
.