【问题标题】:"Importing" another class in C#在 C# 中“导入”另一个类
【发布时间】:2014-11-21 08:40:34
【问题描述】:

将 Visual Studio 2013 用于 Windows 应用程序,我在我的 MainForm 中有一些代码需要另一段代码才能运行,我正在尝试将这个必需的代码单独移动到另一个类中并导入它(使MainForm 类更易于理解)

当我将代码移到该类时,我似乎无法导入该类,以下是 coed 包含的一些内容:

static void WinEventProc(IntPtr hWinEventHook, uint eventType,
        IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    { .. }

还有一些

delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
    IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

所以那些通常在我的里面

namespace myClass
{
    public partial class MainForm : Form
    {

但我想将它们放在另一个类中并导入它们

【问题讨论】:

  • 你的意思是你想使用Inheritance
  • 可能但是MainForm已经继承了Form,如果可行我会寻找多个继承
  • 所以 Mainform : Form,myInterface 也不起作用,嗯

标签: c# class import visual-studio-2013


【解决方案1】:

您不需要仅仅因为它们在另一个类(同一个项目)中而“导入”它们。如果您使用不同的命名空间,那么您可以包含它,但您不需要。

例如,您可以将它们放在另一个类中(最好是静态类,因为它们是静态方法),然后从其他代码中调用它们...

namespace SameNamespace
{
    static class NewClass
    {
        [DllImport("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
    }
}

然后在您的表单代码中,您只需在需要时使用以下内容:

NewClass.GetWindowThreadProcessId(...);

如果要将类放在不同的命名空间中,例如:

namespace DifferentNamespace
{
    ...

然后你要么包括它,像这样:

using DifferentNamespace;

或者您可以在需要该函数时显式调用它:

 DifferentNamespace.NewClass.GetWindowThreadProcessId(...);

【讨论】:

  • 这解决了大部分问题,谢谢,但不知何故,像 EVENT_OBJECT_NAMECHANGE、EVENT_OBJECT_NAMECHANGE 之类的东西会出错,代码中仍然缺少一些东西
  • @user3795356:那些东西是什么?我在您的问题中没有看到它们
  • 是的,您应该将其标记为答案。这些错误来自您的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 2013-01-21
  • 1970-01-01
  • 2021-12-01
  • 2016-01-27
  • 2015-08-15
相关资源
最近更新 更多