Using expressions to get intellisense support in C#
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
c# .net expression
asked Nov 9 at 20:09
MCR
312113
312113
add a comment |
add a comment |
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");
add a comment |
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");
add a comment |
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");
add a comment |
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");
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");
edited Nov 9 at 20:31
answered Nov 9 at 20:18
Thomas Hilbert
2,6502627
2,6502627
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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