通过枚举调用方法C#中的属性 [英] Calling a method by enum Attribute in C#

查看:149
本文介绍了通过枚举调用方法C#中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  public enum FunctionType 
{
[CallMethod(ExecuteFunction.DOPLUS)] //如何实现CallMethod magic属性
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;组; }

public FunctionType FunctionType
{
get {return _functionType; }
set {_functionType = value;
}

public string Execute()
{
return SomeMagicMethod(this.FunctionType); //如何实现此方法以预期的方式返回我的结果
}
}

最后,我的计算类有一些函数返回结果:

  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枚举和SomeMagicMethod以上运行指定的方法而不使用switch case正常?

解决方案

你不能引用一个方法在你写的一个属性(它不是编译时)。



你的方法是错误的 - 你应该使用一个属性来引用它们相应的枚举来装饰方法,就像这样:

  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方法;

if(!FunctionTypeToMethod.TryGetValue(functionType,out method))
{
throw new ArgumentException(找不到给定函数类型的处理函数,functionType );
}
else
{
string result =(string)method.Invoke(null,new object [] {a,b});

返回结果;
}
}
}

当然,优化可以完成如使用Delegate.CreateDelegate缓存编译委托。


First, I'm so sorry because my bellow stupid question. But I hope someone can help me on this approach.

I have an Enum that I want to be add new magic attribute as described:

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
    }

My class has a FunctionType property like this:

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
        }
    }

Last, my calculate class has some functions return result:

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();
        }
    }

My stupid question is: How can I implement CallMethodAttribute in enum and SomeMagicMethod above to run specified method without using switch case as normal ?

解决方案

You can't put a reference to a method in an attribute as you wrote (it is not compile-time).

Your approach is wrong - You should decorate the methods with an attribute referring their corresponding enum, like this:

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();
    }
}

The attribute code:

[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; }
    }
}

And then detect the corresponding method for a given enum value type with reflection and invoke it:

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;
        }
    }
}

Of course, optimizations can be done such as caching a compiled delegate using Delegate.CreateDelegate.

这篇关于通过枚举调用方法C#中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆