0

I have this:

foreach (KeyValuePair<string, decimal> update in updates)
{
salesorder.GetType().InvokeMember(update.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, salesorder, update.Value);
}

I found the example on this site but it was using a string, my example is decimal but it says cannot convert from decimal to object[]

Thanks

1 Answer 1

3

The last parameter has to be an object[], so you need to wrap the decimal:

foreach (KeyValuePair<string, decimal> update in updates)
{
    salesorder.GetType().InvokeMember(update.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, salesorder, new object[] { update.Value });
}

If you want to set values 'deeper' (as indicated in your comments):

foreach (KeyValuePair<string, decimal> update in updates)
{
    var property = salesorder.GetType().InvokeMember(update.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, Type.DefaultBinder, salesorder, new object[] { });
    property.GetType().InvokeMember("Value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, property, new object[] { update.Value });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - i'm getting this now - Method salesorder.new_operations_dealerinvoicefunder' not found. I'm trying to set the property value not call a method by that name.
Then your Key is wrong. You can use InvokeMember for properties, fields or members alike: msdn.microsoft.com/en-us/library/66btctbe(v=vs.110).aspx
I think it's because the property I need to set is a level down, i.e. salesorder.new_operations_dealerfunderinvoice.Value = value instead of salesorder.new_operations_dealerfunderinvoice = value. Is that possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.