lambda - What's the best way to define & access selected properties in C#? -


From

, I try to centralize the domain model by adding some silly logic to the domain interface. However, I am getting some problems which need to be included or removed by applying some properties.

Actually, I can use Expression Tree as the following code. Still, I do not like it because I need to define local variables ("u") when creating lambda expressions. Do you have any source code which is less than me? Apart from this, I need some method to make quick use of the chosen properties.

  Include public zones & lt; T & gt; (Parameter expression & lt; Funk & lt; IUser, Object & gt; & gt; [] Selected properties) Include {// some logic store parameters} Properties & lt; IUser & gt; (U = & gt; UID, U = & gt; u.LogOnName, u = & amp; UT; U.Whited Password);  Thank you,    

lambdass Great for many scenarios - but if you do not want them, then maybe do not use them? I hate to say this, but try simple strings, especially for data binding scenarios. If you want fast access, you can see the hyper descriptor, or there are ways to compile a representative for the property's accessories, or you can create a expression from the string and make it If you want a compiled signature (instead of calling object if you (very slow) DynamicInvoke ), instead of calling it.

Of course, in most cases raw reflection is fast enough, and there is no obstacle.

I recommend starting with simple code, and it is very slow actually before worrying it quickly. If it is not very slow, then do not change it. Any of the above options will work otherwise


Another idea; If you are using Expression , then you can do something like this:

  Include public zones & lt; T & gt; (Expression & lt; Funk & lt; T, Object & gt; Selected Properties) Include certain arguments to store the parameters) & lt; IUser & gt; (U = & new; new {u.ID, u.LogOnName, u.HashedPassword});  

And then take different expressions? A little bit, at least ... showing some sample code decontrons here:

  include the public static zero in the & lt; T & gt; (Expression & lt; funk & lt; t, object & gt; selected property) {NewExpression ne = Selected Properties. As a new expedition; If (ne == null) new invalid operation throw exception ("Object Constructor Expected"); Foreign Language (ne.Arguments in Expression Arg) {Member Expression Me = ARP as Member; If (= = 0 zero = my expression! = Selected properties. Parameter [0]) throw new invalid operation exception ("object constructor logic should be a direct member"); Console.light line ("access:" + me.Member.Name); }} Fixed Zero Main () {Include Properties & lt; IUser & gt; (U = & new; new {u.ID, u.LogOnName, u.HashedPassword}); }  

Once you know MemberInfo s (above me.Member ), then enter your own Lambda should make little trivial For example (including object to get any signature):

  var param = Expression.Parameter (typef (t), "x"); Var member entry = expression. Meckmember Entrance (Ultimate M.Member); Var body = expression. Convert (member access, typef (object)); Var lambda = expression. Black & lt; Func & lt; T, Object & gt; & Gt; (Body, ultimate); Var func = lambda.Compile ();  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -