C# 按特定顺序自动运行函数
我想知道是否有一种方法可以通过在main方法中放一行来运行f.e.“functions Class”中的所有函数。这背后的想法是,为了节省一些速度和时间,写出每一个函数并制作一个非常长的main方法 我在想(只是为了证明我在寻找什么):C# 按特定顺序自动运行函数,c#,C#,我想知道是否有一种方法可以通过在main方法中放一行来运行f.e.“functions Class”中的所有函数。这背后的想法是,为了节省一些速度和时间,写出每一个函数并制作一个非常长的main方法 我在想(只是为了证明我在寻找什么): 提前谢谢 尽管在这种情况下所有关于正确OOP的评论(我认为这是有效的),这里有一个基于反射的小例子: class F { public void F1() { Console.WriteLine("Hello F1");
提前谢谢 尽管在这种情况下所有关于正确OOP的评论(我认为这是有效的),这里有一个基于反射的小例子:
class F
{
public void F1()
{
Console.WriteLine("Hello F1");
}
}
class MainClass
{
public static void Main(string[] args)
{
var f = new F();
foreach (var method in
// get the Type object, that will allow you to browse methods,
// properties etc. It's the main entry point for reflection
f.GetType()
// GetMethods allows you to get MethodInfo objects
// You may choose which methods do you want -
// private, public, static, etc. We use proper BindingFlags for that
.GetMethods(
// this flags says, that we don't want methods from "object" type,
// only the ones that are declared here
BindingFlags.DeclaredOnly
// we want instance methods (use "Static" otherwise)
| BindingFlags.Instance
// only public methods (add "NonPublic" to get also private methods)
| BindingFlags.Public)
// lastly, order them by name
.OrderBy(x => x.Name))
{
//invoke the method on object "f", with no parameters (empty array)
method.Invoke(f, new object[] { });
}
}
}
这将有效地从类型F
中获取所有公共实例方法,按名称对它们进行排序,并且执行时不带任何参数
在这种特殊情况下,它将显示:
你好,F1
但是,一般来说,运行“所有方法”,甚至更糟糕的是,取决于它们的字母顺序,是非常不鼓励的
欢迎来到StackOverflow 使用Func方法
static void Main(string[] args)
{
List<Func<string, string>> methods = new List<Func< string, string>>(){
methodA, methodB, methodC, methodD
};
foreach (Func<string,string> method in methods)
{
string result = method("a");
}
}
static string methodA(string a)
{
return a;
}
static string methodB(string b)
{
return b;
}
static string methodC(string c)
{
return c;
}
static string methodD(string d)
{
return d;
}
static void Main(字符串[]args)
{
列表方法=新列表>(){
方法A,方法B,方法C,方法D
};
foreach(方法中的Func方法)
{
字符串结果=方法(“a”);
}
}
静态字符串方法a(字符串a)
{
返回a;
}
静态字符串方法b(字符串b)
{
返回b;
}
静态字符串方法c(字符串c)
{
返回c;
}
静态字符串方法d(字符串d)
{
返回d;
}
没有内置任何内容。。。它可以通过反射轻松构建(但要小心排除Main
以避免无限递归!)。您是说您将拥有Function1
、Function2
和Function3
,并且您希望在不显式执行函数的情况下调用它们。Function1()
?这就是反射的作用。但是,除非有数百个方法,否则最好直接调用它们。如果你有数百种方法,你可能应该重新思考你的整个方法。学习OOP而不是询问如何排序你的函数你想用C#使用简单函数的顺序执行来运行整个应用程序吗?坏主意。尝试使用OOP。好的,我不知道你在这里做什么,因为我对BindingFlags系统一无所知,它是如何工作的?你到底在这里做什么?我将把代码分成几行,并对其进行注释以提供帮助,但我也推荐并再次表示感谢!不客气!如果你认为这回答了你的问题,请随意将我的帖子标记为答案(选票下方的勾号)。提问者正在寻找动态解决方案OP在哪里要求动态解决方案?当他说“节省一些速度和时间,写出每一个函数并制作一个非常长的主方法”时,我的解释是OP想要减少代码行数。拥有一个数组就可以做到这一点。OP说“A”(一行):“在我的主要方法中放一行”。你并没有真正做到这一点。还有一个方法列表。现在你有了额外的代码。不管怎样,这是我的意见,这就是投票的原因。我不希望你听我的意见,我只是把它留给别人而不是你。
static void Main(string[] args)
{
List<Func<string, string>> methods = new List<Func< string, string>>(){
methodA, methodB, methodC, methodD
};
foreach (Func<string,string> method in methods)
{
string result = method("a");
}
}
static string methodA(string a)
{
return a;
}
static string methodB(string b)
{
return b;
}
static string methodC(string c)
{
return c;
}
static string methodD(string d)
{
return d;
}