Ok, here is the problem:
int a = 111;
int b = 222;
Expression<Func<int>> expr = ()=> someClass.SomeWork(a) + b + 1;
As you see, there is 3 different arguments: someClass, a, b. They are all from another scope of execution, but one isn't. How I can get them? I mean, in general, I want only variables of outter scope.
For example, I want to use it like this:
var result = InvokeAndLog(expr);//this will invoke expression and print out everything I need from these arguments.
Ok, folks, I found answer myself. And here it is:
internal class Program
{
public static int Method1()
{
return new Random(0).Next(10000);
}
public class MyClass
{
private int var = 11111;
public int GetSome(int val)
{
return var * val;
}
}
private static void Main()
{
var clas = new MyClass();
int a = 111;
int b = 222;
Expression<Func<int>> expr = () => a * 2 - clas.GetSome(b) + b + 1 - Method1();
var result = InvokeAndLog(expr);
}
private static T InvokeAndLog<T>(Expression<Func<T>> expr)
{
var visitor = new ArgumentsVisitor();
visitor.Visit(expr);
Console.WriteLine("Inputs: {0}", string.Join(", ", visitor.Arguments));
return expr.Compile()();
}
}
The main class here is ExpressionVisitor descendant. I just overrided its member visitor and aggregated all members into single set:
public class ArgumentsVisitor : ExpressionVisitor
{
private readonly HashSet<MemberInfo> _all = new HashSet<MemberInfo>();
public IEnumerable<MemberInfo> Arguments
{
get { return _all; }
}
protected override Expression VisitMember(MemberExpression node)
{
_all.Add(node.Member);
return base.VisitMember(node);
}
}
Then I applyed visitor to expression and printed out all variables! Thanks, microsoft =)
Related
I try to get recursively the name of a variable, for example for the variable test.child, if I follow this topic, I only get child and I have no idea how to get all the parents.
Here is my test code:
public static class MemberInfoGetting
{
public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
{
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
return expressionBody.Member.Name;
}
}
public class Test
{
public string child = "childValue";
}
static void Main(string[] args)
{
//string testVariable = "value";
Test test = new Test();
test.child = "newValue";
string nameOfTestVariable = MemberInfoGetting.GetMemberName(() => test.child);
Console.WriteLine(nameOfTestVariable + " | " + test.child);
Console.Read();
}
I throw this in as the first intent: (not 100% done, still thinking about it, at least it handles your sample)
public static class MemberInfoGetting {
public static string GetMemberName<T>(Expression<Func<T>> memberExpression) {
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
var str = expressionBody.ToString();
var lst = str.Split('.').Skip(2).ToList(); //This needs LINQ, otherwise do it manually
StringBuilder retVal = new StringBuilder();
for (int i = 0; i < lst.Count; i++) {
retVal.Append(lst[i]);
if(i != lst.Count -1) {
retVal.Append(".");
}
}
return retVal.ToString();
}
}
Then maybe it is useful to have something like:
public class Test
{
public string value = "Value";
public Test child;
public Test(Test childObject)
{
this.child = childObject;
}
}
You can use this object for recursion then. As now you have nested objects.
Something like this:
Test child = new Test(null);
Test parent = new Test(child);
So, if you create a method which takes as parameter the parent object in this case, you can get the parent and the child values as well. In this case, the exit condition of your recursion method being the Test object that has a null child.
I need help. I'm making a programm and i have a problem.
I have a constructor with delegate already:
public delegate bool delIsDone(int curState, int needState);
public Progress(delIsDone doneFunction) { ... }
I need to pass it without creating it outside of creation:
public bool check() { return true }
Progress p = new Progress(check);
I need to do something like this:
Progress p = new ProgressChecker(bool check(int currentProgress, int needProgress) {
return currentProgress < needProgress;
});
Where ProgressChecker is a class that have method that check progress.
In loop i execute this function to get result. If function return "true" it's mean that "Achievement geted" and i need to hold it.
Thx for help
In the constructor you can pass in a function like so:
ProgressChecker(Func<int,int,bool> checker)
That means you can pass a function into the constructor for ProgressChecker
public bool checker (int a, int b)
{
return a < b;
}
var t = new ProgressChecker(checker);
EDIT:
If you want to use the delegate, then in the ProgressChecker class, the constructor must take in that delegate type:
private delIsDone delIsDone;
public ProgressChecker(delIsDone delIsDone)
{
this.delIsDone = delIsDone;
}
You can the pass a delegate instance like so:
public class Program
{
public delegate bool delIsDone(int curState, int needState);
public static bool checkNumbers(int a, int b)
{
return a < b;
}
public static void Main(string[] args)
{
var t = new ProgressChecker(new delIsDone(checkNumbers));
// OR var t = new ProgressChecker(new delIsDone((a, b) => { return a < b; }));
}
}
You can also create Expression Trees, it is my snippet for it (it can be easly converted to your example)
//easy way
Expression<Action<int>> printExpr = (arg) => Console.WriteLine(arg);
printExpr.Compile()(10);
//hard way
ParameterExpression param = Expression.Parameter(typeof(int), "arg");
MethodCallExpression methodCall = Expression.Call
(
typeof(Console).GetMethod("WriteLine", new[]
{
typeof(int)
}
),
param
);
Expression.Lambda<Action<int>>(methodCall, param).Compile()(10); //execution
More information ca be gathered at Generating Dynamic Methods with Expression Trees
I want to overload operator +(f,s) for a generic class. It should return +(f,s) if f and s have plus operator and null otherwise. How can I do it?
public class param<T> : TDefault
{
public string param_name;
public T cnt;
public param(T _cnt)
{
cnt=_cnt;
}
public static param<T> operator +(param<T> f, param<T> s)
{
if(T ? has_operator("+"))
return param<T>(((f.cnt as ?)+(s.cnt as ?)) as T);
return null;
}
}
For checking operator existence I tried
public static bool has_method(this object target,string method_name)
{
return target.GetType().GetMethod(method_name)!=null;
}
but
int x;
print (x.has_method("+="));
prints 'false'
Try this:
public class Param<T>
{
private static readonly Func<T, T, T> addMethod;
static Param()
{
try
{
ParameterExpression left = Expression.Parameter(typeof (T), "left");
ParameterExpression right = Expression.Parameter(typeof (T), "right");
addMethod = Expression.Lambda<Func<T, T, T>>(Expression.Add(left, right), left, right).Compile();
}
catch (InvalidOperationException)
{
//Eat the exception, no + operator defined :(
}
}
public string param_name;
public T cnt;
public Param(T _cnt)
{
cnt = _cnt;
}
public static Param<T> operator +(Param<T> leftOperand, Param<T> rightOperand)
{
if (addMethod != null)
{
return new Param<T>(addMethod(leftOperand.cnt, rightOperand.cnt));
}
return null;
}
}
private static void Main(string[] args)
{
var pi = new Param<int>(5);
var pi2 = new Param<int>(6);
var pi3 = pi + pi2;
var pi4 = new Param<ooject>(5);//Wont work
var pi5 = new Param<object>(6);
var pi6 = pi4 + pi5;
}
Credit goes to JonSkeet and Marc
Consider the following setup:
class A { public int x; }
class B { public int y; }
static class Helper
{
public static Expression<Func<B>> BindInput(
Expression<Func<A, B>> expression,
A input)
{
//TODO
}
}
static void Main(string[] args)
{
Expression<Func<B>> e = Helper.BindInput(
(A a) => new B { y = a.x + 3 },
new A { x = 4 });
Func<B> f = e.Compile();
Debug.Assert(f().y == 7);
}
What I want to do in the method BindInput is transform the expression to have input embedded into it. In the example usage in Main, the resulting expression e would be
() => new B { y = input.x + 3 }
where input is the second value that was passed into BindInput.
How would I go about doing this?
Edit:
I should add that the following expression e is not what I'm looking for:
((A a) => new B { y = a.x + 3 })(input)
This would be fairly trivial to obtain because it just involves adding a layer on top of the existing expression.
After lots of searching I stumbled across the magic ExpressionVisitor class. The following seems to be working perfectly:
class MyExpressionVisitor : ExpressionVisitor
{
public ParameterExpression TargetParameterExpression { get; private set; }
public object TargetParameterValue { get; private set; }
public MyExpressionVisitor(ParameterExpression targetParameterExpression, object targetParameterValue)
{
this.TargetParameterExpression = targetParameterExpression;
this.TargetParameterValue = targetParameterValue;
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (node == TargetParameterExpression)
return Expression.Constant(TargetParameterValue);
return base.VisitParameter(node);
}
}
static class Helper
{
public static Expression<Func<B>> BindInput(Expression<Func<A, B>> expression, A input)
{
var parameter = expression.Parameters.Single();
var visitor = new MyExpressionVisitor(parameter, input);
return Expression.Lambda<Func<B>>(visitor.Visit(expression.Body));
}
}
This is kind of a weird question but it came up the other day and it has me thinking.
When is it preferable design to use lambda expressions in this form ".(x => x.Whatever)" verse ".(() => obj.Whatever)".
Consider the following extension methods.
public static class ExtensionMethods
{
public static string TryToGetTheString<T>(this T value, Func<T, string> method)
{
try
{
return method(value);
}
catch (Exception)
{
return "banana";
}
}
public static string TryToGetTheStringTwo<T>(this T value, Func<string> method)
{
try
{
return method();
}
catch (Exception)
{
return "banana";
}
}
}
And the following self referencing class.
public class testClass5000
{
private int? _id;
public int? ID { get { return _id; } set { _id = value; } }
private string _urgh;
public string Urgh { get; set; }
public testClass5000 tc5k { get; set; }
}
Then using an extremely lazy process to avoid checking for nulls, while attempting to get a string (Urgh) from testClass5000, you could implement the extension methods and class like such,
private void main()
{
var tc = new testClass5000();
textBox1.text = tc.TryToGetTheString(x => x.tc5k.tc5k.tc5k.Urgh);
}
However, since tc is declared locally the following also works.
private void main()
{
var tc = new testClass5000();
textBox1.text = tc.TryToGetTheStringTwo(() => tc.tc5k.tc5k.tc5k.Urgh);
}
I am curious when (x => x.tc5k.tc5k.tc5k.Urgh) is necessary and when (() => tc.tc5k.tc5k.tc5k.Urgh) is preferable.
//////////////////////////////////////////
I did come up with the following scenario where passing the parameter seems preferable.
With the following extension methods.
public static class ExtensionMethods
{
public static T TestOne<T>(this T value, Func<T, T> method)
{
try
{
return method(value);
}
catch (Exception)
{
return default(T);
}
}
public static T TestTwo<T>(this T value, Func<T> method)
{
try
{
return method();
}
catch (Exception)
{
return default(T);
}
}
}
And using the following code.
private void Form1_Load(object sender, EventArgs e)
{
var firstValue = 5;
var secondValue = 10;
var resultOne = firstValue.TestOne(x => x + 1).TestOne(x => x * 2);
//returns 12
var resultTwo = secondValue.TestTwo(() => secondValue + 1).TestTwo(() => secondValue * 2);
//returns 20
var resultThree = secondValue.TestTwo(() => secondValue.TestTwo(() => secondValue + 1) * 2);
//returns 22
}
In this example .TestOne(x => x + 1).TestOne(x => x * 2) is preferable notation because to achieve the same thing without passing a paremeter you need to start nesting expressions.
Injecting the parameters values directly in the lambda is more costly, because the compiler has to create a special class just for this purpose.
If we exclude performance considerations, then I would say that injecting the parameter is easier to write (personal preference here), and keeping the parameters in the prototype ( (x,y) => // do something) is useful when you're not actually the one providing the value of the parameters. For instance, when using the Select Linq query. Or I often use that for load balancing scenarios (a lambda "service => service.SomeFunction()", then a special factory retrieve the service and execute the lambda).
In cases where the parameters are simply not the same as the original value you provided.
A crude example
public static class Extensions
{
public static void DoSomething(this string s,Action<string> action)
{
var something = Enumerable.Range(1,100).Select(i=> String.Format("{0}_{1}",s,i));
foreach (var ss in something)
{
action(ss);
}
}
}
Then
var something = "ABC123";
something.DoSomething(x=>Console.WriteLine(x));
//Ignoring that we could do something.DoSomething(Console.WriteLine);
Obviously without the parameter you cant access the actual value you are insterested in, and the original value is of no use within this concept.