- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要创建一个方法来动态检查作为参数传递的方法委托(delegate)。但我不能强制表达式树接受任何方法,而不管它们的签名如何。
这是我的方法(不编译:error CS0030: Cannot convert type 'method' to 'Delegate'
)
public void Examine<T>(Expression<Func<T, Delegate>> expression2)
{
// examine expression tree to get method name (MethodInfo)
}
我想这样调用它:
Examine<Foo>(x => foo1.Test1);
Examine<Bar>(x => bar2.DifferentMethod2);
// etc, with other methods
哪里:
bool Test1(int num)
如何实现?
备注:
最佳答案
您必须使用 Func
或 Action
,但是您可以在调用方而不是方法方使用它,因此您仍然可以接受任何类型。
static void Main()
{
Foo foo1 = null;
Bar bar2 = null;
Examine<Foo>(x => (Func<int,bool>)foo1.Test1);
Examine<Bar>(x => (Func<string,string,string>)bar2.DifferentMethod2);
}
public static void Examine<T>(Expression<Func<T, Delegate>> expression2)
{
// examine expression tree to get method name (MethodInfo)
}
这会创建一个类似
的表达式.Lambda #Lambda1<System.Func`2[SandboxConsole.Foo,System.Delegate]>(SandboxConsole.Foo $x) {
(System.Func`2[System.Int32,System.Boolean]).Call .Constant<System.Reflection.MethodInfo>(Boolean Test1(Int32)).CreateDelegate(
.Constant<System.Type>(System.Func`2[System.Int32,System.Boolean]),
.Constant<SandboxConsole.Program+<>c__DisplayClass0_0>(SandboxConsole.Program+<>c__DisplayClass0_0).foo1)
}
和
.Lambda #Lambda1<System.Func`2[SandboxConsole.Bar,System.Delegate]>(SandboxConsole.Bar $x) {
(System.Func`3[System.String,System.String,System.String]).Call .Constant<System.Reflection.MethodInfo>(System.String DifferentMethod2(System.String, System.String)).CreateDelegate(
.Constant<System.Type>(System.Func`3[System.String,System.String,System.String]),
.Constant<SandboxConsole.Program+<>c__DisplayClass0_0>(SandboxConsole.Program+<>c__DisplayClass0_0).bar2)
}
对于两次调用。
关于c# - ExpressionTree 作为方法参数接受任何方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39280206/
我需要创建一个方法来动态检查作为参数传递的方法委托(delegate)。但我不能强制表达式树接受任何方法,而不管它们的签名如何。 这是我的方法(不编译:error CS0030: Cannot con
我有一个将 lambda 添加到 IQueryable 的通用方法。下面的代码添加了一个 StartsWith lambda。我现在要做的是创建一个 NotStartsWith lambda。由于 N
我目前有以下代码,它允许我调用对象的 EmailAddress 属性所需的任何方法,并且效果很好: public static Expression> BuildEmailAddressLambda(
using System; using System.Linq.Expressions; class Program { static void Main() { Expression
我正在研究将属性赋值作为表达式树传递给方法的想法。该方法将调用表达式,以便正确分配属性,然后嗅探出刚刚分配的属性名称,以便我可以引发 PropertyChanged 事件。我的想法是,我希望能够在我的
我的代码是对此处示例的轻微修改: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expressi
我目前正在阅读 MSDN,Walkthrough: Creating an IQueryable LInQ Provider ExpressionVisitor 有很多用途。 这让我想知道,使用它是不
我正在尝试为表达式树做,并尝试让它返回一个简单的 int 值。但它不再工作了 var method = typeof(Console).GetMethod("WriteLine", n
让有: Expression> expression = c => c.Name == "John"; 现在我通过使用获得值(value): string myvalue = ((ConstantEx
模糊地与 previous question 相关 注意:我正在使用 ExpressionTree 访问者的派生,如 here 所解释的那样 在我的 VisitMemberAccess 方法中,我目前
以下程序的输出是: 首先:System.String。第二:System.String。 预期结果是:第一:你好1。第二个:hello2。 如果我将索引硬编码为 1 或 2Expression.Ass
看起来 ExpressionTrees 编译器在许多行为上应该接近 C# 规范,但与 C# 不同的是,它不支持从 decimal 到任何 enum-type 的转换: using System; us
假设我有: Func a = (c) => c.fullName == "John"; 现在我想通过任何方式转换为 expressiontree 吗? 我知道我可以从一开始就将它定义为表达式树,但我
我正在尝试使用表达式树,以便我可以选择使用 Entity Framework 映射到 DTO,其方式与 Include 指令在 DbSet(实现 OData 的开源项目的一部分)上的工作方式大致相同。
我正在手动构建一个谓词来过滤 CollectionView 中的数据,我想添加通过用户提供的 Regex 过滤特定字段的功能。直接编写谓词会给出如下内容: string userRegex = "ab
如果我在下面的代码中犯了任何错误/输入错误,请不要生气,只需在这里发表评论,我会立即修复 - 谢谢 目标 重新映射 Expression来自一个EntityA到 EntityB . 我怀疑以前有人做过
我有一个 gridview,我们可以在其中按不同的标准进行过滤。每个条件都是一个表达式。我有一个场景,在调用 Compile 方法时,我可以有超过一千个条件导致我的表达式抛出 StackOverflo
今天早上我看到了一个问题 (Query my model on a range of values),似乎由 (https://stackoverflow.com/a/1447926/195550)
我编写了一个 ToList(); 扩展方法来将 DataTable 转换为 List。这只在某些情况下有效,但我们有很多使用数据表的旧代码,有时需要它。我的问题是这种方法与反射一起工作是好的,但不是那
我正在尝试创建委托(delegate)来访问任何 Property Get 和 Set 方法。 我发现以下代码(在这篇文章中:driis about deep properties)非常有效: pub
我是一名优秀的程序员,十分优秀!