开发者

Using an unkown method based on what object is selected in the dictionary

开发者 https://www.devze.com 2023-01-27 15:08 出处:网络
I\'m currently done with my build script and have moved onto some C# programming. This is what I have:

I'm currently done with my build script and have moved onto some C# programming.

This is what I have:

public bool resultOfOperation(bool firstOperator, bool secondOperator, string operation)
        {
            //The bool value in the dictionary is gotten by used the corresponding operation method in the operation class.
            Dictionary<string, bool> mappedObjects = new Dictionary<string, bool>();
            mappedObjects.Add("OR", new OrOperator().operationOR(firstOperator,secondOperator));

            foreach (KeyValuePair<string, bool>  keyAndValue in mappedObjects)
            {
                if (keyAndValue.Key == operation.ToUpper())
                    return keyAndValue.Value;
            }

            throw new ArgumentException("Something has gone very wrong.");
        }

And although this works, I want to do something else. I want dictionary to hold onto a string and an object. And then I want to use the correct object based on what the method I supply the object with.

Example (Doesn't work)

Dictionary<string, object> test = new Dictionary<string, object>();
            for开发者_如何学运维each (KeyValuePair<string, object> keyAndValue in test)
            {
                if(keyAndValue.Key == "SomethingValue")
                    keyAndValue.Value.USE METHOD OF CLASS HERE;
            }

I should mention that all classes will have a similar methods e.g. bool FUNCTIONname(bool operator).

I'm fairly certain I have to you anonymous and delegates but I'm not sure how to. I would appreciate any help with this.


Instead of having unique method names, you should instead have the same method names, and leverage the type system to your advantage! For example:

abstract class Operation {
    public abstract bool DoOperation(bool left, bool right);
}

class OrOperation {
    public override bool DoOperation(bool left, bool right) {
        return left || right;
    }
}

class DoOperationStuff {
    Dictionary<string, Operation> mappedObjects = new Dictionary<string, Operation>() {
        {"OR", new OrOperation()}
    };

    public bool resultOfOperation(bool left, bool right, string operation) {
        if(mappedObjects.ContainsKey(operation)) {
            return mappedObjects[operation].DoOperation(left, right);
        }

        throw new ArgumentException("Something has gone very wrong.");
    }
}

This should be fairly explanitory, but:

  • Abstract classes are classes that cannot be instantiated directly, but are intended to be inherited by other classes.
  • Abstract (and virtual) methods are methods that are intended to be overridden in inherited classes.
  • Because DoOperation is marked abstract, the program knows to look at the real type of the object in order to know what to call.
  • So, even though the program has a bunch of Operation objects, each one is actually an OrOperation or whatever, and gets its respective method called.


As has already been mentioned, Interfaces or Inheritance are the preferable ways to solve this problem.

If for some reason you can't use them then you need reflection. (I'm sorry, I'm a VB programmer so my c# syntax might be wrong, hopefully you'll get the idea though)

Object o = the object to operate on
MethodInfo m = typeof(o).GetMethod("Method Name to Call");
m.Invoke(o);


Maybe the new Func<>() could be of help? http://tekpub.com/view/concepts/2

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号