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

时间:2017-02-28 03:30:09

标签: c# winapi

我试图通过将我的一些WinAPI调用加载到另一个类中来使我的生活变得更轻松。这样做的时候,我得到.... is inaccessible due to its protection level;很明显问题是什么。例如:我有一个课程,知道如何制作钓竿,但这个班级应该是唯一知道如何制作它的班级,而我觉得其他班级不应该。

这是我目前的课程......

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

    }
}

以下是我面临的问题......

我需要访问此类以及另一个类中要使用的方法, 希望它们可以在任何类中实例化其他课程,使用或看过。

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

2 个答案:

答案 0 :(得分:0)

你需要一个单身人士模式

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

}

现在用作

var ptr = APIHelper.GetWindowDC(hdc);

答案 1 :(得分:0)

我认为这就是你想要的......

/// <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

}

它并不完全符合您的描述。 internal关键字将阻止helper类中方法在类所在的程序集之外的可见性。这应该足够好了。如果你真的需要隐藏其他人的这些方法,那么将两个类放在一个程序集中。