c# - Extracting Property Names For Reflection, with Intellisense and Compile-Time Checking -


Ok so I have some code that some controls on WinForm have certain properties in the object for some properties in the object Controls so that some things happen on the data. All well and good, the problem is not problem issue, to add items to mapping, I call a function that looks like this:

  this.AddMapping (this. MyControl, myObject, "myObjectPropertyName");  

The problem I get is that to tell the time, it is difficult to tell the difference between the top line and the bottom:

  AddMapping (this.myControl, myObject, "myObjectPropretyName");  

Since the last parameter is a string, there is no timed probe or anything that will apply that string actually corresponds to the valid property name on the given object. Additionally, things like refactors and "find all references" emerge out of such context, as a result the family name changes when the name of the property changes, so what do I think if there is any way to change the function , Which I am passing, then still the name of a string represents the name of the property in some way, but at the time of compiling the actual value, The person said that I can do this with expressions trees, but I have read on them and do not see the connection. I would love to do something like this:

  this.addmapping (this .myControl, myObject, myObject.myObjectPropertyName);  

or even

  this.addmapping (this.myControl, myObject.myObjectPropertyName);  

Sweet!

Any thoughts?

3.5, there is a way to specify member names in the form of expression code; You may have:

  Public Zero AddMapping & lt; TObj, TValue & gt; (Control myControl, TOBj myObject, Expression & lt; Func & lt; TObj, TValu & gt; & gt; Mapping) {...}  

and then expression to get the value Parse the tree. A small disabled, but not too bad.

Here is the example code:

  Public Zero AddMapping & lt; TSource, TValuue & gt; (Control control, TSource source, expression & lt; Func & lt; TSource, TValue & gt; & gt; mapping) {if (mapping.bid.NodeType! = Expression typeemember entry) {new invalid operation exception;); } Member Experience Me = (Member Experiment) Mapping Body; If (my .expression! = Mapping parameter [0]) (new invalid operation throw exception); } String name = me.Member.Name; // TODO: Do something with "control", "source" and "name", // maybe even using "me.member") called with  

Gone:

  AddMapping (myControl, foo, F = & gt; F.BR);  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -