草庐IT

iob_func

全部标签

c# - 我可以使用带输出参数的 Action<> 或 Func<> 吗?

我有一个带有out参数的方法,我想指向一个Action或Func(或其他类型的委托(delegate))就可以了。这很好用:staticvoidFunc(inta,intb){}Actionaction=Func;然而这不是staticvoidOutFunc(outinta,outintb){a=b=0;}Actionaction=OutFunc;//loadsofcompileerrors这可能是重复的,但搜索“outparameter”并不是特别有效。 最佳答案 Action和Func具体不带out或者ref参数。然而,他们只是

c# - 我可以在 Action 或 Func 委托(delegate)中使用参数吗?

当我尝试在Action委托(delegate)中使用参数时...privateActionWriteToLogCallBack;我收到了这个设计时错误:Invalidtoken'params'inclass,struct,orinterfacememberdeclaration任何帮助! 最佳答案 这个解决方法怎么样?privateActionwriteToLogCallBack;publicvoidWriteToLogCallBack(strings,paramsobject[]args){if(writeToLogCallBac

c# - 我可以在 Action 或 Func 委托(delegate)中使用参数吗?

当我尝试在Action委托(delegate)中使用参数时...privateActionWriteToLogCallBack;我收到了这个设计时错误:Invalidtoken'params'inclass,struct,orinterfacememberdeclaration任何帮助! 最佳答案 这个解决方法怎么样?privateActionwriteToLogCallBack;publicvoidWriteToLogCallBack(strings,paramsobject[]args){if(writeToLogCallBac

c# - 将 Func 委托(delegate)与 Async 方法结合使用

我正在尝试将Func与异步方法结合使用。我收到一个错误。Cannotconvertasynclambdaexpressiontodelegatetype'Func'.Anasynclambdaexpressionmayreturnvoid,TaskorTask,noneofwhichareconvertibleto'Func'.下面是我的代码:publicasyncTaskCallAsyncMethod(){Console.WriteLine("CallingYoutube");HttpClientclient=newHttpClient();varresponse=awaitclie

c# - 将 Func 委托(delegate)与 Async 方法结合使用

我正在尝试将Func与异步方法结合使用。我收到一个错误。Cannotconvertasynclambdaexpressiontodelegatetype'Func'.Anasynclambdaexpressionmayreturnvoid,TaskorTask,noneofwhichareconvertibleto'Func'.下面是我的代码:publicasyncTaskCallAsyncMethod(){Console.WriteLine("CallingYoutube");HttpClientclient=newHttpClient();varresponse=awaitclie

c# - 如何将 Expression<Func<T, bool>> 转换为 Predicate<T>

我有一个接受Expression>的方法作为参数。我想将它用作List.Find()方法中的谓词,但我似乎无法将其转换为List采用的谓词。您知道执行此操作的简单方法吗?publicIListFind(Expression>expression)whereT:class,new(){varlist=GetList();varpredicate=[whatgoesheretoconvertexpression?];returnlist.Find(predicate);}更新结合tvanfosson和280Z28的答案,我现在正在使用这个:publicIListFind(Expressio

c# - 如何将 Expression<Func<T, bool>> 转换为 Predicate<T>

我有一个接受Expression>的方法作为参数。我想将它用作List.Find()方法中的谓词,但我似乎无法将其转换为List采用的谓词。您知道执行此操作的简单方法吗?publicIListFind(Expression>expression)whereT:class,new(){varlist=GetList();varpredicate=[whatgoesheretoconvertexpression?];returnlist.Find(predicate);}更新结合tvanfosson和280Z28的答案,我现在正在使用这个:publicIListFind(Expressio

c# - 带有 ref 变量的 Func 委托(delegate)

publicobjectMethodName(reffloaty){//elided}如何为该方法定义一个Func委托(delegate)? 最佳答案 它不能通过Func来完成,但你可以为它定义一个自定义的delegate:publicdelegateobjectMethodNameDelegate(reffloaty);使用示例:publicobjectMethodWithRefFloat(reffloaty){returnnull;}publicvoidMethodCallThroughDelegate(){MethodName

c# - 带有 ref 变量的 Func 委托(delegate)

publicobjectMethodName(reffloaty){//elided}如何为该方法定义一个Func委托(delegate)? 最佳答案 它不能通过Func来完成,但你可以为它定义一个自定义的delegate:publicdelegateobjectMethodNameDelegate(reffloaty);使用示例:publicobjectMethodWithRefFloat(reffloaty){returnnull;}publicvoidMethodCallThroughDelegate(){MethodName

c# - 从 Expression<Func<TModel,TProperty>> 获取字符串形式的属性

我使用一些被序列化的强类型表达式,以允许我的UI代码具有强类型排序和搜索表达式。这些是类型Expression>并按原样使用:SortOption.Field=(p=>p.FirstName);.对于这个简单的案例,我已经让它完美地工作了。我用来解析“FirstName”属性的代码实际上是在我们使用的第三方产品中重用了一些现有功能,并且效果很好,直到我们开始使用深层嵌套的属性(SortOption.Field=(p=>p.Address.State.Abbreviation);)。此代码在支持深层嵌套属性的需要方面有一些非常不同的假设。至于这段代码的作用,我不是很理解,与其修改那段代码