Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅在一个类中实例化一个类_C#_Winapi - Fatal编程技术网

C# 仅在一个类中实例化一个类

C# 仅在一个类中实例化一个类,c#,winapi,C#,Winapi,我正试图通过将一些WinAPI调用卸载到另一个类中来让我的生活变得更轻松。这样做时,我会得到一个。。。。由于其保护级别而无法访问;问题是什么显而易见。例如:我有一个班知道如何制作鱼竿,但这个班应该是唯一一个知道如何制作鱼竿的班,我觉得其他班不应该 这是我目前的课程 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System

我正试图通过将一些
WinAPI
调用卸载到另一个类中来让我的生活变得更轻松。这样做时,我会得到一个
。。。。由于其保护级别而无法访问
;问题是什么显而易见。例如:我有一个班知道如何制作鱼竿,但这个班应该是唯一一个知道如何制作鱼竿的班,我觉得其他班不应该

这是我目前的课程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace AeonianControls.DLL
{
    /// <summary>
    /// Provides importing API functions and methods.
    /// </summary>
    public class APIHelper
    {
        /// <summary>
        /// Provides importing API functions and methods.
        /// </summary>
        public APIHelper()
        {

        }

        #region Imported DLL's
        /// <summary>
        /// Retrieves the device context for the entire window.
        /// </summary>
        /// <param name="hWnd">A handle to the window.</param>
        /// <returns>The handle to the device context for the window.</returns>
        [DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);
        /// <summary>
        /// Releases a device context.
        /// </summary>
        /// <param name="hWnd">A handle to the window whose DC is to be released.</param>
        /// <param name="hDC">A handle to the DC to be released.</param>
        /// <returns>The return value indicates whether the DC was released. If the DC was released, the return value is 1 otherwise 0.</returns>
        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
        /// <summary>
        /// Updates the specified rectangle or region in a window's client area.
        /// </summary>
        /// <param name="hWnd">A handle to the window to be redrawn.</param>
        /// <param name="lprc">A pointer to a RECT structure containing the coordinates, in device units, of the update rectangle. This parameter is ignored if the hrgnUpdate parameter identifies a region.</param>
        /// <param name="hrgn">A handle to the update region. If both the hrgnUpdate and lprcUpdate parameters are NULL, the entire client area is added to the update region.</param>
        /// <param name="flags">One or more redraw flags. This parameter can be used to invalidate or validate a window, control repainting, and control which windows are affected by RedrawWindow.</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprc, IntPtr hrgn, uint flags);
        #endregion

        #region Methods and Functions

        #endregion

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.InteropServices;
使用系统文本;
命名空间aeonincontrols.DLL
{
/// 
///提供导入API函数和方法。
/// 
公共类助理
{
/// 
///提供导入API函数和方法。
/// 
公共助理医生()
{
}
#区域导入的DLL
/// 
///检索整个窗口的设备上下文。
/// 
///窗户的把手。
///窗口的设备上下文的句柄。
[DllImport(“user32.dll”)]
静态外部IntPtr GetWindowDC(IntPtr hWnd);
/// 
///释放设备上下文。
/// 
///要释放其DC的窗口的句柄。
///要释放的DC的句柄。
///返回值指示DC是否已释放。如果DC已释放,则返回值为1,否则为0。
[DllImport(“user32.dll”)]
静态外部内部释放DC(IntPtr hWnd、IntPtr hDC);
/// 
///更新窗口客户端区域中的指定矩形或区域。
/// 
///要重绘的窗口的句柄。
///指向包含更新矩形坐标(以设备单位表示)的RECT结构的指针。如果hrgnUpdate参数标识区域,则忽略此参数。
///更新区域的句柄。如果hrgnUpdate和lprcUpdate参数都为NULL,则整个客户端区域将添加到更新区域。
///一个或多个重画标志。此参数可用于使窗口无效或验证窗口、控制重新绘制以及控制哪些窗口受重画窗口影响。
/// 
[DllImport(“user32.dll”)]
静态外部布尔重绘窗口(IntPtr hWnd、IntPtr lprc、IntPtr hrgn、uint标志);
#端区
#区域方法和函数
#端区
}
}
这是我面临的问题…

我需要访问这个类和另一个类中要使用的方法,但是我不希望它们在任何其他类中被实例化、使用或看到


我已经尝试将这些方法设置为public,这很好,但我不希望它们在我需要的类之外被看到。我尝试过将构造函数设置为私有,然后设置为嵌套类型,但没有成功。

您需要一个单例模式

public class APIHelper
{
    public readonly static APIHelper API = new APIHelper();
    private APIHelper()
    {
    }
    public void SomeMethod() { .. }
}
下面将使用哪一个

// Gain access through the API field
APIHelper.API.SomeMethod();

也许你只需要静态字段。您需要澄清您打算如何使用这个类

public static class APIHelper
{

    #region Imported DLL's
    /// <summary>
    /// Retrieves the device context for the entire window.
    /// </summary>
    /// <param name="hWnd">A handle to the window.</param>
    /// <returns>The handle to the device context for the window.</returns>
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);
    /// <summary>
    /// Releases a device context.
    /// </summary>
    /// <param name="hWnd">A handle to the window whose DC is to be released.</param>
    /// <param name="hDC">A handle to the DC to be released.</param>
    /// <returns>The return value indicates whether the DC was released. If the DC was released, the return value is 1 otherwise 0.</returns>
    [DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    /// <summary>
    /// Updates the specified rectangle or region in a window's client area.
    /// </summary>
    /// <param name="hWnd">A handle to the window to be redrawn.</param>
    /// <param name="lprc">A pointer to a RECT structure containing the coordinates, in device units, of the update rectangle. This parameter is ignored if the hrgnUpdate parameter identifies a region.</param>
    /// <param name="hrgn">A handle to the update region. If both the hrgnUpdate and lprcUpdate parameters are NULL, the entire client area is added to the update region.</param>
    /// <param name="flags">One or more redraw flags. This parameter can be used to invalidate or validate a window, control repainting, and control which windows are affected by RedrawWindow.</param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprc, IntPtr hrgn, uint flags);
    #endregion

}

我想这就是你想要的

/// <summary>
/// Provides importing API functions and methods.
/// </summary>
internal class APIHelper
{
/// <summary>
/// Provides importing API functions and methods.
/// </summary>
internal APIHelper()
{

}

#region Imported DLL's
/// <summary>
/// Retrieves the device context for the entire window.
/// </summary>
/// <param name="hWnd">A handle to the window.</param>
/// <returns>The handle to the device context for the window.</returns>
[DllImport("user32.dll")]
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
/// <summary>
/// Releases a device context.
/// </summary>
/// <param name="hWnd">A handle to the window whose DC is to be released.</param>
/// <param name="hDC">A handle to the DC to be released.</param>
/// <returns>The return value indicates whether the DC was released. If the DC was released, the return value is 1 otherwise 0.</returns>
[DllImport("user32.dll")]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// Updates the specified rectangle or region in a window's client area.
/// </summary>
/// <param name="hWnd">A handle to the window to be redrawn.</param>
/// <param name="lprc">A pointer to a RECT structure containing the coordinates, in device units, of the update rectangle. This parameter is ignored if the hrgnUpdate parameter identifies a region.</param>
/// <param name="hrgn">A handle to the update region. If both the hrgnUpdate and lprcUpdate parameters are NULL, the entire client area is added to the update region.</param>
/// <param name="flags">One or more redraw flags. This parameter can be used to invalidate or validate a window, control repainting, and control which windows are affected by RedrawWindow.</param>
/// <returns></returns>
[DllImport("user32.dll")]
internal static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprc, IntPtr hrgn, uint flags);
#endregion

#region Methods and Functions

#endregion

}
//
///提供导入API函数和方法。
/// 
内部类APIHelper
{
/// 
///提供导入API函数和方法。
/// 
内部助理人员()
{
}
#区域导入的DLL
/// 
///检索整个窗口的设备上下文。
/// 
///窗户的把手。
///窗口的设备上下文的句柄。
[DllImport(“user32.dll”)]
内部静态外部IntPtr GetWindowDC(IntPtr hWnd);
/// 
///释放设备上下文。
/// 
///要释放其DC的窗口的句柄。
///要释放的DC的句柄。
///返回值指示DC是否已释放。如果DC已释放,则返回值为1,否则为0。
[DllImport(“user32.dll”)]
内部静态外部内部释放DC(IntPtr hWnd、IntPtr hDC);
/// 
///更新窗口客户端区域中的指定矩形或区域。
/// 
///要重绘的窗口的句柄。
///指向包含更新矩形坐标(以设备单位表示)的RECT结构的指针。如果hrgnUpdate参数标识区域,则忽略此参数。
///更新区域的句柄。如果hrgnUpdate和lprcUpdate参数都为NULL,则整个客户端区域将添加到更新区域。
///一个或多个重画标志。此参数可用于使窗口无效或验证窗口、控制重新绘制以及控制哪些窗口受重画窗口影响。
/// 
[DllImport(“user32.dll”)]
内部静态外部布尔重绘窗口(IntPtr hWnd、IntPtr lprc、IntPtr hrgn、uint标志);
#端区
#区域方法和函数
#端区
}

它和你描述的不完全一样。internal关键字将阻止助手类中的方法在该类所在的程序集之外的可见性。这应该足够好了。如果你真的需要对其他人隐藏这些方法,那么就把这两个类单独放在一个程序集中。

有趣的是,从来没有想过单例模式。当我这样做时,我得到
。。。由于其保护级别而无法访问
。例如:
var hdc=APIHelper.API.GetWindowDC(this.Handle)这就是我使用它的方式。我调用的函数是:
[DllImport(“user32.dll”)]static extern int ReleaseDC(IntPtr hWnd,IntPtr hDC)如果我添加了public,那么这是另一个错误。什么是不可访问的?类、方法和
static
字段
API
都是公共的?我以为您想包装
extern
调用为API调用导入dll。不,我不想包装它们,但我不能叫它们…@DavidHeffernan,OP可以根据他的要求选择。无状态单例没有什么错,只是它在API中添加了一个不必要的层。问题不清楚是否有
/// <summary>
/// Provides importing API functions and methods.
/// </summary>
internal class APIHelper
{
/// <summary>
/// Provides importing API functions and methods.
/// </summary>
internal APIHelper()
{

}

#region Imported DLL's
/// <summary>
/// Retrieves the device context for the entire window.
/// </summary>
/// <param name="hWnd">A handle to the window.</param>
/// <returns>The handle to the device context for the window.</returns>
[DllImport("user32.dll")]
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
/// <summary>
/// Releases a device context.
/// </summary>
/// <param name="hWnd">A handle to the window whose DC is to be released.</param>
/// <param name="hDC">A handle to the DC to be released.</param>
/// <returns>The return value indicates whether the DC was released. If the DC was released, the return value is 1 otherwise 0.</returns>
[DllImport("user32.dll")]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// Updates the specified rectangle or region in a window's client area.
/// </summary>
/// <param name="hWnd">A handle to the window to be redrawn.</param>
/// <param name="lprc">A pointer to a RECT structure containing the coordinates, in device units, of the update rectangle. This parameter is ignored if the hrgnUpdate parameter identifies a region.</param>
/// <param name="hrgn">A handle to the update region. If both the hrgnUpdate and lprcUpdate parameters are NULL, the entire client area is added to the update region.</param>
/// <param name="flags">One or more redraw flags. This parameter can be used to invalidate or validate a window, control repainting, and control which windows are affected by RedrawWindow.</param>
/// <returns></returns>
[DllImport("user32.dll")]
internal static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprc, IntPtr hrgn, uint flags);
#endregion

#region Methods and Functions

#endregion

}