有没有办法从一个Expression<Func<T>,bool>?拉出属性,运算符和匹配值?给出以下示例:
var customers = GetCustomers();
var customerQuery = customers.Where(x=> x.CustomerID == 1
&& x.CustomerName == "Bob"); // The query is for illustration only
Run Code Online (Sandbox Code Playgroud)
我需要能够得到以下内容:
Property: CustomerID
Operator: Equals
Value: 1
Property: CustomerName
Operator: Equals
Value: Bob
Run Code Online (Sandbox Code Playgroud)
我已经编写了一些可以提取Expression的属性名称的东西,但我似乎无法找到值和运算符的位置,尽管它在Expression的DebugView属性中非常清晰可见.
通常我会以这种方式创建一个表达式.
ParameterExpression pe = Expression.Parameter(typeof(object1), "x");
string Name = "property1";
MemberExpression left = Expression.Property(pe, (object1).GetProperty(Name));
Run Code Online (Sandbox Code Playgroud)
它产生 left = x => x.property1
我需要知道我该如何制作
left = x => x.Object2.property1
如果Name ="Object2.property1"; 和object2是object1的子项
提前致谢
我有许多不同的函数,它们都有类似于这个的简单printf语句__func__:
printf("%s - hello world!", __func__);
现在我遇到的问题是在某些函数中它返回<unknown>而不是函数名.
这是为什么?难道我做错了什么?AFAIK __func__是其中的一部分,c99所以我不明白它为什么不像宣传的那样工作.
我在Debian中使用GCC 4.7.2.
我有这样的静态方法:
public static string MyMethod(Func<Student, object> func)
{
return ??? ;
}
Run Code Online (Sandbox Code Playgroud)
我用它如下:
var s1 = MyMethod(student => student.ID); // Return "ID" ???
var s2 = MyMethod(student => student.Age); // Return "Age" ???
var s3 = MyMethod(student => student.Name); // Return "Name" ???
Run Code Online (Sandbox Code Playgroud)
怎么写方法返回以下结果?
*在=>之后返回每个属性的名称作为字符串
我有一个这样的课:
class MyClass { public object[] Values; }
Run Code Online (Sandbox Code Playgroud)
在其他地方我正在使用它:
MyClass myInstance = new MyClass() {Values = new object[]{"S", 5, true}};
List<Func<MyClass, object>> maps = new List<Func<MyClass, object>>();
for (int i = 0; i < myInstance.Values.Length ; i++)
{
maps.Add(obj => obj.Values[i]);
}
var result = maps[0](myInstance); //Exception: Index outside the bounds of the array
Run Code Online (Sandbox Code Playgroud)
我以为它会返回S,但它会抛出异常.知道发生了什么事吗?
我想在一个函数中创建一个名为lambda的函数,这样我就可以在同一个函数中重复调用它.
我过去常常同步/没有任务
Func<string, bool> pingable = (url) => return pingtest(url);
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我想将pingable函数作为一个任务调用,所以我需要一个Task返回类型.
这是我被困的地方.
对于以下所有,我收到编译错误:
* Func<string, Task<bool>> pingable = (input) => { return pingtest(url); };
* Task<bool> pingable = new Task<bool>((input) => { return pingtest(url); });
Run Code Online (Sandbox Code Playgroud)
我可以正常声明函数,但是我不能把它称为任务:
Func<string, bool> pingable = (input) => { return pingtest(url); };
var tasks = new List<Task>();
* tasks.Add(async new Task(ping("google.de")));
Run Code Online (Sandbox Code Playgroud)
我用*标记的所有行都会产生copmile错误.
http://dotnetcodr.com/2014/01/17/getting-a-return-value-from-a-task-with-c/似乎对解决方案有一个暗示,但那里的样本不允许提供输入参数.(从那里取样并简化:)
Task<int> task = new Task<int>(obj =>
{
return obj + 1;
}, 300);
Run Code Online (Sandbox Code Playgroud)
如何在C#中创建和调用命名的任务lambdas,我想在函数而不是类级别声明它们.
我想要命名的lambda以便多次调用它(在这种情况下是几个url).
您询问代码后编辑/更新:
Func<string, Task<bool>> ping = url => …Run Code Online (Sandbox Code Playgroud) 有没有办法将现有的Func委托转换为这样的字符串:
Func<int, int> func = (i) => i*2;
string str = someMethod(func); // returns "Func<int, int> func = (i) => i*2"
Run Code Online (Sandbox Code Playgroud)
或者至少接近它
我该如何映射?
从:Expression<Func<TEntity, bool>>
到:Expression<Func<TDbEntity, bool>>
where TEntity: class, new() and TDbEntity: class, new()
Run Code Online (Sandbox Code Playgroud)
TEntity来自Domain,TDbEntity来自Infrastructure层,但具有相同的属性.
有可能的?
#include <stdio.h>
void someFunc(void) {
printf("%s\n"), __func__);
}
Run Code Online (Sandbox Code Playgroud)
每次调用该函数时,它将打印:
someFunc
Run Code Online (Sandbox Code Playgroud)
什么是Java等价物?
我已经找到
(new Exception()).getStackTrace()[0].getMethodName()
Run Code Online (Sandbox Code Playgroud)
和
java.lang.Thread.currentThread().getStackTrace()[1].getMethodName()
Run Code Online (Sandbox Code Playgroud)
但这些看似荒谬,是否有更简单的方法?
如何使用FakeItEasy框架检查Func是否被调用?
例子:
Func<bool> myFunc = () => true;
// Unfortunately this fails:
A.CallTo(myFunc.Invoke()).MustHaveHappened();
Run Code Online (Sandbox Code Playgroud) func ×10
c# ×8
linq ×3
delegates ×2
expression ×2
reflection ×2
.net ×1
async-await ×1
c ×1
c99 ×1
fakeiteasy ×1
gcc ×1
java ×1
lambda ×1