Survey - Who here uses System.Linq.Expressions to build Lambdas?
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
Yes I have, yes I do, basic example;
public Society FindById(int id)
{
return this.FirstOrDefault(item => item.Id.Equals(id));
}Rhys "Technological progress is like an axe in the hands of a pathological criminal" "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
-
Yes I have, yes I do, basic example;
public Society FindById(int id)
{
return this.FirstOrDefault(item => item.Id.Equals(id));
}Rhys "Technological progress is like an axe in the hands of a pathological criminal" "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
You're not technically using System.Linq.Expressions. You're using the compiler to generate an anonymous method of a specific signature. It isn't the same thing.
-
You're not technically using System.Linq.Expressions. You're using the compiler to generate an anonymous method of a specific signature. It isn't the same thing.
You are right, I'm using System.Linq but it's been a very long week... and it was a bank holiday day off on Monday :zzz:
Rhys "Technological progress is like an axe in the hands of a pathological criminal" "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
-
You are right, I'm using System.Linq but it's been a very long week... and it was a bank holiday day off on Monday :zzz:
Rhys "Technological progress is like an axe in the hands of a pathological criminal" "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Np.
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
Cool snippet. I'll hopefully find a way to use something similar in the near future. I haven't used Expressions to build lambdas, but I have used CodeDom a lot, and it is about 17,000 times uglier than the code you pasted. I feel dirty even thinking about it.
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
I use
System.Linq
extensively, but notSystem.Linq.Expressions
. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
-
Cool snippet. I'll hopefully find a way to use something similar in the near future. I haven't used Expressions to build lambdas, but I have used CodeDom a lot, and it is about 17,000 times uglier than the code you pasted. I feel dirty even thinking about it.
If using Expressions was easier, would you likely use it over CodeDom? If not, what is the reason for your usage of CodeDom vs. Expressions?
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
I feel dirty for admitting that I have. There's a piece of code that we have that does this for generating messaging layers based on config that we pass in - it's a nasty piece of code that seemed cool at the time - now I'm afraid to touch it because of what it could break.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
Occasionally, especially when I need fast Reflection.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein -
Never. I have some System.Reflection.Emit code for dynamically generating statically typed delegates (for attaching to things), and lots of reflective stuff, as I wrote a language interpreter which needs to do all that stuff at runtime.
(Same question I asked Phil) If using Expressions was easier, would you likely use it over Emit? If not, what is the reason for your usage of Emit vs. Expressions?
-
I feel dirty for admitting that I have. There's a piece of code that we have that does this for generating messaging layers based on config that we pass in - it's a nasty piece of code that seemed cool at the time - now I'm afraid to touch it because of what it could break.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
*grin* It is nasty because the code is a tangled mess itself or is it just the interconnectedness of it all?
-
Yes I have, yes I do, basic example;
public Society FindById(int id)
{
return this.FirstOrDefault(item => item.Id.Equals(id));
}Rhys "Technological progress is like an axe in the hands of a pathological criminal" "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
That seems perfectly fine to me.
Software Kinetics Dependable Software
-
*grin* It is nasty because the code is a tangled mess itself or is it just the interconnectedness of it all?
A bit of both really. It replaced a CodeDOM module that we had - the problem is, we attempted to go feature for feature with the CodeDOM stuff, rather than taking the time to revisit the whole architecture. Now it's so convoluted that I don't want to touch it. Sooner or later we'll have to bite the bullet, but there are other more pressing things to work on first, and I really don't want to divert resources to this just in case they become suicidal.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
(Same question I asked Phil) If using Expressions was easier, would you likely use it over Emit? If not, what is the reason for your usage of Emit vs. Expressions?
-
I wrote this code under .Net 1.1. I'm not sure Expressions can build delegates, though, can it? And yeah I'd use anything over Emit that did the same job. Writing out IL opcodes is not at all fast or easy!
Yes, Expressions can build delegates (typed even). Take a look at this[^]. The reason for this survey is that I've developed an open source API to make expressions easier. I'm just trying to gauge how useful it would be to the community at large. Once I've added some unit tests, I'll put together an article and post it on CodeProject. :D
-
Yes, Expressions can build delegates (typed even). Take a look at this[^]. The reason for this survey is that I've developed an open source API to make expressions easier. I'm just trying to gauge how useful it would be to the community at large. Once I've added some unit tests, I'll put together an article and post it on CodeProject. :D
I'll certainly take a look at it.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
If using Expressions was easier, would you likely use it over CodeDom? If not, what is the reason for your usage of CodeDom vs. Expressions?
The short answer is that I couldn't figure out a way for Expressions to write out c# code to a file.
-
I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?
// Add the following directive to the file:
// using System.Linq.Expressions;// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
Expression.Switch(
switchValue,
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Default")
),
new SwitchCase[] {
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("First")
),
Expression.Constant(1)
),
Expression.SwitchCase(
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("Second")
),
Expression.Constant(2)
)
}
);// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda(switchExpr).Compile()();// This code example produces the following output:
//
// DefaultUpdate: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?
I tried something like that, but failed, so I bought a book on LINQ and haven't had a chance to read it yet. That is some confusing stuff.
-
The short answer is that I couldn't figure out a way for Expressions to write out c# code to a file.
You needed to write it out to a file?