2016-07-05 4 views
1

私はC#には新しく、プログラミングにはかなり新しいです。私は過去の週から把握しようとしているトピックについて助けが必要です。オブジェクトとメソッドのデータ構造をC#

  1. コントロール:これはインタフェースであり、私の 方法
  2. ControlImplのリストを含める必要があります。このインタフェースのimplementaionを私は3つのファイルを持っています。

  3. ランタイム:私は ランタイムメソッド呼び出しどこからが問題

'と呼ぶ':mainメソッド とインタフェースimplementaion

  • Test_main間の結合コードが含まれてい(ex:c、c1、c2などの)インスタンスはControlファイルにいくつでも存在でき、各インスタンスはSetTime()およびNop()メソッドを呼び出すことができます。

    SetTime()Nop()のリストを作成しました。しかし、インスタンスをリストに追加して、呼び出された各インスタンスがそのメソッドを呼び出すようにするにはどうすればよいですか?

    CONTROL

    namespace create_interface 
    { 
    interface Control 
    { 
    
        void SetTime(params object[] paramsArr); 
        void Nop(params object[] paramsArr); 
    } 
    
    public class CM 
    { 
        Control c = new ControlImpl(); 
        public List<object> ControlMain() 
        { 
    
    
         List<object> methods = new List<object>(); 
         methods.Add(new Action<object[]>(c.SetTime));         
         methods.Add(new Action<object[]>(c.Nop));      
         return methods; 
        } 
    
    } 
    
    } 
    

    ControlImpl:

    namespace create_interface 
    { 
        public class ControlImpl : Control 
        { 
        void Control.SetTime(params object[] paramsArr)      
        { 
         Console.WriteLine("inside Control.SetTime {0} ", paramsArr[0]); 
    
        } 
    
        void Control.Nop(params object[] paramsArr)      
        { 
         Console.WriteLine("inside Control.Nop "); 
    
        } 
        } 
    } 
    

    ランタイム:

    namespace create_interface 
        { 
    
    public class runtime 
    { 
    public void call(params object[] methodparams) 
        { 
    
    
         if ((methodparams[0].Equals(0)) || (methodparams[0].Equals(1))) 
         { 
          //List<Control> objectlists = cmObject.ControlObjectList(); 
    
          List<object> methods = cmObject.ControlMain(); 
          //Console.WriteLine(methods.Count); 
          Action<object[]> method = (Action<object[]>)methods[(int)methodparams[0]];  //object[] 
          object[] args = new object[] { methodparams[1] }; 
          method(args); 
         }    
    
         else 
          Console.WriteLine("wrong ID number entered"); 
        } 
    

    Test_main:

    namespace create_interface 
    { 
        class test_main 
        { 
    
        static void Main(string[] args) 
        { 
         long time; 
         CallingFunc CF = new CallingFunc(); 
    
    
         Console.WriteLine("enter method ID"); 
         int methodID = Convert.ToInt32(Console.ReadLine()); 
         try 
         { 
          switch (methodID) 
          { 
           case 0: 
            Console.WriteLine("enter the time in long"); 
            time = Convert.ToInt64(Console.ReadLine()); 
            CF.call(methodID, time); 
            break; 
    
           case 1: 
            CF.call(methodID, null); 
            break; 
    
           default: 
            Console.WriteLine("you entered wrong method ID or parameters"); 
            break; 
          } 
         } 
         catch (Exception e) 
         { 
          Console.WriteLine(e.Message); 
         } 
        } 
    
  • +0

    あなたはすべての指定されたメソッドを実行するためのすべてのインスタンスのリストを有することを意味くださいそれらは同時にですか?その場合は、System.Collection.Generics名前空間のリストオブジェクトを見てください。 –

    +0

    @NahuelIanni Test_mainにはRuntime.call(ID)が含まれている必要があります。したがってIDはそれぞれのインスタンス(すなわち、c、c1、c2など)およびそれぞれのメソッド(すなわちSetTimeまたはNop)にリンクする必要があるruntime-> callメソッドに渡されます。 –

    +0

    @NahuelIanniだから私はそれが木のような何かを必要とすると思っていた。インスタンスとこれらのインスタンスで構成されるツリーは、メソッドにリンクする必要があります。しかし、これが正しい手順であるかどうかはわかりません。 –

    答えて

    0

    次の解決方法を見て、私たちはあなたの最終的な解決策を考え出すためにベースとしてそれを使用することができますしてください:

    using System; 
    using System.Collections.Generic; 
    using System.Collections.ObjectModel; 
    using System.Linq; 
    
    namespace StackOverflow38200633 
    { 
        class Program 
        { 
         static void Main(string[] args) 
         { 
          Collection<IControl> controls = new Collection<IControl>(); 
          controls.Add(ControlFactory.Create()); 
          controls.Add(ControlFactory.Create()); 
          controls.Add(ControlFactory.Create()); 
    
          ControlManager manager = new ControlManager(controls); 
    
          Console.WriteLine("Enter method ID:"); 
          int methodID = Convert.ToInt32(Console.ReadLine()); 
    
          try 
          { 
           switch(methodID) 
           { 
            case 0: 
             Console.WriteLine("Enter the time in long: "); 
             long time = Convert.ToInt64(Console.ReadLine()); 
             manager.InvokeAllSetTime(time); 
             break; 
            case 1: 
             manager.InvokeAllNop(); 
             break; 
            default: 
             Console.WriteLine("You entered wrong method ID or parameters"); 
             break; 
           } 
          } 
          catch(Exception e) 
          { 
           Console.WriteLine(e.Message); 
          } 
          Console.WriteLine("Press any key to exit..."); 
          Console.ReadKey(); 
         } 
        } 
    
        public interface IControl 
        { 
         void SetTime(long time); 
         void Nop(); 
        } 
    
        public class ConcreteControl : IControl 
        { 
         public void SetTime(long time) 
         { 
          Console.WriteLine("inside Control.SetTime {0} ", time); 
         } 
         public void Nop() 
         { 
          Console.WriteLine("inside Control.Nop "); 
         } 
        } 
    
        public class ControlManager 
        { 
         public void InvokeAllSetTime(long time) 
         { 
          foreach(IControl control in _controls) control.SetTime(time); 
         } 
         public void InvokeAllNop() 
         { 
          foreach(IControl control in _controls) control.Nop(); 
         } 
         public ControlManager(Collection<IControl> controls) 
         { 
          _controls = controls; 
         } 
         public Collection<IControl> _controls { get; private set; } 
        } 
    
        public static class ControlFactory 
        { 
         public static IControl Create() 
         { 
          return new ConcreteControl(); 
         } 
        } 
    }