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

C# 在C中通过枚举属性调用方法#,c#,reflection,enums,C#,Reflection,Enums,首先,我很抱歉,因为我的问题太愚蠢了。但我希望有人能在这方面帮助我 我有一个要添加新魔法属性的枚举,如下所述: public enum FunctionType { [CallMethod(ExecuteFunction.DOPLUS)] //How to implement CallMethod magic attribute PLUS, [CallMethod(ExecuteFunction.DOMINUS)] MINU

首先,我很抱歉,因为我的问题太愚蠢了。但我希望有人能在这方面帮助我

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

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

我愚蠢的问题是:如何在enum和上面的SomeMagicMethod中实现CallMethodAttribute以运行指定的方法,而不使用正常的开关大小写?

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

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]);
    }
}
公共类函数
{
专用静态只读IDictionary functionMappings=
新词典
{
{FunctionType.PLUS,ExecuteFunction.DOPLUS},
{FunctionType.减号,ExecuteFunction.DOMINUS},
{FunctionType.MULTIPLY,ExecuteFunction.DOMULTIPLY},
{FunctionType.DIVIDE,ExecuteFunction.DODIVIDE},
};
公共字符串Execute()
{
返回函数映射[_functionType]((int)参数[0],(int)参数[1]);
}
}

如果准备用映射词典替换属性:

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]);
    }
}
公共类函数
{
专用静态只读IDictionary functionMappings=
新词典
{
{FunctionType.PLUS,ExecuteFunction.DOPLUS},
{FunctionType.减号,ExecuteFunction.DOMINUS},
{FunctionType.MULTIPLY,ExecuteFunction.DOMULTIPLY},
{FunctionType.DIVIDE,ExecuteFunction.DODIVIDE},
};
公共字符串Execute()
{
返回函数映射[_functionType]((int)参数[0],(int)参数[1]);
}
}

您不能在编写时将对方法的引用放入属性中(这不是编译时)

您的方法是错误的-您应该使用一个引用其对应枚举的属性来修饰方法,如下所示:

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
        }
    }
公共静态类ExecuteFunction
{
[调用方法(FunctionType.PLUS)]
公共静态字符串DOPLUS(int a、int b)
{
return(a+b).ToString();
}
[调用方法(FunctionType.减号)]
公共静态字符串DOMINUS(int a,int b)
{
return(a-b).ToString();
}
[调用方法(FunctionType.MULTIPLY)]
公共静态字符串DOMULTIPLY(int a,int b)
{
return(a*b).ToString();
}
[调用方法(FunctionType.DIVIDE)]
公共静态字符串DODIVIDE(inta,intb)
{
返回(a/b).ToString();
}
}
属性代码:

[AttributeUsage(AttributeTargets.Method,Inherited=true,AllowMultiple=false)]
公共类CallMethodAttribute:属性
{
私有只读FunctionType MFFunctionType;
公共调用方法属性(函数类型函数类型)
{
mFunctionType=函数类型;
}
公共函数类型函数类型
{
get{return mffunctionType;}
}
}
然后使用反射检测给定枚举值类型的对应方法并调用它:

公共类YourMagicClass
{
私有静态只读字典函数TypeToMethod=
typeof(ExecuteFunction)。
GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(x=>x.IsDefined(typeof(CallMethodAttribute)))
.选择(x=>new
{
方法=x,
FunctionType=x.GetCustomAttribute().FunctionType
})
.ToDictionary(x=>x.FunctionType,x=>x.Method);
公共静态字符串SomeMagicMethod(FunctionType FunctionType,int a,int b)
{
方法信息法;
if(!FunctionTypeToMethod.TryGetValue(functionType,out方法))
{
抛出新ArgumentException(“找不到给定函数类型的处理程序”,“functionType”);
}
其他的
{
string result=(string)method.Invoke(null,新对象[]{a,b});
返回结果;
}
}
}

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

编写时不能在属性中放置对方法的引用(这不是编译时)

您的方法是错误的-您应该使用一个引用其对应枚举的属性来修饰方法,如下所示:

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
        }
    }
公共静态类ExecuteFunction
{
[调用方法(FunctionType.PLUS)]
公共静态字符串DOPLUS(int a、int b)
{
return(a+b).ToString();
}
[调用方法(FunctionType.减号)]
公共静态字符串DOMINUS(int a,int b)
{
return(a-b).ToString();
}
[调用方法(FunctionType.MULTIPLY)]
公共静态字符串DOMULTIPLY(int a,int b)
{
return(a*b).ToString();
}
[调用方法(FunctionType.DIVIDE)]
公共静态字符串DODIVIDE(inta,intb)
{
返回(a/b).ToString();
}
}
属性代码:

[AttributeUsage(AttributeTargets.Method,Inherited=true,AllowMultiple=false)]
公共类CallMethodAttribute:属性
{
私有只读FunctionType MFFunctionType;
公共调用方法属性(函数类型函数类型)
{
mFunctionType=函数类型;
}
公共函数类型函数类型
{
获取{return mFunctionType;}
}
}
然后使用反射检测给定枚举值类型的对应方法并调用它:

公共类YourMagicClass
{
私有静态只读字典函数TypeToMethod=
typeof(ExecuteFunction)。
GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(x=>x.IsDefined(typeof(CallMethodAttribute)))
.选择(x=>new
{
方法=x,
FunctionType=x.GetCustomAttribute().FunctionType