Reputation: 5524
I'm preparing to pass values to a method:
private string BuildMessage(int templateID, string body, object data)
where the data param is an array of name/value pairs. To prepare my values for that data param I need to combine the properties of a strongly typed class with the values of a simple 2d array.
What's the best way to merge those values?
Upvotes: 0
Views: 5728
Reputation: 3522
You can easily get the properties and teir values via Reflection, like this:
public Dictionary<string, string> GetParameters(object data)
{
if (data == null)
return null;
Dictionary<string, string> parameters = new Dictionary<string, string>();
foreach (PropertyInfo property in data.GetType().GetProperties())
parameters.Add(property.Name, property.GetValue(data, null).ToString());
return parameters;
}
Merging the two dictionaries shouldn't need further explanation :)
Upvotes: 1