Tag: lambda

在Monotouch中编译lambdas并在设备上调用委托

我目前正在MonoTouch中移植.NET代码库,我目前正在研究一种接收Expression 。 我正在尝试编译它,然后动态调用它。 这是我做的: // Here’s an example of what I could receive Expression<Action> expression = (a => Console.WriteLine (a * 2)); // And here’s what I’m trying to do to invoke it expression.Compile().DynamicInvoke(6); 这在iOS模拟器中工作正常,结果“12”在我的控制台中打印。 但后来我在iPad上尝试了,我收到了以下exception。 Object reference not set to an instance of an object at System.Linq.jvm.Runner.CreateDelegate () at System.Linq.Expressions.LambdaExpression.Compile () at System.Linq.Expressions.Expression`1[System.Action`1[System.Int32]].Compile () […]

One Liner:WeakReference-to-a Lambda事件处理程序

除了多次使用它会违反DRY原则这个事实之外,你能否看到这种单线的缺点? 这似乎很简单但事实上我还没有看到其他人提出这个问题让我想知道它是否有缺点。 这段代码创建一个方法的WeakReference ,然后注册一个调用引用目标的事件处理程序。 SomeEvent += (sender, e) => ((Action)(new WeakReference((Action)ProcessEvent)).Target)(); 谢谢, 本

Lambda分配局部变量

请考虑以下来源: static void Main(string[] args) { bool test; Action lambda = () => { test = true; }; lambda(); if (test) Console.WriteLine(“Ok.”); } 它应该编译,对吗? 嗯,事实并非如此。 我的问题是:根据C#标准,这个代码应该编译还是这个编译器错误? 错误消息: Use of unassigned local variable ‘test’ 注意:我知道 ,如何修复错误,我部分知道 ,为什么会发生。 但是,局部变量是无条件分配的,我想,编译器应该注意到,但事实并非如此。 我想知道为什么。 评论答案:C# 允许声明未分配的变量,这实际上非常有用,即。 bool cond1, cond2; if (someConditions) { cond1 = someOtherConditions1; cond2 = someOtherConditions2; } else { […]

通过比较返回lambda表达式的扩展方法

我正在为我们这个庞大的项目创建一个更精细的过滤系统。 其中一个主要谓词是能够通过字符串参数传递比较。 这表现为以下forms:“> 50”或“5-10”或“<123.2” 我有什么(作为一个例子来说明) 视图模型: TotalCost (string) (value: “<50") Required (string) (value: "5-10") EF型号: TotalCost (double) Required(double) 我想要使​​用的表达式: model => model.Where(field => field.TotalCost.Compare(viewModel.TotalCost) && field.Required.Compare(viewModel.Required)); 我希望收到的表达: model => model.Where(field => field.TotalCost 5 && field.Required < 10); 或类似的东西 但是……我不知道从哪里开始。 我把它缩小到了 public static Expression Compare(this Expression<Func> value, string compare) 它甚至可能不正确,但这是我所拥有的一切。 比较构建器不是问题,这很容易。 困难的部分实际上是返回表达式。 我从未尝试将表达式作为函数值返回。 所以我基本上需要保留的是字段并返回一个比较表达式。 有帮助吗? :X […]

C#:编译表达式时,已添加具有相同键的项

好的,这是一个棘手的问题。 希望这里有一位表达大师可以发现我在这里做错了什么,因为我只是没有得到它。 我正在构建用于过滤查询的表达式。 为了简化这个过程,我有一些Expression<Func>扩展方法,这些方法使我的代码更清晰,到目前为止它们已经很好地工作了。 我已经为所有人编写了测试,除了一个,我今天写了一个。 并且该测试完全失败,带有长堆栈跟踪的ArgumentException 。 我只是不明白。 特别是因为我在查询中成功使用了该方法一段时间了! 无论如何,这是运行测试时得到的堆栈跟踪: failed: System.ArgumentException : An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Expressions.ExpressionCompiler.PrepareInitLocal(ILGenerator gen, ParameterExpression p) at System.Linq.Expressions.ExpressionCompiler.GenerateInvoke(ILGenerator gen, InvocationExpression invoke, StackType ask) at System.Linq.Expressions.ExpressionCompiler.Generate(ILGenerator gen, Expression node, StackType ask) at System.Linq.Expressions.ExpressionCompiler.GenerateBinary(ILGenerator gen, […]

是否存在委托语法优先于匿名方法的lambda表达式的情况?

随着lambda表达式(内联代码)等新function的出现,它是否意味着我们不再需要使用委托或匿名方法了? 在我看过的几乎所有样本中,都是使用新语法进行重写。 我们仍然需要使用delegates和lambda表达式的任何地方都不起作用?

C#中“()=>”是什么意思?

在综合申请指南中涉及以下内容。 我知道=>是一个lambda但是()是什么意思? 还有其他一些例子吗? 它叫什么,所以我可以搜索它? this.regionViewRegistry.RegisterViewWithRegion(RegionNames.SelectionRegion , () => this.container.Resolve().View);

使用LINQ将值从一个列表分配给另一个列表

您好我将一个列表项的属性值分配给其他人有一点问题。 我知道我可以通过迭代两个列表来解决它“旧方法”但我正在寻找使用LINQ的更优雅的解决方案。 让我们从代码开始…… class SourceType { public int Id; public string Name; // other properties } class DestinationType { public int Id; public string Name; // other properties } List sourceList = new List(); sourceList.Add(new SourceType { Id = 1, Name = “1111” }); sourceList.Add(new SourceType { Id = 2, Name = “2222” }); sourceList.Add(new […]

使用.NET中的linq计算dataGridView中列的不同值

我需要在dataGridView中计算并显示不同/唯一的值。 我想像这样呈现它,这个代码适用于列表。 List aryIDs = new List(); aryIDs.Add(“1234”); aryIDs.Add(“4321”); aryIDs.Add(“3214”); aryIDs.Add(“1234”); aryIDs.Add(“4321”); aryIDs.Add(“1234”); var result= aryIDs.GroupBy(id => id).OrderByDescending(id => id.Count()).Select(g => new { Id = g.Key, Count = g.Count() }); 但是,当我尝试在dataGridView中的列上使用相同的方法时,我得到一个错误,指出groupBy不能在我的dataGridView上使用。 DataGridView dataGridView1= new DataGridView(); dataGridView1.Columns.Add(“nr”, “nr”); string[] row1 = new string[] { “1234” }; string[] row2 = new string[] { “4321” }; string[] row3 […]

是否可以缓存在lambda表达式中计算的值?

在以下代码的ContainsIngredients方法中,是否可以缓存p.Ingredients值而不是多次显式引用它? 这是一个相当简单的例子,我刚刚为了说明目的而编写,但我正在处理的代码引用了深入内部的值,例如。 p.InnerObject.ExpensiveMethod()。值 编辑:我正在使用来自http://www.albahari.com/nutshell/predicatebuilder.html的PredicateBuilder public class IngredientBag { private readonly Dictionary _ingredients = new Dictionary(); public void Add(string type, string name) { _ingredients.Add(type, name); } public string Get(string type) { return _ingredients[type]; } public bool Contains(string type) { return _ingredients.ContainsKey(type); } } public class Potion { public IngredientBag Ingredients { get; private set;} public string […]