【问题标题】:data structure of objects and methods in c#c#中对象和方法的数据结构
【发布时间】:2016-11-07 03:00:36
【问题描述】:

我是 C# 新手,对编程也很陌生。我需要关于过去一周一直试图弄清楚的话题的帮助。我有 3 个文件:

  1. 控制:这是一个界面,应该包含我的列表 方法
  2. ControlImpl : 这是接口的实现。

  3. Runtime:包含哪些main方法之间的绑定代码 和接口实现

  4. Test_main:我从哪里调用 运行时方法“调用”

问题:Control 文件中可以有任意数量的实例(例如:c、c1、c2 等),并且每个实例都应该能够调用 SetTime() 和 Nop() 方法.

我列出了SetTime()Nop() 的方法。但是如何将实例添加到列表中,以便每个实例在调用时都应该调用它的方法?

控制

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);
        }
   }

【问题讨论】:

  • 你的意思是有一个包含所有实例的列表来同时在所有实例上执行给定的方法吗?如果是这样,请查看 System.Collection.Generics 命名空间中的 List 对象。
  • @NahuelIanni Test_main 应该包含 Runtime.call(ID);因此,ID 被传递给运行时-> 调用方法,该方法应链接到相应的实例(即 c、c1、c2 等)和相应的方法(即 SetTime 或 Nop)。
  • @NahuelIanni 所以我想它应该需要像一棵树这样的东西。由实例组成的树,这些实例应该链接到方法。但是,我不确定这是否是正确的程序。

标签: c# list dictionary data-structures


【解决方案1】:

请查看以下解决方案,我们可以以此为基础来提出您的最终解决方案:

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();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多