c# - Shorten this reflection property name helper method? -


I want a static helper method to remove magic strings. Of course, I could pack the property method within TestContainer, which will remove the requirement to provide TestContainer as a reasoning. This is done well.

But I want to have helpful methods in a stable class, if I later decide to optimize my code and remove the method. I managed to downgrade it, but to provide a little ugly (string type, does not look very good)

Keep in mind to know any better way to "expression God", the static class should be kept universal and nothing should be known about the Test Container (otherwise it is as easy as the link provided). )

  Internal class Property Name Resolving {Internal Class Test Container {Public String LastName {Receive; Set; }} Internal Static Class Binding Helper {Public Stable String Property Name and Lieutenant; Tubact, TVL> (Expression & lt; Funk & Left; Tobagect, TVLux> PropertiesCycler) {var Members Expression = PropertiesThich. As a Member Member; If (member intent! = Null) leave the member Expression.member .name; And throw a new exception ("something went wrong"); }} Internal Static Zero Test () {var t = New TestContainer {LastName = "Hans"}; Console.light line (binding helper. Property name & lt; test container, string & gt; (x = & gt; x.LastName)); Console.ReadLine (); }}  

BTW, the output is "lastime" and it can be used to set binding.

And another question, can I safely remove the check of the tap? 3 options for you:

  • Generate and normalize the square, and use the

  • Use an example item to estimate based on the method
  • Forget the object type completely and use only one example
  • < P> All of these are shown below. null Again to security - Expression type (you can handle methods too, BTW) - Examination to check some more code Will take

      using the system; Using System.Linq.Expressions; Internal Category TestContainer {Public String LastName {get; Set; }} Fixed class program {static zero main () {var t = New TestContainer {LastName = "Hans"}; String s1 = binding helper & lt; Test Container & gt; . Property Name (x = & gt; x.ListName); String s2 = binding helper Property Name (T, X = & gt; x.ListName); String s3 = binding helper. Property Name ((= = gt; t.LastName); }} Internal Static Binding Binding Helper {Public Stabilized String Superman & Lt; Tubex, TLL & gt; (Tobuk template, expression & lt; funk & lieutenant; tubeckt, tvview & gt; property selector) {var member expansion = attribute. As a Member Member; If (member intent! = Null) leave the member Expression.member .name; And throw a new exception ("something went wrong"); } Public static string asset name & lt; TValue & gt; (Expression & lt; funk & lt; tvalu & gt; select property property) {var member expansion = attribute. As a Member Member; If (member intent! = Null) leave the member Expression.member .name; And throw a new exception ("something went wrong"); }} Internal Static Class Binding Helper & Lt; Tobias & gt; {Public static string communication name & lt; TValue & gt; (Expression & lt; funk & lieutenant; tubeckt, tvview & gt; property selector) {var member expression = property secretary. As a Member Member; If (Member Purpose! = Null) leave the member Expression.member .name; And throw a new exception ("something went wrong"); }}  

    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 -