C# 从另一个文件访问父类方法

发布于 2024-09-27 20:07:20 字数 996 浏览 2 评论 0原文

我想知道是否有一种方法可以让类的实例访问其父类的方法,同时在运行时保留数据。 (这有意义吗?)

如果有多个 COM 端口设置,我有一个 ConnectionManager,它会生成多个连接器(负责 COM 端口通信)。

file1.cs

namespace connector
{
public class connectormanager : idisposable
{
  public connectormanager(comport1, comport2, retries, delay)
  {
    // create dictionary that maps COMPorts with devices
    // **foreach spawn connector (if com port range is given)**
  }

  public searchchannel()
  {
    // search existing com ports
  }
}
}


file2.cs

namespace connector
{
  public class connector : idisposable
{
  public void connector(port, retries, delay)
  {
  // there is a timer with a timeout event defined
  }

  // com port read/write operations

  void _timer_Elapsed()
  {
    // **i want to access searchchannel**
    // problem is if i create a new instance of connectionmanager
    // i lose all the dictionary stuff
  }
}
}

这两个文件位于同一解决方案和项目下。我想要做的是从 file2.cs 中的 _timer_Elapsed 访问搜索通道,而不创建连接器管理器的新实例。

非常感谢任何建议或帮助。

I was wondering if there was a way for an instance of a class to access its parent class's method(s) while preserving the data during runtime. (Did that make any sense?)

I have a ConnectionManager that spawns several Connectors (which is in charge of COM port communications) if there is a multiple COM Port setup.

file1.cs

namespace connector
{
public class connectormanager : idisposable
{
  public connectormanager(comport1, comport2, retries, delay)
  {
    // create dictionary that maps COMPorts with devices
    // **foreach spawn connector (if com port range is given)**
  }

  public searchchannel()
  {
    // search existing com ports
  }
}
}


file2.cs

namespace connector
{
  public class connector : idisposable
{
  public void connector(port, retries, delay)
  {
  // there is a timer with a timeout event defined
  }

  // com port read/write operations

  void _timer_Elapsed()
  {
    // **i want to access searchchannel**
    // problem is if i create a new instance of connectionmanager
    // i lose all the dictionary stuff
  }
}
}

both files are under the same solution and project. what I want to do is access search channel from _timer_Elapsed in file2.cs without creating a new instance of the connector manager.

Any advice or help greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给不了的爱 2024-10-04 20:07:20

你的 ConnectorManager 对我来说就像一个工厂。您不应单独调用 Connector 构造函数,而应在 ConnectorManager 类中添加 CreateConnector 函数

。在此工厂中,您将调用 Connector 构造函数,并将 ConnectorManager 实例传递给它。

连接器将存储此实例,并根据需要使用它。

public class ConnectorManager
{
  public Connector CreateConnector()
  {
      return new Connector(comport1, comport2, retries, delay,this);
  }
  [...]
}

public class Connector
{
  private ConnectorManager connectorManager;

  internal Connector(int comport1, int comport2, int retries, int delay, ConnectorManager manager)
  {
     connectorManager = manager;
     [...]
   }
   [...]
}

请注意,连接器构造函数是内部的,您不应从程序集外部调用它。你永远不应该在不使用 ConnectorManager.CreateConnector() 的情况下实例化一个 Connector

如果仔细阅读你的 ConnectorManager 构造函数,单例模式不适合你的需要,是否可以为不同的 comport 配置实例化多个 ConnectorManager,这是真的吗?

Your ConnectorManager looks like a factory for me. You shouldn't call Connector constructor by itself, but rather add a CreateConnector function in the ConnectorManager class

In this factory, you will call the Connector constructor, and pass to it the ConnectorManager instance.

The connector will store this instance, and use it as required.

public class ConnectorManager
{
  public Connector CreateConnector()
  {
      return new Connector(comport1, comport2, retries, delay,this);
  }
  [...]
}

public class Connector
{
  private ConnectorManager connectorManager;

  internal Connector(int comport1, int comport2, int retries, int delay, ConnectorManager manager)
  {
     connectorManager = manager;
     [...]
   }
   [...]
}

Notice that the Connector constructor is internal, you shouldn't call it from outside your assembly. You should NEVER instantiate a Connector without using ConnectorManager.CreateConnector()

If a read well your ConnectorManager constructor, the singleton pattern won't suit your need, are several ConnectorManager could be instantiated for different comport configuration, is it true?

盗梦空间 2024-10-04 20:07:20

您可以将 searchchannel 设为静态。

此处阅读相关内容。

You could make searchchannel static.

Read about it here.

霞映澄塘 2024-10-04 20:07:20

您可以将 ConnectionManager 作为 Singleton 对象,并且由于您的连接器类需要 ConnectionManager 的此依赖项,因此您可以通过属性注入来注入它。

这是代码

 public interface IConnectionManager
         {
             void searchchannel() ;
         }


            public class connectormanager : IDisposable , IConnectionManager
               {
                     public static readonly connectormanager ConnectionManager = new connectormanager();

                      private connectormanager() 
                      { 
                        // create dictionary that maps COMPorts with devices 
                        // **foreach spawn connector (if com port range is given)** 
                      } 

                      private void searchchannel() 
                      { 
                        // search existing com ports 
                      } 



                    void  IConnectionManager.searchchannel()
                    {
                        searchchannel();
                    }

                 } 



         public class connector : IDisposable 
         {
               IConnectionManager _manager = null;
               public void connector(port, retries, delay)
               {
               }

             private IConnectionManager myManager
             {
                 get
                 {
                     return _manager;
                 }

                 set
                 {
                    myManager = connectormanager.ConnectionManager;
                 }

             }

             void _timer_Elapsed() 
             {
                   myManager.searchchannel();
             }


         }

You can make ConnectionManager as a Singleton object and since your connector class requires this dependency of ConnectionManager , you can inject it via property injection.

here is the code

 public interface IConnectionManager
         {
             void searchchannel() ;
         }


            public class connectormanager : IDisposable , IConnectionManager
               {
                     public static readonly connectormanager ConnectionManager = new connectormanager();

                      private connectormanager() 
                      { 
                        // create dictionary that maps COMPorts with devices 
                        // **foreach spawn connector (if com port range is given)** 
                      } 

                      private void searchchannel() 
                      { 
                        // search existing com ports 
                      } 



                    void  IConnectionManager.searchchannel()
                    {
                        searchchannel();
                    }

                 } 



         public class connector : IDisposable 
         {
               IConnectionManager _manager = null;
               public void connector(port, retries, delay)
               {
               }

             private IConnectionManager myManager
             {
                 get
                 {
                     return _manager;
                 }

                 set
                 {
                    myManager = connectormanager.ConnectionManager;
                 }

             }

             void _timer_Elapsed() 
             {
                   myManager.searchchannel();
             }


         }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文