Reputation: 1316
I need to create a very simple object with a single property. The problem is that the name of this property is variable and not known until run-time.
I'm using c#.
I've been looking around and found lots of, what appear to be very long-winded ways of doing what I want (e.g. Add properties at runtime). However, this type of solution seems rather overkill so I'm really hoping there is a simpler way.
Why?
I'm working on a WPF app. In some cases, I have combo boxes that I want to cope with null values. I've already found a fantastic solution to this here: http://philondotnet.wordpress.com/2009/09/18/how-to-select-null-none-in-a-combobox-listbox-listview/
However, the major problem with this solution (as noted by the second comment) is that if the DisplayMemberPath of the Selector is set then the null value text appears blank. The way to resolve this is simply to modify NullItem to return an object with a property where the name of the property is DisplayMemberPath and the value is the string representation of the null value.
Upvotes: 2
Views: 109
Reputation: 62246
You can think about to use
Considering that ExpandoObject implements IDictionary
, you can cast it and:
var expando = new System.Dynamic.ExpandoObject();
var expandoDic= expando as IDictionary<String, Object>;
expandoDic["Name"] = "Jane";
expandoDic["Surname"] = "Eyre";
var name = expandoDic["Name"];
var surname = expandoDic["Surname"];
var book = name + " " + surname;
output of this would be
"Jane Eyre", as expected;
Upvotes: 1