C# 将泛型类型(抽象类的子类)传递给func

C# 将泛型类型(抽象类的子类)传递给func,c#,C#,我正在编写一个应用程序来检查条件(即bool是真是假),但它可以将任何对象作为func的参数,func派生自HtmlControl 我的func如下所示: Func<HtmlControl, bool> func Method1("Next parameter is a parameter to the func. If I don't write the cast, the compiler will think its the abstract HtmlControl cl

我正在编写一个应用程序来检查条件(即bool是真是假),但它可以将任何对象作为func的参数,func派生自HtmlControl

我的func如下所示:

Func<HtmlControl, bool> func
Method1("Next parameter is a parameter to the func. If I don't write the cast, the   compiler will think its the abstract HtmlControl class, right?", (HtmlImage) img => img.Alt.Length == 0);
然而,这是一个错误

(语音标记中的内容是这个问题的一部分)

当我为以func为参数的方法编写func时,如何传入抽象类的派生类型

从Windows页面代码隐藏调用/使用该逻辑。我将该页面设置为通用页面,如下所示:

  public partial class Window1<T> : Window where T : HtmlControl, new()
public部分类Window1:Window,其中T:HtmlControl,new()
但是我得到一个错误,说InitializeComponent在当前上下文中不存在

(HtmlImage)img=>img.Alt.Length==0

你试图将整个lambda表达式转换为HtmlImage。。。如果要指定lambda expression参数的类型,请执行以下操作:

(HtmlImage img) => img.Alt.Length == 0
(HtmlImage img) => img.Alt.Length == 0