为了账号安全,请及时绑定邮箱和手机立即绑定

从字符串中调用方法作为继承类

从字符串中调用方法作为继承类

C#
白猪掌柜的 2021-06-23 18:12:31
我面临的问题是我有一个用户输入对象的一些基本描述,然后他们指定计算的类型。将其视为具有乘法、加法、减法的能力。输入相同的数字,输出相同的数字。这些是一种类型的 IEnumerable 对象。所以他们在 excel 中输入数字,一个类抓取这些数字,我可以像 inputedobject.firstnumber、inputedobject.secondnumber 等一样使用它们。最后一个是 inpytedobject.calculationType。我在想,因为已经定义了计算类型,如果我可以接受并从该字符串中调用该计算。所以我会有类似的东西:var s = invokeMethod (inputedobject.calculationType, inputedobject)它会产生结果,calculationType (inputedobject)我会用它做一些事情。很高兴听到其他更好的实现。但是在我提到的这种方法中,我尝试了以下方法无济于事,因为它是在字符串上实现的,我不知道如何让它继承这些对象。class InvokeString    {        public static IEnumerable<eTotal> InvokeStringMethod(string typeName, string methodName, LoanInput loanInput)        {            // Get the Type for the class            Type calledType = Type.GetType(typeName);            // Invoke the method itself. The string returned by the method winds up in s.            // Note that stringParam is passed via the last parameter of InvokeMember,            // as an array of Objects.            IEnumerable<eTotal> s =  (string)calledType.InvokeMember(                            methodName,                            BindingFlags.InvokeMethod | BindingFlags.Public |                                BindingFlags.Static,                            null,                            null,                            new Object[] { loanInput } );            // Return the string that was returned by the called method.            return s;        }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信