|
Thanks. I may create it anyway since it's not part of microsoft's framework, but it depends if their input language is a C# subset like mine is.
Real programmers use butterflies
|
|
|
|
|
Are you looking for something like that?
var product = CSScript.CreateFunc<int>(@"int Product(int a, int b)
{
return a * b;
}");
int result = product(3, 4);
The solution (cs-script) is based on Roslyn engine.
|
|
|
|
|
How about the following?
static void Main()
{
Expression<Func<int, int>> exp = x => 1 + x;
} exp.Body is now of the type SimplyBinaryExpression , with a ConstantExpression for Left and PrimitiveParameterExpression for Right , etc.
That's the only way I can think of turning code into Expressions.
If you can parse an actual string in .NET, I don't know.
Apparently there's a library that does it: Dynamic Expressions · zzzprojects/System.Linq.Dynamic Wiki · GitHub[^]
|
|
|
|
|
Cool, maybe I'll write it then. I already have a C# subset parser.
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: Cool, maybe I'll write it then. Me: "There's a library that does that."
You, a typical programmer (in this regard): *Sigh* "Fine, I'll do it myself!"
|
|
|
|
|
LOL
I've been looking at one, I'm just not sure if I like it.
Real programmers use butterflies
|
|
|
|
|
Here's one simple trick and developers hate it!
string theCodez = "1 + x;";
var exp = (Expression)theCodez; True story.
|
|
|
|
|
wow. just... wow.
Real programmers use butterflies
|
|
|
|
|
I just thought of something.
If you could ever find that eval-like function, you could do something like Eval($"Expression<Func<int, int>> exp = {myCode};"); and the exp variable would be available outside the Eval function in your current scope
I remember the context in which I saw that function.
The customer wanted to do stuff like enter "amount * 10", sort of a calculator utility inside the application.
My coworker was like, "I could write a parser and spend a few days, or I can do it like this and be done with it"
I should mention it was VB.NET though and I think it was somewhere inside a VB specific namespace (although I'm not sure).
I think I saw it work with C# once too.
The fact that I can't find this function anywhere is really pissing me off (not that I'd want to use it)
I did find some smart guy who just compiled a string using reflection (using ICodeCompiler.CompileAssemblyFromSource) and then simply invokes it
|
|
|
|
|
Sander Rossel wrote: My coworker was like, "I could write a parser and spend a few days, or I can do it like this and be done with it
I can write an expression parser in about a day, but it would take forever to test
Sander Rossel wrote: I did find some smart guy who just compiled a string using reflection (using ICodeCompiler.CompileAssemblyFromSource) and then simply invokes it
I actually considered that approach. Or using Roslyn.
I think I'll avoid the Eval function, wherever it is. It probably compiles code anyway.
Real programmers use butterflies
|
|
|
|
|
|
Sander Rossel wrote: So to hell with unit tests and back to some good old manual testing and praying for the best
That's the spirit! Liberating isn't it? What's a good program without a few bugs anyway?
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: What's a good program without a few bugs anyway? A very short program
|
|
|
|
|
CS0030 Cannot convert type 'string' to 'System.Linq.Expressions.Expression'
|
|
|
|
|
Yes.
That was a joke, there's no way a string can simply be cast to an Expression
|
|
|
|
|
|
|
That was my plan, but before I did it I wanted to see if it was done.
My parser spits out CodeDOM constructs, so it needs some retooling to work with expressions.
Real programmers use butterflies
|
|
|
|
|
So not sure if this is what your looking for but I used the System.Linq.Expressions namespace
to build a dynamic link query to be executed against EF.
here is an incomplete sample for syntax example:
propertyToUse = workingProperty.Substring(0, workingProperty.IndexOf('.'));
Type propertyToUseType = GetEntityType(propertyToUse, incomingParentType);
ParameterExpression propertyToUseParameterExpression = Expression.Parameter(propertyToUseType, propertyToUse.Substring(0,1));
Expression parentExpression = Expression.Property(workingExpression, propertyToUse);
if (parentExpression.Type.IsGenericType &&
typeof(IEnumerable<>)
.MakeGenericType(parentExpression.Type.GetGenericArguments())
.IsAssignableFrom(parentExpression.Type))
{
Expression childExpression = BuildPropertyExpression(propertyToUseParameterExpression,
workingProperty, comparisonOperation, compareValue);
Type func = typeof(Func<,>);
Type genericFunc = func.MakeGenericType(propertyToUseType, typeof(bool));
LambdaExpression predicate =
Expression.Lambda(genericFunc, childExpression, propertyToUseParameterExpression);
MethodInfo asQueryableMethod = typeof(Queryable).GetMethods()
.Where(m => m.Name == "AsQueryable")
.Single(m => m.IsGenericMethod)
.MakeGenericMethod(propertyToUseType);
Expression asQueryableExpression = Expression.Call(null, asQueryableMethod, parentExpression);
MethodInfo anyMethod = typeof(Queryable).GetMethods()
.Where(m => m.Name == "Any")
.Single(m => m.GetParameters().Length == 2)
.MakeGenericMethod(propertyToUseType);
returnValue = Expression.Call(
null,
anyMethod,
asQueryableExpression,
predicate);
}
There are other factories off the Expression class to help create parts this snip it does not show like
valueExpression = Expression.Constant(compareValue)
and
returnValue = Expression.Equal(workingExpression, valueExpression);
Hope this helps
Buckrogerz
|
|
|
|
|
I did it this way (and I an NOT a JavaScript programmer):
1. JavaScript code:
class JsMath
{
static function Eval(expression: String): double
{
return eval(expression);
};
}
2. Compile:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\jsc.exe /t:library jsMath.js
3. Use in C#:
private static double ExpressionValue(string expr)
{
return JsMath.Eval(expr);
}
|
|
|
|
|
well I suppose that's one way to do it.
Real programmers use butterflies
|
|
|
|
|
Very lazy way
|
|
|
|
|
When deep in the pit of "why won't that button move up just a bit" and googling solution awash with divs, css margins and other cave drawings, I think there must be a way to grab elements from a tool bar and place them where I want then have compiler make it all just work.
I found one in WYSIWYG Web Builder and was able more or less to do what I had envisioned.
So, all you top gun web devs, do you use this stuff or hand roll it still because?
TIA
|
|
|
|
|
No. I use VS (various versions, or occasionally VS Code for PHP development) but NEVER use it in Source / Split view. When I need to tweak / debug CSS I just load the page in Chrome and use dev tools to inspect the applied CSS, add rules, adjust values, and even tinker with the HTML. Then copy/paste changes back to the source. The downside with that is I have to remember all the elements where I changed any CSS values; to make this manageable I tend to work in very small "increments" updating the source CSS / HTML frequently and reloading the page (all very well, of course, unless the page is the result of a complex or non-repeatable transaction).
But then I'm a technophobe; I tend to shun frameworks and a plethora of "tools". My trustiest tool is Notepad (the original version, not ++ or any other enhanced version).
|
|
|
|
|
DerekT-P wrote: Then copy/paste changes back to the source. The downside with that is I have to remember all the elements where I changed any CSS values; to make this manageable I tend to work in very small "increments" updating the source CSS / HTML frequently and reloading the page
You may want to explore the Filesystem tab in Chrome dev tools: it'll save your changes to the original CSS file.
Might just be for when you're working on a localhost webserver, but I've found it pretty useful.
|
|
|
|