我正在尝试在lambda表达式中使用方法组,如下所示:
public class Foo { public void Hello(string s) { } }
void Test()
{
// this works as long as Hello has a void return type
Func<Foo, Action<string>> a = foo => foo.Hello;
}
Run Code Online (Sandbox Code Playgroud)
当我改变的返回类型Hello
来int
,不过,我得到
'Bar.Hello(string)'的返回类型错误.
我试过用Func
它代替Action
,但这似乎阻止我使用方法组语法.
有任何想法吗?
(我的目标,fwiw,是能够引用具有不同返回类型和大量字符串参数的众多方法.我甚至不想打电话给它们 - 我只想反思它们的属性.我喜欢安全性然而,lambda只是键入方法名称字符串.)
编辑:澄清我想要使用的原因Action<string>
:int
在我的例子中可能是多种类型中的任何一种.我尝试模仿那种类型 -
void Set<T>(Func<Foo, Func<string, T>> a) { }
void Test() { Set(foo => foo.Hello); } // fails
Run Code Online (Sandbox Code Playgroud)
- 但是编译器无法派生T
(大概是出于同样的原因我不能在返回类型上重载?).
还有其他想法吗?只要我能让编译器检查该方法组的名称,我就不会在这种情况下反对一些疯狂的反思.
下面的第二个测试方法无法编译(无法将lambda表达式转换为目标类型D1
).这是否意味着(非泛型)委托逆转不适用于lambda表达式?
[TestFixture]
public class MyVarianceTests
{
private abstract class Animal {}
private class Tiger : Animal {}
private delegate Type D1(Tiger tiger);
private static Type M1(Animal animal)
{
return animal.GetType();
}
[Test]
public void ContravariantDelegateWithMethod()
{
D1 func = M1;
Type result = func(new Tiger());
Assert.AreEqual(result, typeof (Tiger));
}
[Test]
public void ContravariantDelegateWithLambda()
{
D1 func = (Animal animal) => animal.GetType();
Type result = func(new Tiger());
Assert.AreEqual(result, typeof (Tiger));
}
}
Run Code Online (Sandbox Code Playgroud) 为什么(i <UniqueWords.Count)表达式在for循环中有效,但返回"CS0019运算符'<'不能应用于'int'和'方法组'类型的操作数"错误放在我的if中?它们都是先前声明的字符串数组.
for (int i = 0;i<UniqueWords.Count;i++){
Occurrences[i] = Words.Where(x => x.Equals(UniqueWords[i])).Count();
Keywords[i] = UniqueWords[i];
if (i<UniqueURLs.Count) {rURLs[i] = UniqueURLs[i];}
}
Run Code Online (Sandbox Code Playgroud)
编辑添加声明:
List<string> Words = new List<string>();
List<string> URLs = new List<string>();
Run Code Online (Sandbox Code Playgroud)
//元素添加如此....
Words.Add (referringWords); //these are strings
URLs.Add (referringURL);
UniqueWords = Words.Distinct().ToList();
UniqueURLs = URLs.Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
解决了.谢谢,方法需要括号.Count()我仍然不完全理解为什么它们并不总是必要的.
Jon Skeet,谢谢,我想我不明白究竟是什么声明呢?您想要分配实际值吗?它们是从外部源引出的,但却是字符串.
我知道了!谢谢.(至少是().)
假设我有一个List,我想检查成员是否都等于"some string":
myList.All(v => v.Equals("some string"))
我想这会做同样的事情(或者会吗?!):
myList.All("some string".Equals)
但如果.Equals
我不想使用自己的方法呢?
public bool LessThan16Char(string input)
{
return (input.Length < 16);
}
Run Code Online (Sandbox Code Playgroud)
如何更换.Equals
使用LessThan16Char
?
如果该方法具有第二个参数(例如lessthan(string Input, Int lessThanVal)
),该怎么办
我还要感谢网上描述这些事情的任何文章,谢谢.
遇到一个问题,return View(await _context.Reviews.ToListAsync);
出现以下错误:Cannot await 'method group'
我相信要使用该return View(await _context.Reviews.ToListAsync);
语句,我需要使用using ASPNETCoreWebApplication.data
(因此我包含它),但它返回一个错误:The type or namespace 'ASPNETCoreWebApplication' could not be found [...]
。
如果我删除using <project_name>data
,则会出现与上面ApplicationDbContext
in相同的HomeController.cs
错误
以下是我的HomeController.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ASPNETCoreWebApplication.Data; // "The type or namespace 'ASPNETCoreWebApplication' could not be found [...]"
using <project_name>.Data;
using Microsoft.EntityFrameworkCore;
namespace <project_name>.Controllers
{
public class HomeController : Controller
{
private readonly ApplicationDbContext _context;
public …
Run Code Online (Sandbox Code Playgroud) method-group ×5
c# ×4
lambda ×3
.net ×1
asp.net-mvc ×1
compare ×1
delegates ×1
linq ×1
loops ×1
namespaces ×1
operators ×1