在c#中通过枚举属性调用方法



首先,我很抱歉,因为我的问题很愚蠢。但是我希望有人能在这个方法上帮助我。

我有一个枚举,我想添加新的魔法属性如下所述:

public enum FunctionType
    {
        [CallMethod(ExecuteFunction.DOPLUS)] //How to implement CallMethod magic attribute
        PLUS,
        [CallMethod(ExecuteFunction.DOMINUS)]
        MINUS,
        [CallMethod(ExecuteFunction.DOMULTIPLY)]
        MULTIPLY,
        [CallMethod(ExecuteFunction.DODIVIDE)]
        DIVIDE
    }

我的类有这样一个FunctionType属性:

public class Function
    {
        private FunctionType _functionType;
        public List<object> Params
        { get; set; }
        public FunctionType FunctionType
        {
            get { return _functionType; }
            set { _functionType = value; }
        }
        public string Execute()
        {
            return SomeMagicMethod(this.FunctionType); //How to implement this method to return my result as expected
        }
    }
最后,我的计算类有一些函数返回结果:
public static class ExecuteFunction
    {
        public static string DOPLUS(int a, int b)
        {
            return (a + b).ToString();
        }
        public static string DOMINUS(int a, int b)
        {
            return (a - b).ToString();
        }
        public static string DOMULTIPLY(int a, int b)
        {
            return (a * b).ToString();
        }
        public static string DODIVIDE(int a, int b)
        {
            return (a / b).ToString();
        }
    }

我的愚蠢的问题是:我怎么能实现CallMethodAttribute在enum和someemagicmethod上面运行指定的方法,而不使用开关情况正常?

你不能像你写的那样在属性中放置对方法的引用(它不是编译时的)。

你的方法是错误的——你应该用一个引用它们对应枚举的属性来修饰这些方法,像这样:

public static class ExecuteFunction
{
    [CallMethod(FunctionType.PLUS)]
    public static string DOPLUS(int a, int b)
    {
        return (a + b).ToString();
    }
    [CallMethod(FunctionType.MINUS)]
    public static string DOMINUS(int a, int b)
    {
        return (a - b).ToString();
    }
    [CallMethod(FunctionType.MULTIPLY)]
    public static string DOMULTIPLY(int a, int b)
    {
        return (a * b).ToString();
    }
    [CallMethod(FunctionType.DIVIDE)]
    public static string DODIVIDE(int a, int b)
    {
        return (a / b).ToString();
    }
}

属性代码:

[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class CallMethodAttribute : Attribute
{
    private readonly FunctionType mFunctionType;
    public CallMethodAttribute(FunctionType functionType)
    {
        mFunctionType = functionType;
    }
    public FunctionType FunctionType
    {
        get { return mFunctionType; }
    }
}

然后用反射检测给定枚举值类型的相应方法并调用它:

public class YourMagicClass
{
    private static readonly Dictionary<FunctionType, MethodInfo> FunctionTypeToMethod =
        typeof (ExecuteFunction).
            GetMethods(BindingFlags.Public | BindingFlags.Static)
                                .Where(x => x.IsDefined(typeof (CallMethodAttribute)))
                                .Select(x => new
                                    {
                                        Method = x,
                                        FunctionType = x.GetCustomAttribute<CallMethodAttribute>().FunctionType
                                    })
                                .ToDictionary(x => x.FunctionType, x => x.Method);

    public static string SomeMagicMethod(FunctionType functionType, int a, int b)
    {
        MethodInfo method;
        if (!FunctionTypeToMethod.TryGetValue(functionType, out method))
        {
            throw new ArgumentException("Could not find a handler for the given function type", "functionType");
        }
        else
        {
            string result = (string)method.Invoke(null, new object[] { a, b });
            return result;
        }
    }
}

当然,可以进行优化,例如使用delegate . createdelegate缓存编译后的委托。

如果您准备用映射字典替换您的属性:

public class Function 
{
    private static readonly IDictionary<FunctionType, Func<int, int, string>> functionMappings = 
        new Dictionary<FunctionType, Func<int, int, string>>
    {
        { FunctionType.PLUS, ExecuteFunction.DOPLUS },
        { FunctionType.MINUS, ExecuteFunction.DOMINUS },
        { FunctionType.MULTIPLY, ExecuteFunction.DOMULTIPLY },
        { FunctionType.DIVIDE, ExecuteFunction.DODIVIDE },
    };
    public string Execute()
    {
        return functionMappings[_functionType]((int)Params[0], (int)Params[1]);
    }
}

相关内容

  • 没有找到相关文章

最新更新