Click here to Skip to main content
16,022,368 members

Comments by Max Power Sr (Top 4 by date)

Max Power Sr 10-Feb-11 11:52am View    
Oops. There were two lines ahead of what I posted above.

public void CopyInto(Coefficients rhs)
{

The "this" object is getting a copy of the fields in rhs. "This" is of type Coefficients. The name "rhs" came from the example that dasblinkenlight posted. Which was a copy constructor and would have resulted in code that had a right hand side.

In my object I have declared some fields:
string m_Coeff0, m_Coeff1, etc...

Then I wrote some methods, such as:

[System.ComponentModel.Bindable(true)]
public string Coeff0
{ get { return m_Coeff0; } set { m_Coeff0 = value; OnPropertyChanged("Coeff0"); } }

This creates methods set_Coeff0 and get_Coeff0. So I know they will exist. The methods have to be called to update the fields so that the TextBox GUI items that are sync'd via a call to DataBinding.Add will be updated.

What am I trying to do?

I have a DSP target that is running control algorithms. Each controller will have 40+ parameters that can be changed to affect the controller. There are at least 5 controllers running on the DSP.

So I am writing a PC app to allow someone to load/save these parameters from disk and send them to the target (via CAN bus). The user can also change the parameters. The GUI is a tab control, with each tab having 40 or so TextBox items.

I needed a way to synchronize the on screen GUI text box items with values in memory. That is why I use DataBinding. For each TextBox item I add to the GUI, I set the "Tag" property to the same name so I can bind the GUI objects to the appropriate Get/Set method automatically without having to write DataBinding.Add 40+ times.

If I had to manually copy, set, configure, etc... each parameter the amount of work, and chance for copy/paste type bugs, increases. I am looking for ways to automate.

This was all pretty straightforward up until I read the data from disk and the deserialize method creates a new object. You lose the DataBinding. I could have just DataBinding.Add again but it throws an exception because the TextBox items still are bound to the old data object. You can delete them but it just seemed like I should be able to copy objects and not lose the DataBinding lol

Again, thanks for the help!
Max Power Sr 10-Feb-11 11:17am View    
Thanks to both of you for the help. I learned a lot today.

I should have rephrased the question at a higher level and not pushed this in the direction of reflection. However, here we are lol

This is what I did and it works. I am not sure this is the best/safest solution but here goes...



// Get class
Type myType = typeof(Coefficients);

// Get Properties of that class
PropertyInfo[] myClassProperties = myType.GetProperties();

MethodInfo[] myClassMethods = myType.GetMethods();

foreach (MethodInfo mi in myClassMethods)
{
String fullMethodName = mi.Name;
char[] seps = { '_' };
String[] methodName = fullMethodName.Split(seps);
if (methodName[0] == "set")
{
string newMethodName = "m_" + methodName[1];
FieldInfo fi = myType.GetField(newMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
object[] o = {fi.GetValue(rhs)};
mi.Invoke(this, o);
}
}
Max Power Sr 10-Feb-11 10:38am View    
Thanks!

First off, I don't necessarily want to go with reflection. I am just looking for some solution to my problem.

Now that I have a list of methods, how do I know which method to call and how do I match which field (from the new object I created from deserialization) that goes with it? If I have to search for specific method names, then it defeats the purpose of trying to write generic code to update all of my fields.

I have used the code you posted and I am looking at the results to see if there is a way to do what I want.

Thanks again for the help
Max Power Sr 10-Feb-11 10:04am View    
The code is nice and simple. I had seen reflection as a solution but the examples I saw were much more complicated. Thanks!

I did not implement the code as a copy constructor as I do not want to create a new class. I just use this code to copy the fields from a new class into my existing class.

I had to use BindingFlags.NonPublic only because I have a member defined:

public event PropertyChangedEventHandler PropertyChanged;

and I don't want to overwrite that.


The problem I am having now is that after I load the new serialzed data into my existing object, the binded TextBox objects are not being updated. This is because reflection does not go through the normal get/set methods.


Thanks