无法将lambda表达式转换为类型“ System.Delegate”,因为它不是委托类型 [英] Cannot convert lambda expression to type 'System.Delegate', Because it is not a delegate type

查看:340
本文介绍了无法将lambda表达式转换为类型“ System.Delegate”,因为它不是委托类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为class1定义了一个依赖项属性,该属性引发一个事件。我不知道为什么会给我这个错误无法将lambda表达式转换为类型'System.Delegate'

I define a dependency Property for my class1, which raise an event. I don't know why it give me this error "Cannot convert lambda expression to type 'System.Delegate'"

        public static readonly DependencyProperty class1Property =
        DependencyProperty.Register("class1Property", typeof(Class1), typeof(UserControl1), new PropertyMetadata(null));

    public Class1 class1
    {
        get
        {
            return Dispatcher.Invoke((() => GetValue(class1Property)))as Class1;
        }
        set
        {
            Dispatcher.Invoke(new Action(() => { SetValue(class1Property, value); }));
        }
    }

非常简单的Class1代码:

Very simple Class1 code :

    public class Class1
{
    public delegate void myhandler(object sender, EventArgs e);
    public event myhandler test;


    public void connection()
    {
        test(this, new EventArgs());
    }

}


推荐答案

恕我直言,通常最好解决单个属性之外的跨线程需求。属性本身应该很简单,只需调用 GetValue() SetValue()。换句话说,属性获取器或设置器根本不需要调用 Dispatcher.Invoke()

IMHO, it is generally better to address cross-thread needs outside of individual properties. The properties themselves should be simple, just calling GetValue() and SetValue(). In other words, a property getter or setter should not need to call Dispatcher.Invoke() at all.

就是说,在您的代码示例中,您在属性getter中看到了您要询问的错误,因为编译器没有足够的信息来推断正确的委托类型。 Dispatcher.Invoke()方法只是将基类 Delegate 作为其参数。但是该类没有固有的签名,编译器需要该签名才能将lambda表达式自动转换为适当的匿名方法和匹配的委托实例。

That said, in your code example, you are seeing the error you're asking about in the property getter, because the compiler does not have enough information to infer the correct delegate type. The Dispatcher.Invoke() method simply takes as its parameter the base class Delegate. But this class has no inherent signature, which the compiler would need in order to automatically translate the lambda expression to an appropriate anonymous method and matching delegate instance.

在设置器中没有类似的错误。这是因为您已经通过使用 Action 类型的构造函数来显式提供了委托类型。如果您将getter代码更改为更类似于setter,它将可以使用。

Note that there is not a similar error in the setter. This is because you have provided the delegate type explicitly, through the use of the Action type's constructor. If you change the getter code to look more like the setter, it will work.

您可以选择几种语法,但这似乎与语法最接近基于设置器,您似乎更喜欢:

There are a few different syntaxes you could choose from, but this seems closest to the syntax you seem to prefer, based on the setter:

get
{
    return Dispatcher.Invoke(
        new Func<object>(() => GetValue(class1Property))) as Class1;
}



参见相关讨论,例如为什么将lambda表达式作为纯委托参数提供时必须强制转换(如果您在Stack Overflow中搜索错误消息,则说明再次看到,您会发现一些相关问题。)


See related discussion at e.g. Why must a lambda expression be cast when supplied as a plain Delegate parameter (if you search Stack Overflow for the error message you're seeing, you'll find a few related questions).

这篇关于无法将lambda表达式转换为类型“ System.Delegate”,因为它不是委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆