0

I want to pass a property name as an argument:

   protected T findControl<T>(string myProperty, string myPropertyValue , UITestControl control = null) where T : UITestControl, new()
    {
        var uiContainer = control ?? Window;
        return uiContainer.SearchFor<T>(new { myProperty = myPropertyValue });
    }

 public static T SearchFor<T>(
        this UITestControl control,
        dynamic searchProperties,
        dynamic filterProperties = null) where T : UITestControl, new() 

I use:

return findControl<HtmlComboBox>("id", "PersonComboBox")

When debuging, I get:

dynamic searchProperties = {myProperty = PersonComboBox}

what, I would like to is:

dynamic searchProperties = {id = PersonComboBox}

Why is that so? Is there a way to fix that?

Peter
  • 41
  • 1
  • 9
  • Why not just use a `Dictionary`? – Andrew Sun Nov 18 '15 at 15:27
  • There is a way to go from `()=> foo.Title` to `"Title"` as a string. See http://stackoverflow.com/q/5092387/380384. This allows you not to hard code property names, but lets the runtime finds them based on the reference parameter of the function. – John Alexiou Dec 01 '15 at 19:27

2 Answers2

1

Agree with Andrew Sun - dynamics is not very popular feature and it's only usage is dealing with COM interop or with special APIs such Newton.Json,MongoConnector (where it's not very popular too - most developers prefer their Dictionary overload).

If you want impress something dynamic in .net - best way use collections and containers that are mostly close to JS object behavior.

Mostly common used classes for this task is - Dictionary<string,object> (almost exactly same thing as JS object) or Dictionary<string,string> (if it's really string only map and no nesting).

If you must provide nesting - you still can use Dictionary<string,object>, but for some scenarios XElement could be better choice.

I not suggest to use Newton.JSON without large reasone because it's addition dependency and is kind of swiss-knife - you will just use 1% of services it provide.

When think that dynamics are good - remember - it's just hack with not efficient implemenation and it cause CSharp dependency for project and overheat with runtime compilation. I and i think many other people not suggest use them instead of very special cases.

comdiv
  • 865
  • 7
  • 26
0

I also agree with my previous speakers that using a Dictionary might be an easier solution here, but if you still want to use dynamics here, you could try the following:

protected T findControl<T>(string myProperty, string myPropertyValue, UITestControl control = null) where T : UITestControl, new()
{
  var uiContainer = control ?? Window;
  // create an expando object here and reference it as dictionary
  IDictionary<string, object> searchProperties = new ExpandoObject();
  // now add your data to the dictionary
  searchProperties.Add(myProperty, myPropertyValue);
  // call your method with your newly created expando object,
  // in your method, you can now call "searchProperties.id"
  // and also your debug view (open the "Dynamic View" item in VS debugger)
  // should show a property called "id"
  return uiContainer.SearchFor<T>(searchProperties);
}
pgenfer
  • 597
  • 3
  • 13