我需要执行Array中列出的函数 [英] I need to execute the functions which are in listed in Array

查看:52
本文介绍了我需要执行Array中列出的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void PrintRecord(ref Student std)
        {
string[] tname = { "Name : ", "Gender : ", "Age : ", "Marks : ", "Standard : ", "Divison : " };
            string [] fname={"std.GetName()", "std.GetGender()", "std.GetAge()", "std.GetMarks()", "std.GetStandard()", "std.GetDivision"};
            string funName;
            for (int index = 0; index < tname.Length; ++index)
            {
                funName=fname[index];
                Console.WriteLine(tname[index] + ":" + std.GetName());
            }
        }





输出:

名称:std.GetName() ;

性别:std.GetGender(); ...



在上面的输出......这些函数直接打印..我不想要这个,我需要执行这些功能而不是这个输出。 />
请给我指路.....

感谢你。



Output :
Name : std.GetName();
Gender : std.GetGender(); ...

In above output ... these functions are directly printing .. I dont want this , i need to executed these function instead of this output.
Please show me way.....
Thanking you.

推荐答案

功能不能真正成为在数组中列出。你列出了函数的名称,但这确实是非常糟糕的方法(解决方案1是一个非常糟糕的答案)。这是不可维护的。自己想一想:如果你拼错了名字,编译器将无法检测到问题。如果重命名该函数,您将立即中断该功能,并且永远不会出现可帮助您恢复一致性的编译器错误/警告。这样的方法是真正的灾难。



同时,有一个明显而且非常简洁的解决方案。您应该存储委托实例是您的数组或某个集合。我已经开发出了一种完整的强大技术,这种技术具有通用性和高度通请参阅我的CodeProject文章:

动态方法调度程序 [ ^ ]。



-SA
The "functions" cannot really be listed in an array. You list the names of the functions, but this is really, really bad approach (and Solution 1 is a really bad answer). It is not maintainable. Think by yourself: should you misspell the name, a compiler would not be able to detect the problem. Should you rename the function, you will immediately break the functionality, and the compiler errors/warning which could help you to restore the consistency, would never appear. Such approaches are the real disaster.

At the same time, there is an apparent and really neat solution. You should store delegate instances is your array or some collection. I''ve developed a whole robust technique, which is generic and highly universal. Please see my CodeProject article:
Dynamic Method Dispatcher[^].

—SA


请尝试更改

string [] fname = {std.GetName(), std.GetGender(),std.GetAge(),std.GetMarks(),std.GetStandard(),std.GetDivision};



as

string [] fname = {std.GetName(),std.GetGender(),std.GetAge(),std.GetMarks(),std.GetStandard(), std.GetDivision};



如果你的方法没有返回字符串值那么请把它投成字符串



我尝试过你的代码



Please try to change
string [] fname={"std.GetName()", "std.GetGender()", "std.GetAge()", "std.GetMarks()", "std.GetStandard()", "std.GetDivision"};

as
string [] fname={std.GetName(), std.GetGender(), std.GetAge(), std.GetMarks(), std.GetStandard(), std.GetDivision};

and if your method not returning string value then please cast it into string

I have tried with your code

public  void PrintRecord(ref Student std)
        {
            string[] tname = { "Name : ", "Gender : ","Age : "};
            string[] fname = { std.GetName(), std.GetGender(), std.GetAge().ToString() };
            string funName;
            for (int index = 0; index < tname.Length; ++index)
            {
                funName = fname[index];
                Response.Write(tname[index] + ":" + funName);
            }
            }





演示学生班





demo student class

public class Student
    {
        public string GetName()
        {
            return "soumen";
        }

        internal string GetGender()
        {
            return "M";
        }

        internal int GetAge()
        {
            return 32;
        }
    }





输出结果是

名称:: soumen性别: :M年龄:: 32



这是你想要显示的输出还是请解释



The output is
Name : :soumen Gender : :M Age : :32

Is that the output you want to display or please explain


这篇关于我需要执行Array中列出的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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