字段初始值设定项不能引用非静态字段、方法或 属性

A field initializer cannot reference the non-static field, method, or property

我把下面的代码写成一个Form:

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Media;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ScreenShots
{
    public partial class ScreenShotConfigurationForm : Form
    {

        // ----- some other stuff (code, methods, etc...) is placed in here -----

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private LowLevelKeyboardProc _proc = HookCallback; // <<<< This is the row that is causing the error
        private IntPtr _hookID = IntPtr.Zero;

        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                if (vkCode == 44) // 44 is the key code of PrintScreen button
                {
                    MakeScreenShot();
                }
            }

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        private IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())

            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]

        private extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private extern IntPtr GetModuleHandle(string lpModuleName);
    }
}

在 Microsoft C# 2008 上出现以下错误:

字段初始值设定项不能引用非静态字段、方法或 属性 'ScreenShots.ScreenShotConfigurationForm.HookCallback(int, System.IntPtr, System.IntPtr)'

  1. 这是什么意思?
  2. 我该如何解决?

我看到的截图:

什么意思?

错误说:

A field initializer

它指的是字段 private LowLevelKeyboardProc _proc。这是一个非静态字段。初始化部分是 = HookCallback;.

cannot reference the non-static field, method, or property 'HookCallback'

HookCallback是一个非静态方法,字段初始化器显然是在引用它。

这里禁止的是用其他实例成员初始化实例成员。由于它们都已初始化 "at the same time" - 创建实例时 - 它们在初始化时不应相互引用。

这是 C# 编译器在理论上实际上可以解决的问题 - 规范定义了初始化程序 运行 的特定顺序 - 但无论出于何种原因,设计人员都采用了错误消息。

怎么解决的?

只有字段初始值设定项不允许访问实例成员。您 可以 在构造函数中访问实例成员,运行 紧跟在字段初始值设定项之后。因此,只需将初始化移动到您的构造函数(如 recommended by Microsoft):

private LowLevelKeyboardProc _proc;

public ScreenShotConfigurationForm()
{
    InitializeComponent();
    _proc = HookCallback;
}