Tag: 代表

无法将lambda表达式转换为类型’System.Delegate’,因为它不是委托类型?

我遇到了一个我似乎无法弄清楚的问题,虽然这是Stackoverflow上的一个标准问题。 我正在尝试使用以下代码异步更新我的Bing地图(请注意,这是来自旧的Silverlight项目,似乎不适用于WPF) _map.Dispatcher.BeginInvoke(() => { _map.Children.Clear(); foreach (var projectedPin in pinsToAdd.Where(pin => PointIsVisibleInMap(pin.ScreenLocation, _map))) { _map.Children.Add(projectedPin.GetElement(ClusterTemplate)); } }); 我究竟做错了什么?

为属性setter或getter创建一个高性能的开放委托

开放委托是没有目标的实例方法的委托。 要调用它,您需要提供目标作为其第一个参数。 它们是优化代码的一种聪明方法,否则会使用reflection并且性能较差。 有关开放代表的介绍,请参阅此内容 。 你在实践中使用它的方法是使用昂贵的reflection代码来构建这些开放的委托,但是你可以像一个简单的委托调用那样非常便宜地调用它们。 我正在尝试编写将任意PropertyInfo转换为其setter的委托的代码。 到目前为止,我想出了这个: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Test { class TestClass { static Action MakeSetterDelegate(PropertyInfo property) { MethodInfo setMethod = property.GetSetMethod(); if (setMethod != null && setMethod.GetParameters().Length == 1) //skips over nasty index properties { //To be able to bind to the delegate […]

C#中的函数指针

我想在某些方面(或两者) Delegate或MethodInfo都有资格获得这个标题。 但是,它们都没有提供我正在寻找的语法上的好处。 所以,简而言之,有什么方法可以写下面的内容: FunctionPointer foo = // whatever, create the function pointer using mechanisms foo(); 我不能使用一个可靠的委托(即使用delegate关键字来声明委托类型),因为直到运行时才能知道确切的参数列表。 作为参考,这是我目前在LINQPad中使用的内容,其中B将是(大部分)用户生成的代码,因此Main将会如此,因此对于我的用户来说,我正在尝试删除.Call : void Main() { A foo = new B(); foo[“SomeFuntion”].Call(); } // Define other methods and classes here interface IFunction { void Call(); void Call(params object[] parameters); } class A { private class Function : IFunction { […]

C#事件处理(与Java相比)

我目前正在使用delagates在C#中很难理解和实现事件。 我习惯了Java的做事方式: 为侦听器类型定义一个接口,该接口包含许多方法定义 如果我对侦听器中定义的所有事件不感兴趣,请为该接口定义适配器类以使事情更容易 在类中定义Add,Remove和Get []方法以引发事件 定义受保护的fire方法,以便在添加的侦听器列表中循环并调用正确的方法 这个我理解(并且喜欢!) – 我知道我可以在c#中完全相同,但似乎有一个新的(更好的?)系统用于c#。 阅读了无数的教程,解释了c#中代表和事件的使用后,我仍然无法真正了解正在发生的事情:S 简而言之,对于以下方法,我将如何在c#中实现事件系统: void computerStarted(Computer computer); void computerStopped(Computer computer); void computerReset(Computer computer); void computerError(Computer computer, Exception error); ^上面的方法取自我曾经制作的Java应用程序,我正在尝试移植到c#。 非常感谢!

传递给委托时,整数作为引用类型处理

本周我参加了荷兰的TechDays 2013,我得到了一个有趣的测验问题。 问题是:以下程序的输出是什么。 这是代码的样子。 class Program { delegate void Writer(); static void Main(string[] args) { var writers = new List(); for (int i = 0; i < 10; i++) { writers.Add(delegate { Console.WriteLine(i); }); } foreach (Writer writer in writers) { writer(); } } } 显然,我给出的答案是错误的。 我认为,因为int是一个值类型,传递给Console.WriteLine()的实际值会被复制,因此输出将为0 … 9。 但是在这种情况下i被作为参考类型处理。 正确答案是它会显示十次10.有人可以解释为什么以及如何解释?

事件处理程序提升方法约定

我只是在浏览并遇到了这个问题: 行动与委托事件 来自nobug的答案包括以下代码: protected virtual void OnLeave(EmployeeEventArgs e) { var handler = Leave; if (handler != null) handler(this, e); } 使用“创建提升方法”快速修复时,Resharper也会生成类似的代码。 我的问题是,为什么这条线是必要的?: var handler = Leave; 为什么写这个更好?: protected virtual void OnLeave(EmployeeEventArgs e) { if (Leave != null) Leave(this, e); }

在方法签名中使用委托和使用Func / Action 有什么区别?

我一直在尝试用C#代替我的代表,但我似乎没有明白使用它们的意义。 以下是代表们的MSDN页面上的一些稍微重建的代码: using System; using System.Collections; namespace Delegates { // Describes a book in the book list: public struct Book { public string Title; // Title of the book. public string Author; // Author of the book. public decimal Price; // Price of the book. public bool Paperback; // Is it paperback? public Book(string title, […]

我应该创建一个新的代理实例吗?

这样做有什么意义…… this.myButton.Click += new EventHandler(this.myButton_Clicked); ……对比这个? this.myButton.Click += this.myButton_Clicked; 我怀疑编译器在第二个例子中为我创建了一个新实例。 我确定这是一个新手问题,但谷歌没有发现任何问题。 谁能给我一些见解?

C#new 没有必要吗?

我最近一直在玩HttpWebRequest ,在他们总是做的教程中: IAsyncResult result = request.BeginGetResponse( new AsyncCallback(UpdateItem),state); 但是new AsyncCallback似乎并不是必需的。 如果UpdateItem具有正确的签名,那么似乎没有问题。 那么人们为什么要包括它呢? 它有什么用吗?

C#跨线程通信

在C#.NET中,我编写了以下简单的后台工作线程: public class MyBackgrounder { public delegate void dlgAlert(); public dlgAlert Alert; public event EventHandler eventAlert; Thread trd; public void Start() { if (trd == null || trd.ThreadState == ThreadState.Aborted) { trd = new Thread(new ThreadStart(Do)); } trd.IsBackground = true; trd.Priority = ThreadPriority.BelowNormal; trd.Start(); } void Do() { Thread.Sleep(3000); Done(); } void Done() { if […]