开发者

Lambda Expression - Parameter cannot be known?

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 acco开发者_运维技巧mplish this instruction?


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 });


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

var SimpleAreaPanel = new SimpleArea<Panel>(new Panel{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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜