C# Treat A Block Of Code As If It Were Written As Part Of A Method

Question:

I’ve got an issue which I know how to solve in Python, but can’t find any way to solve in C#. The issue is that I need a way to run a block of code inside a Method without hard coding that block of code into the method

For example, the block of code could be simply "return;" or it could change variables within the method, but there are a near infinite number of possibilities for what this code could be so any kind of if statement or hard coding possibilities in is out of the question

In python, the easiest way I can think of to do this would be using Eval:

def Function1():
  num = 10
  Eval(MysteryCode())
  num += 1
  return num

def MysteryCode():
  return input("Write code here:  ")

print(Function1())

If you type "return num" as the input, it should return 10. If you wrote "num += 1", it should print 12. I won’t actually be taking input from the user, but it’s roughly the same as what I’m looking for

It also doesn’t need to be in string format, any way to run a block of code in a method that isn’t hard coded into that method works

Sorry if this doesn’t make much sense, I’m struggling to explain this properly 🙁

Asked By: WuzHizFace

||

Answers:

I figured it out, unlike in the example I don’t actually take input from the user so I found an Eval method for C#

Here’s a link to the package I ended up using: https://eval-expression.net/

To re-emphasize, this is NOT taking input from the user to run as code, as that is unsafe

Answered By: WuzHizFace

You’ve stated in the comments that you want to provide special abilities to characters without hard coding the same ability every time you need it. This sounds like the perfect time to use composition:

public class Character
{
    public int Health { get; set; }
    public ISpecialAbility Ability { get; set; }
}

public interface ISpecialAbility
{
    int SomeAction();
}

public class SomeAbility : ISpecialAbility
{
    private readonly Character _character;

    public SomeAbility(Character character)
    {
        _character = character;
    }

    public int SomeAction()
    {
        if (character.Health > 10)
            return 3;
        else
            return 5;
    }
}

And then when you construct a character, you can assign their special ability:

Character c = new Character();
c.Ability = new SomeAbility(c);
// Call the special ability
int result = c.Ability.SomeAction();

You don’t necessarily need to pass the character, but I figured you might need a reference to it.

Alternatively, you could use a delegate:

public delegate int SpecialAbility(Character c);

And perhaps have some static methods:

public static int SomeAbility(Character c)
{
    if (c.Health > 10)
        return 3;
    else
        return 5;
}

Then your Character class might look like this:

public class Character
{
    private readonly SpecialAbility _specialAbility;
    public int Health { get; set; }

    public Character(SpecialAbility specialAbility)
    {
        _specialAbility = specialAbility;
    }

    public void PerformSpecialAbility()
    {
        int result = _specialAbility(this);
    }
}

Then you would construct your character like this:

Character c = new Character(SomeAbility);
// call special ability
c.PerformSpecialAbility();
Answered By: DiplomacyNotWar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.