Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:58:50+00:00 2026-05-23T23:58:50+00:00

Update (Stupidity Fail) So then, in all of my convoluted formula code, I neglected

  • 0

Update (Stupidity Fail)

So then, in all of my convoluted formula code, I neglected the fundamental principles of C#.

Methods may return a value.

static dynamic Construct<T>(T expression){
    return expression;
}

Then just use that, instead of a variable …

Method = Construct<Action<Context, string, int>>(
                        (context, key, change) =>
                            {
                                context.Saved[key] += change;

                                Console.WriteLine("{0}'s saved value of {1} was changed by {2}, resulting in {3}",
                                    context.Name, key, change, context.Saved[key]);
                            }
                        )

I have a situation where I need to call upon methods that don’t exist as compiled methods, but rather need to be able to accept an array of parameters and execute as an anonymous function. I thought I had it worked out, but I am running into an issue with the following..

public static IDictionary<string, Function> Expressions =
            new Dictionary<string, Function> {
            {
                "Increase [X] by value of [Y]",
                new Function {
                    Name = "Increase [X] by [Y]",
                    Parameters = 2,
                    Types = new List<Type>{
                        typeof(Param),
                        typeof(Param)
                    },
                    Method = (Expression<Func<Context, Param, Param, bool>>)
                                ((context, x, y) => {
                                    Console.WriteLine("test"); // this is where I need to do stuff... 
                                })
                }
            }
        };

I am being told that a Method name is expected on this. The problem is that Context will be passed in by the object that takes the function and runs its method, because the Context object cannot be pre-bound (it has to be late bound). So basically I package up the trailing 2 parameters (Param) and (Param) in this case and create a function to execute against them.

The database stores those parameters, and then invokes the method passing in the appropriate Context as the first parameter by using Compile().DynamicInvoke(object[] params).

Can anyone give me a hand here as to why I cannot put any kind of logic in between my { }?

UPDATE

Okay, since I’ve been told this example is unclear, here is an entire program running start to finish that illustrates what I am trying to accomplish.

public class Program {
    static void Main(string[] args) {
        // simple object stored in database.
        var ctx = new Context {
            Name = "Ciel",
            Saved = new Dictionary<string, int> {
                { "First", 10 },
                { "Second", 20 }
            }
        };
        // simple object stored in database.
        var rule = new Rule {
            Equations = new List<Equation> {
                new Equation {
                    Parameters = new List<object>{
                        "First",
                        5
                    },
                    Name = "Increase [X] by value of [Y]"
                }
            }
        };

        // =======================================
        // runtime environment!!!
        // =======================================
        var method = Evaluations.Expressions[rule.Equations[0].Name].Method;
        var parameters = rule.Equations[0].Parameters;

        // insert the specific context as the first parameter.
        parameters.Insert(0, ctx);
        method.DynamicInvoke(parameters.ToArray());

        Console.ReadLine();
    }
}

public class Function {
    public string Name { get; set; }
    public dynamic Method { get; set; }
}

public class Equation {
    public string Name { get; set; }
    // these objects will be simple enough to serialize.
    public IList<object> Parameters { get; set; }
    public Function Function { get; set; }
}

public class Context {
    public string Name { get; set; }
    // this is a crude example, but it serves the demonstration purposes.
    public IDictionary<string, int> Saved { get; set; }
}

public class Rule {
    // again, a crude example.
    public IList<Equation> Equations { get; set; }
}

public static class Evaluations {
    static Action<Context, string, int> expr = (context, key, change) =>
        {
                context.Saved[key] += change;

                Console.WriteLine("{0}'s saved value of {1} was changed by {2}, resulting in {3}",
                    context.Name, key, change, context.Saved[key]);
            };

    public static IDictionary<string, Function> Expressions =
        new Dictionary<string, Function> {
        {
            "Increase [X] by value of [Y]",
            new Function {
                Name = "Increase [X] by [Y]",
                Method = expr
            }
        }
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:58:50+00:00Added an answer on May 23, 2026 at 11:58 pm

    If you refactor your code to make it a little more readable and manageable, you’ll probably be well on your way to solving your problem. Rather than having one mammoth C# statement with a single semicolon, split it up into several lines. Something like this:

    public static Dictionary<string, Function> Expressions = getExpressions();
    
    private static Dictionary<string, Function> getExpressions()
    {   
        var method = (Expression<Func<Context, Param, Param, bool>>)
                        ((context, x, y) => {
                            Console.WriteLine("test"); // this is where I need to do stuff... 
                        })(true);
    
        var func = new Function()
        {
            Name = "Increase [X] by [Y]",
            Parameters = 2,
            Types = new List<Type>
            {
                typeof(Param),
                typeof(Param)
            },
            Method = method
        };
    
        var dict = new Dictionary<string, Function>();
        dict["Increase [X] by value of [Y]"] = func;
        return dict;
    }
    

    Note: my syntax could be incorrect, but you get the general idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: I reported this as a bug to Apple and they fixed it! All
Update: This has been answered. It was my own stupidity, possibly not worth reading
UPDATE maybe changing the Bitmap size in the new Activity may fix the problem
Update 14th May It's the mix of text sizes that breaks it, if I
Update: Solved, with code I got it working, see my answer below for the
UPDATE May this post be helpful for coders using RichTextBoxes. The Match is correct
UPDATE: Turns out my code works. Browser was caching previous failed response. Thanks for
** Update ** Modified the code for Ricardo Lohmann's suggestion with the additional modification
Update: Is there a way to achieve what I'm trying to do in an
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.