Lambda 表达式 - 参数未知?

发布于 2024-11-24 14:09:26 字数 383 浏览 1 评论 0原文

我已经声明了这个类:

public class SimpleArea<T> where T: Control
{
    private T control;

    public SimpleArea(T control)
    {
        this.control = control;
    }

}

在我的主程序上,我想做这样的事情:

var SimpleAreaPanel = SimpleArea<Panel>(p => { p.Height= 150; })

问题是他无法定义“p”的类型,智能感知显示“参数???p”

我该如何完成这个指令?

I've this class declared:

public class SimpleArea<T> where T: Control
{
    private T control;

    public SimpleArea(T control)
    {
        this.control = control;
    }

}

And on my Main Program, i want to do something like this:

var SimpleAreaPanel = SimpleArea<Panel>(p => { p.Height= 150; })

The problem is that he can't define the type of "p" the intellisense shows "Parameter ???p"

How can i accomplish this instruction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榆西 2024-12-01 14:09:26

您的构造函数不接受 lambda - 它接受一个 T 实例,因此是一个 Panel。要么它一个Panel,或者编写一个可以采用该表达式的构造函数 - 可能是一个Action

就我个人而言,我怀疑你的意思很简单:

new Panel {Height = 150}

这是一个对象初始值设定项,而不是 lambda - 即

var SimpleAreaPanel = new SimpleArea<Panel>(
    new Panel { Height= 150 });

Your constructor doesn't take a lambda - it takes a T instance, so a Panel. Either give it a Panel, or write a constucor that can take that expression - maybe an Action<T>.

Personally, I suspect you mean simply:

new Panel {Height = 150}

which is an object initializer, not a lambda - i.e.

var SimpleAreaPanel = new SimpleArea<Panel>(
    new Panel { Height= 150 });
冬天旳寂寞 2024-12-01 14:09:26

如果我理解正确的话,你根本不需要使用 lambda 。

var SimpleAreaPanel = new SimpleArea<Panel>(new Panel{Height = 150});

If I understand correctly, you don't need to use lambda at all.

var SimpleAreaPanel = new SimpleArea<Panel>(new Panel{Height = 150});
孤檠 2024-12-01 14:09:26

MB你需要这样的东西:

class SimpleArea<T> where T : Control, new()
{
    public T control;

    public SimpleArea(Action<T> action)
    {
        control = new T();
        action(control);
    }
}

所以你可以写:

var SimpleAreaPanel = new SimpleArea<Panel>(p => { p.Height = 150; });

但我不知道为什么......

Mb you need something like this:

class SimpleArea<T> where T : Control, new()
{
    public T control;

    public SimpleArea(Action<T> action)
    {
        control = new T();
        action(control);
    }
}

So you can write:

var SimpleAreaPanel = new SimpleArea<Panel>(p => { p.Height = 150; });

But I don't know what for...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文