Making operations more dynamic I am trying to build something that will allow us to configure "custom" validation for each customer.  So if a customer wants to see a value contain a specific char, be specific length, or equal something else they just add a pre-created option.  I wrote something that seems to be very primitive, but works.  I would like it to be more dynamic.  For instance, I can pass in actual C# code and it will perform that operation on the value input and being compared.

public class Operation
{
    public object value { get; set; }
    public OMagic perform { get; set; }
}

public class Magic
{


    public bool Validate(object val, Operation operation)
    {
        switch (operation.perform)
        {
            case OMagic.Equal:
                return (string)val == (string)operation.value;
            case OMagic.NotEqual:
                return (string)val != (string)operation.value;
            case OMagic.LengthGreaterThan:
                return (int)val &lt; (int)operation.value;
            case OMagic.LengthEqual:
                return (int)val == (int)operation.value;
            case OMagic.LengthLessThan:
                return (int)val &gt; (int)operation.value;
            default:
                return false;
        }
    }
}

public enum OMagic
{
    Equal,
    NotEqual,
    LengthGreaterThan,
    LengthEqual,
    LengthLessThan

}


That is the primitive code, and here are the tests I wrote (using nunit)

[TestFixtureSetUp]
    public void SetupTest()
    {
        //CurrentMessage = getMessage(WebIZ.Test.UnitTest.Properties.Resources.TestResponse1);
        Magic = new Magic();
    }

    private Magic Magic;

    [Test]
    public void Text_Compare_Works()
    {
        Assert.IsTrue(Magic.Validate("yes", new Operation() { value = "yes", perform = OMagic.Equal }));
        Assert.IsFalse(Magic.Validate("yes", new Operation() { value = "no", perform = OMagic.Equal }));
        Assert.IsTrue(Magic.Validate("yes", new Operation() { value = "no", perform = OMagic.NotEqual }));
        Assert.IsFalse(Magic.Validate("yes", new Operation() { value = "yes", perform = OMagic.NotEqual }));
    }

    [Test]
    public void Int_Compare_Works()
    {
        Assert.IsFalse(Magic.Validate(7, new Operation(){value = 7, perform=OMagic.LengthGreaterThan}));
        Assert.IsTrue(Magic.Validate(7, new Operation() { value = 8, perform = OMagic.LengthGreaterThan }));
        Assert.IsFalse(Magic.Validate(7, new Operation() { value = 7, perform = OMagic.LengthLessThan }));
        Assert.IsTrue(Magic.Validate(7, new Operation() { value = 6, perform = OMagic.LengthLessThan }));
        Assert.IsFalse(Magic.Validate(7, new Operation() { value = 8, perform = OMagic.LengthEqual }));
        Assert.IsTrue(Magic.Validate(7, new Operation() { value = 7, perform = OMagic.LengthEqual }));
    }
Well there is multiple solution to this problem , 


Using delegate: 

public bool Validate&lt;T1,T2&gt;(T1 value, T2 compareValue, Func&lt;T1,T2,bool&gt; operation)
{
    return operation(value, compareValue);
}
magic.Validate(1, 2, (a, b) =&gt; a &gt; b);
magic.Validate("Testing", "Testing", (a, b) =&gt; a.Equals(b));



but twp problems with this code is : first your input can be limited , right now you cannot compare more than two values , so you might need to see what can be done to extend this code.

second you can make a switch case to return the right method for comparision. 


Using the Expression which could be more compact and will be cleaner code. I have not tried this yet. 
Avoid using private variable naming with capital letters. 
Avoid giving a name where two capital letters are at start (OMagic), you could write this like OperationMagic.
Avoid boxing and unboxing (if your operations are small dont worry).