1

I am writing a wrapper around some data library that executes sql statements and returns results.

The ExecuteQuery command should ideally return an IEnumerable<T> depending on what type of object the query results map to. The problem is that I cannot know what type this would be and therefore I would rather return a IEnumerable<dynamic> type.

This is all good so far except that I want the dynamic objects properties to be the same as the columns being queried for but I cannot find anyway to assign this, I currently have all the column names in an array col[] e.g.

dynamic obj = new {} as dynamic;
for(int i = 0; i < col.Length; i++){
    //say col[i] == 'id', I want obj.id = someValue 
    obj.? = someValue;  ???
}

I hope this is clear. I hope it's not impossible!

p.s. I am writing this for WP7 so my options are severely limited, it might be good to keep this in mind.

Thanks for you help.

nawfal
  • 70,104
  • 56
  • 326
  • 368
Obi
  • 3,091
  • 4
  • 34
  • 56
  • Thanks for the edits @DanielHilgarth, my typing was just all over the place – Obi Apr 30 '13 at 14:22
  • Similar, non-wp: [Dynamically set the property name of a C# anonymous type](http://stackoverflow.com/questions/1642733/dynamically-set-the-property-name-of-a-c-sharp-anonymous-type) – nawfal Jul 19 '14 at 19:15

1 Answers1

1

dynamic is not applicable in your case. You use dynamic when you know the property name at compile time but not the actual type that property will be defined on.

If you know the property name only at runtime you will have to use ordinary reflection, something like this:

obj.GetType()
   .GetProperty(col[i])
   .SetValue(obj, someValue);

Please note that dynamic obj = new {} as dynamic; makes absolutely no sense. This will create an anonymous type without any properties. Assigning it to a dynamic variable doesn't change the fact that it doesn't have - and never will - any properties.

If you are trying to dynamically add properties to an object, you might want to use ExpandoObject.

Usage would be like this:

IDictionary<string, object> obj = new ExpandoObject();
for(int i = 0; i < col.Length; i++)
{
    obj.Add(col[i], someValue);
}

return (dynamic)obj;
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Err, so are you saying that assuming a different scenario, if I did obj.SomeProperty = "some value"; that obj would still not have any properties? The reason I did that was so that the reference would not be null! – Obi Apr 30 '13 at 13:55
  • @ObiOnuorah: Indeed. You would simply get a `RuntimeBinderException`: *'`<>f__AnonymousType0`' does not contain a definition for 'SomeProperty'*. – Daniel Hilgarth Apr 30 '13 at 13:58
  • @ObiOnuorah: What are you trying to achieve here anyway? – Daniel Hilgarth Apr 30 '13 at 13:59
  • Also, your answer seems to assume that obj already has a property called col[i] which it doesn't hence the need for a dynamic object. I tried using an ExpandoObject but I do not get this type when I add System.Dynamic to my references....any thoughts? – Obi Apr 30 '13 at 14:00
  • Like the question says, I am writing a wrapper for a data provider. I want the consumer to be able to give me a sql select statement with columns and get back an object collection with properties matching the column names that was queried. – Obi Apr 30 '13 at 14:03
  • @ObiOnuorah: ExpandoObject should work. See also my updated answer. Just add a reference to System.Core to your project and add `using System.Dynamic;` to the top of your *.cs file. But I am not sure about Windows Phone 7 - sorry. – Daniel Hilgarth Apr 30 '13 at 14:04