I searched the search queries, trying to find a way to call Control.DataBindings.Add , not using a string literal, but getting the property name from the property itself, which, I think, would be less error prone, at least for my particular case, since I usually let Visual Studio do the renaming when renaming the property. Thus, my code will look like DataBindings.Add(GetName(myInstance.myObject)... instead of DataBindings.Add("myObject"... So I found this:
static string GetName<T>(T item) where T : class { var properties = typeof(T).GetProperties(); if (properties.Length != 1) throw new Exception("Length must be 1"); return properties[0].Name; }
This will be called if I have a property called One , this way: string name = GetName(new { this.One }); that would give me "One" . "One" I do not know why this works and whether it is safe to use. I donβt even know what this new { this.One } means. And I do not know in what case it could happen that properties.Length not 1.
By the way, I just tested to rename my One property to Two , and Visual Studio turned new { this.One } into new { One = this.Two } , which when used with the GetName function gave me "One" , which does everything this is useless since the name that I am switching to Control.DataBindings.Add will still be βoneβ after the property is renamed.
source share