ref 或 out 参数必须是可赋值变量?

A ref or out argument must be an assignable variable?

错误:

A ref or out argument must be an assignable variable

代码:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class OAKListView : ListView
{
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);

        this.WndProc(ref new Message()
        {
            HWnd = this.Handle,
            Msg = 4150,
            LParam = (IntPtr)43,
            WParam = IntPtr.Zero
        });
    }
}

显示错误

this.WndProc(ref new Message()

错误解释得很清楚。您需要一个可分配的变量

protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    var message = new Message()
    {
        HWnd = this.Handle,
        Msg = 4150,
        LParam = (IntPtr)43,
        WParam = IntPtr.Zero
    };
    this.WndProc(ref message);
}

您的 ref 参数不是可赋值变量。创建 Message class 的新实例和作为参考传递不应同时进行。调用方法应该填充内存中的某个位置。你的电话里没有这样的东西。这将编译:

var message = new Message()
{
    HWnd = this.Handle,
    Msg = 4150,
    LParam = (IntPtr)43,
    WParam = IntPtr.Zero
});

this.WndProc(ref message);