How can I evaluate C# code dynamically?

后端 未结 16 1940
[愿得一人]
[愿得一人] 2020-11-22 03:37

I can do an eval(\"something()\"); to execute the code dynamically in JavaScript. Is there a way for me to do the same thing in C#?

An example of what I

相关标签:
16条回答
  • 2020-11-22 04:12

    You can use reflection to get the property and invoke it. Something like this:

    object result = theObject.GetType().GetProperty("Property" + i).GetValue(theObject, null);
    

    That is, assuming the object that has the property is called "theObject" :)

    0 讨论(0)
  • 2020-11-22 04:12

    You might check the Heleonix.Reflection library. It provides methods to get/set/invoke members dynamically, including nested members, or if a member is clearly defined, you can create a getter/setter (lambda compiled into a delegate) which is faster than reflection:

    var success = Reflector.Set(instance, null, $"Property{i}", value);
    

    Or if number of properties is not endless, you can generate setters and chache them (setters are faster since they are compiled delegates):

    var setter = Reflector.CreateSetter<object, object>($"Property{i}", typeof(type which contains "Property"+i));
    setter(instance, value);
    

    Setters can be of type Action<object, object> but instances can be different at runtime, so you can create lists of setters.

    0 讨论(0)
  • 2020-11-22 04:15

    You could do it with a prototype function:

    void something(int i, string P1) {
        something(i, P1, String.Empty);
    }
    
    void something(int i, string P1, string P2) {
        something(i, P1, P2, String.Empty);
    }
    
    void something(int i, string P1, string P2, string P3) {
        something(i, P1, P2, P3, String.Empty);
    }
    

    and so on...

    0 讨论(0)
  • 2020-11-22 04:15

    Unfortunately, C# doesn't have any native facilities for doing exactly what you are asking.

    However, my C# eval program does allow for evaluating C# code. It provides for evaluating C# code at runtime and supports many C# statements. In fact, this code is usable within any .NET project, however, it is limited to using C# syntax. Have a look at my website, http://csharp-eval.com, for additional details.

    0 讨论(0)
  • 2020-11-22 04:17

    the correct answer is you need to cache all the result to keep the mem0ry usage low.

    an example would look like this

    TypeOf(Evaluate)
    {
    "1+1":2;
    "1+2":3;
    "1+3":5;
    ....
    "2-5":-3;
    "0+0":1
    } 
    

    and add it to a List

    List<string> results = new List<string>();
    for() results.Add(result);
    

    save the id and use it in the code

    hope this helps

    0 讨论(0)
  • 2020-11-22 04:18

    All of that would definitely work. Personally, for that particular problem, I would probably take a little different approach. Maybe something like this:

    class MyClass {
      public Point point1, point2, point3;
    
      private Point[] points;
    
      public MyClass() {
        //...
        this.points = new Point[] {point1, point2, point3};
      }
    
      public void DoSomethingWith(int i) {
        Point target = this.points[i+1];
        // do stuff to target
      }
    }
    

    When using patterns like this, you have to be careful that your data is stored by reference and not by value. In other words, don't do this with primitives. You have to use their big bloated class counterparts.

    I realized that's not exactly the question, but the question has been pretty well answered and I thought maybe an alternative approach might help.

    0 讨论(0)
提交回复
热议问题