Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在属性'中调用而不使用Func;s.NET2.0的getter_C#_.net_Invoke_Getter_Invokerequired - Fatal编程技术网

C# 在属性'中调用而不使用Func;s.NET2.0的getter

C# 在属性'中调用而不使用Func;s.NET2.0的getter,c#,.net,invoke,getter,invokerequired,C#,.net,Invoke,Getter,Invokerequired,我想在getter中使用Invoke,在使用.NET2.0而不是例如4.0时如何使用它?对于.Net>2.0,我们可以使用Func,什么是.Net 2.0的替代品 下面是.Net 4.0的示例(来自) 公共应用程序视图模型选定应用程序 { 得到{ if(this.invokererequired) { 返回(ApplicationViewModel)this.Invoke(newfunc(()=>this.SelectedApplication)); } 其他的 { return _appli

我想在getter中使用Invoke,在使用.NET2.0而不是例如4.0时如何使用它?对于.Net>2.0,我们可以使用
Func
,什么是.Net 2.0的替代品

下面是.Net 4.0的示例(来自)

公共应用程序视图模型选定应用程序
{
得到{
if(this.invokererequired)
{
返回(ApplicationViewModel)this.Invoke(newfunc(()=>this.SelectedApplication));
}
其他的
{ 
return _applicationsCombobox.SelectedItem作为ApplicationViewModel;
}
}
}

使用委托,委托是一种类型化的函数指针。
这里有更多阅读:

使用委托,它们是一种类型化的函数指针。
这里有更多的阅读:

由于您使用的是.NET 2.0,您将无法使用
Func
委托,但您可以使用该委托

您将无法在.NET2.0中使用lambda表达式语法,但可以使用“匿名委托”语法(这几乎是相同的),如下面的代码示例所示

从非UI线程查询UI控件中的数据通常是不常见的事情;通常,您的UI控件会触发在UI线程上执行的事件,因此您可以从UI控件中收集当时需要的数据,然后将这些数据传递给其他函数,这样您就不必担心调用了

但是,在您的情况下,您应该能够执行以下操作:

public ApplicationViewModel SelectedApplication
{
    get
    {
        if (this.InvokeRequired)
        {
            ApplicationViewModel value = null; // compiler requires that we initialize this variable
            // the call to Invoke will block until the anonymous delegate has finished executing.
            this.Invoke((MethodInvoker)delegate
            {
                // anonymous delegate executing on UI thread due calling the Invoke method
                // assign the result to the value variable so that we can return it.
                value = _applicationsCombobox.SelectedItem as ApplicationViewModel;
            });
            return value;
        }
        else
        {
            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
        }
    }
}
编辑:现在我看了你的.NET4.0代码示例,也看了Invoke函数,我看到它如何返回一个值(我以前没有理由使用它)

MethodInvoker委托不需要返回值,但正如@haiyyu所指出的,您可以定义自己的委托。例如,您只需要定义自己的
Func
delegate,原始代码可能可以正常工作:

// this is all that is required to declare your own Func<TResult> delegate.
delegate TResult Func<TResult>();

由于您使用的是.NET 2.0,因此您将无法使用
Func
委托,但您可以使用该委托

您将无法在.NET2.0中使用lambda表达式语法,但可以使用“匿名委托”语法(这几乎是相同的),如下面的代码示例所示

从非UI线程查询UI控件中的数据通常是不常见的事情;通常,您的UI控件会触发在UI线程上执行的事件,因此您可以从UI控件中收集当时需要的数据,然后将这些数据传递给其他函数,这样您就不必担心调用了

但是,在您的情况下,您应该能够执行以下操作:

public ApplicationViewModel SelectedApplication
{
    get
    {
        if (this.InvokeRequired)
        {
            ApplicationViewModel value = null; // compiler requires that we initialize this variable
            // the call to Invoke will block until the anonymous delegate has finished executing.
            this.Invoke((MethodInvoker)delegate
            {
                // anonymous delegate executing on UI thread due calling the Invoke method
                // assign the result to the value variable so that we can return it.
                value = _applicationsCombobox.SelectedItem as ApplicationViewModel;
            });
            return value;
        }
        else
        {
            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
        }
    }
}
编辑:现在我看了你的.NET4.0代码示例,也看了Invoke函数,我看到它如何返回一个值(我以前没有理由使用它)

MethodInvoker委托不需要返回值,但正如@haiyyu所指出的,您可以定义自己的委托。例如,您只需要定义自己的
Func
delegate,原始代码可能可以正常工作:

// this is all that is required to declare your own Func<TResult> delegate.
delegate TResult Func<TResult>();

您可以创建自己的委托。您可以创建自己的委托。我可以在属性的getter中创建它吗?是的,委托是一种变量类型,可以通过与任何其他变量相同的方式传递它。我可以在属性的getter中创建它吗?是的,委托是一种变量类型,它的传递方式与任何其他变量的传递方式相同。