Most likely, if you need to refer to a specific field, your method is not quite so Generic. Is it really necessary for this method to be universal? If not (which is most likely), I recommend replacing the type of the data parameter with the type in which the f_version field was declared.
If you still do not like this solution, you can use one of the proposed solutions below:
a) Add a refinement to type T:
public void Save<T>(object data, string filepath) where T: BaseType
where BaseType is the type in which the f_version field is declared
b) Pass an anonymous function to the Save method.
public void Save<T>(object data, string filepath, Action<T,BF_Load> action = null) { if(action != null) { action((T)data, this); } ... }
The method itself is called as follows:
bfLoad.Save(data, filepath, (t, bf) => { bf.Version = t.f_version });
However, in this case, you will have to change the availability of the Version field.
c) Take advantage of reflection:
Version = (int)typeof(T).GetField("f_version", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(data);
However, I strongly recommend that you first revise the need for the Save method to remain universal.
T
does not contain thef_version
field? - VladD pm