f.eですべての関数を実行する方法があるかどうかを知りたい。私の主な方法に特定の行を置くことによって "関数 - クラス"。この背後にあるアイデアは、あらゆる時間と機能を節約し、すべての機能を書き出し、非常に長いメインメソッドを作成することです。特定の順序で自動的に関数を実行する
私はsthを考えています。 (私が探しているものを実証するだけです):
namespace ConsoleApp1
{
class Functions
{
public void Function1()
{
do.Something();
}
}
class Program
{
static void Main(string[] args)
{
RunAllFunctionsInFunctionsInAlphabeticalOrder();
}
}
}
ありがとうございます!
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[] { });
}
}
}
この意志は、事実上、すべてのパブリックインスタンスメソッドを取得します。この場合、適切なOOPに関するすべてのコメント(私が思うに、有効である、)、ここで少し、リフレクションベースの一例であるにも関わらず
を使用してください。 – Richard
あなたは 'Function1'、' Function2'、 'Function3'を持っていて、' Functions.Function1() 'を明示的に実行せずにそれらを呼びたいのですか?それが反映されたものです。しかし、何百ものメソッドがなければ、明示的に呼び出すほうがよいでしょう。何百ものメソッドがある場合は、おそらく全体のアプローチを再考する必要があります。 – mason
関数の順序を尋ねる代わりにOOPを学ぶ –