2017-09-15 18 views
-1

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に関するすべてのコメント(私が思うに、有効である、)、ここで少し、リフレクションベースの一例であるにも関わらず

+2

を使用してください。 – Richard

+0

あなたは 'Function1'、' Function2'、 'Function3'を持っていて、' Functions.Function1() 'を明示的に実行せずにそれらを呼びたいのですか?それが反映されたものです。しかし、何百ものメソッドがなければ、明示的に呼び出すほうがよいでしょう。何百ものメソッドがある場合は、おそらく全体のアプローチを再考する必要があります。 – mason

+7

関数の順序を尋ねる代わりにOOPを学ぶ –

答えて

2

タイプFから、名前順に並べ替え、パラメーターなしで実行します。この特定のケースでは、それが表示されます

こんにちはF1

しかし、一般走行時の「すべてのメソッド」、そして、さらに悪いことには、彼らのアルファベット順に依存し、なることです強く落胆した。

ようこそStackOverflow!

+0

私はこのBindingFlagsシステムについて何も知らないので、ここで何をしているのか分かりません。どのように機能しますか?あなたはここで何をしていますか? –

+0

私はコードを分割してコメントにしますが、https://msdn.microsoft.com/en-us/library/system.reflection.bindingflags(v=vs.110).aspxとhttps ://msdn.microsoft.com/en-us/library/4d848zkb(v = vs.110).aspx –

+0

もう一度ありがとう! –

-2

は、内蔵のものは何もありません...それは反射で簡単に構築された(しかし、無限再帰を避けるために、 `Main`を除外するように注意してください!)することができた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; 
     } 
+0

Askerは動的ソリューションを探しています –

+0

OPはどこで動的ソリューションを求めていますか?再びあなたは悪い仮定をしています。 – jdweng

+0

彼は「時間と時間を節約し、すべての機能を書き出し、非常に長いメインメソッドを作成する」と言います。 –

関連する問題