Using expressions to get intellisense support in C#









up vote
0
down vote

favorite
1












I'm trying use expressions in order to have strong typing over properties.



So I have this model.



public class Entity

public int Id get; set;
public string Name get; set;



And this method which should, well in the end I would like to use the properties in some way but for now I just want to return them.



public List<string> DoSomething<TEntity>(params Expression<Func<TEntity, object>> expressions)

List<string> props = new List<string>();

foreach (var expression in expressions)

var memberExpression = expression.Body as MemberExpression;

var q = memberExpression.Member.Name;

props.Add(q);

return props;



This is the usage



var props = DoSomething<Entity>(x => x.Id, x => x.Name);


Well, It works but only partially. What I mean by that is that it will work for reference types, for example it will work for Name property because it is a reference type, but it will return null for any value type, in this case for ID which is an int.



Why is that and what is the solution?










share|improve this question

























    up vote
    0
    down vote

    favorite
    1












    I'm trying use expressions in order to have strong typing over properties.



    So I have this model.



    public class Entity

    public int Id get; set;
    public string Name get; set;



    And this method which should, well in the end I would like to use the properties in some way but for now I just want to return them.



    public List<string> DoSomething<TEntity>(params Expression<Func<TEntity, object>> expressions)

    List<string> props = new List<string>();

    foreach (var expression in expressions)

    var memberExpression = expression.Body as MemberExpression;

    var q = memberExpression.Member.Name;

    props.Add(q);

    return props;



    This is the usage



    var props = DoSomething<Entity>(x => x.Id, x => x.Name);


    Well, It works but only partially. What I mean by that is that it will work for reference types, for example it will work for Name property because it is a reference type, but it will return null for any value type, in this case for ID which is an int.



    Why is that and what is the solution?










    share|improve this question























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I'm trying use expressions in order to have strong typing over properties.



      So I have this model.



      public class Entity

      public int Id get; set;
      public string Name get; set;



      And this method which should, well in the end I would like to use the properties in some way but for now I just want to return them.



      public List<string> DoSomething<TEntity>(params Expression<Func<TEntity, object>> expressions)

      List<string> props = new List<string>();

      foreach (var expression in expressions)

      var memberExpression = expression.Body as MemberExpression;

      var q = memberExpression.Member.Name;

      props.Add(q);

      return props;



      This is the usage



      var props = DoSomething<Entity>(x => x.Id, x => x.Name);


      Well, It works but only partially. What I mean by that is that it will work for reference types, for example it will work for Name property because it is a reference type, but it will return null for any value type, in this case for ID which is an int.



      Why is that and what is the solution?










      share|improve this question













      I'm trying use expressions in order to have strong typing over properties.



      So I have this model.



      public class Entity

      public int Id get; set;
      public string Name get; set;



      And this method which should, well in the end I would like to use the properties in some way but for now I just want to return them.



      public List<string> DoSomething<TEntity>(params Expression<Func<TEntity, object>> expressions)

      List<string> props = new List<string>();

      foreach (var expression in expressions)

      var memberExpression = expression.Body as MemberExpression;

      var q = memberExpression.Member.Name;

      props.Add(q);

      return props;



      This is the usage



      var props = DoSomething<Entity>(x => x.Id, x => x.Name);


      Well, It works but only partially. What I mean by that is that it will work for reference types, for example it will work for Name property because it is a reference type, but it will return null for any value type, in this case for ID which is an int.



      Why is that and what is the solution?







      c# .net expression






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 20:09









      MCR

      312113




      312113






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Expression<Func<TEntity, object>> is an Expression that builds a Func which returns an object (which is a reference type).
          If you pass x => x.Id as value of that Expression, the return type of the resulting Func will not match, as int is a value type but the Func is expected to return a reference type.



          C#'s compiler will see that and automatically build a Func that wraps the int inside an object (called boxing). But that Func no longer has a simple MemberExpression as its body, because the MemberExpression has to be wrapped in a "boxing expression".



          That's basically what happens. So you have to handle the case where expression.Body is not of type MemberExpression to fix that.



          This is untested, but it might help:



          if (!(expression.Body is MemberExpression))

          if (expression.Body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.TypeAs)
          expression = unaryExpression.Operand as MemberExpression;
          else
          expression = null;
          if (expression == null)
          throw new ArgumentException("something happened");






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232668%2fusing-expressions-to-get-intellisense-support-in-c-sharp%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Expression<Func<TEntity, object>> is an Expression that builds a Func which returns an object (which is a reference type).
            If you pass x => x.Id as value of that Expression, the return type of the resulting Func will not match, as int is a value type but the Func is expected to return a reference type.



            C#'s compiler will see that and automatically build a Func that wraps the int inside an object (called boxing). But that Func no longer has a simple MemberExpression as its body, because the MemberExpression has to be wrapped in a "boxing expression".



            That's basically what happens. So you have to handle the case where expression.Body is not of type MemberExpression to fix that.



            This is untested, but it might help:



            if (!(expression.Body is MemberExpression))

            if (expression.Body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.TypeAs)
            expression = unaryExpression.Operand as MemberExpression;
            else
            expression = null;
            if (expression == null)
            throw new ArgumentException("something happened");






            share|improve this answer


























              up vote
              3
              down vote



              accepted










              Expression<Func<TEntity, object>> is an Expression that builds a Func which returns an object (which is a reference type).
              If you pass x => x.Id as value of that Expression, the return type of the resulting Func will not match, as int is a value type but the Func is expected to return a reference type.



              C#'s compiler will see that and automatically build a Func that wraps the int inside an object (called boxing). But that Func no longer has a simple MemberExpression as its body, because the MemberExpression has to be wrapped in a "boxing expression".



              That's basically what happens. So you have to handle the case where expression.Body is not of type MemberExpression to fix that.



              This is untested, but it might help:



              if (!(expression.Body is MemberExpression))

              if (expression.Body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.TypeAs)
              expression = unaryExpression.Operand as MemberExpression;
              else
              expression = null;
              if (expression == null)
              throw new ArgumentException("something happened");






              share|improve this answer
























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                Expression<Func<TEntity, object>> is an Expression that builds a Func which returns an object (which is a reference type).
                If you pass x => x.Id as value of that Expression, the return type of the resulting Func will not match, as int is a value type but the Func is expected to return a reference type.



                C#'s compiler will see that and automatically build a Func that wraps the int inside an object (called boxing). But that Func no longer has a simple MemberExpression as its body, because the MemberExpression has to be wrapped in a "boxing expression".



                That's basically what happens. So you have to handle the case where expression.Body is not of type MemberExpression to fix that.



                This is untested, but it might help:



                if (!(expression.Body is MemberExpression))

                if (expression.Body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.TypeAs)
                expression = unaryExpression.Operand as MemberExpression;
                else
                expression = null;
                if (expression == null)
                throw new ArgumentException("something happened");






                share|improve this answer














                Expression<Func<TEntity, object>> is an Expression that builds a Func which returns an object (which is a reference type).
                If you pass x => x.Id as value of that Expression, the return type of the resulting Func will not match, as int is a value type but the Func is expected to return a reference type.



                C#'s compiler will see that and automatically build a Func that wraps the int inside an object (called boxing). But that Func no longer has a simple MemberExpression as its body, because the MemberExpression has to be wrapped in a "boxing expression".



                That's basically what happens. So you have to handle the case where expression.Body is not of type MemberExpression to fix that.



                This is untested, but it might help:



                if (!(expression.Body is MemberExpression))

                if (expression.Body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.TypeAs)
                expression = unaryExpression.Operand as MemberExpression;
                else
                expression = null;
                if (expression == null)
                throw new ArgumentException("something happened");







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 9 at 20:31

























                answered Nov 9 at 20:18









                Thomas Hilbert

                2,6502627




                2,6502627



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232668%2fusing-expressions-to-get-intellisense-support-in-c-sharp%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Syphilis

                    Darth Vader #20