Tag: delegates

C#到VB6 COM事件(“对象或类不支持事件集”)

真的把这头发拉出来…… 我有一个C#项目,其接口定义为: /* Externally Accessible API */ [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface ISerial { [DispId(1)] bool Startup(); [DispId(2)] bool Shutdown(); [DispId(3)] bool UserInput_FloorButton(int floor_number); [DispId(4)] bool Initialize(); } /* Externally Accesssible Event API */ [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface ISerialEvent { [DispId(5)] void DataEvent(); } [ComSourceInterfaces(typeof(ISerialEvent), typeof(ISerial))] [ClassInterface(ClassInterfaceType.None)] public class SerialIface : ISerial { public delegate void DataEvent(); public […]

Lambda \ Anonymous函数作为参数

我是C#的新手。 只是玩弄它。 不是出于真正的目的。 void makeOutput( int _param) { Console.WriteLine( _param.ToString()); } //… // Somewhere in a code { makeOutput( /* some not c# code for an example for what do I want */ function : int () { return 0; } ); } 是否可以使用REAL匿名函数(意味着返回结果)? 我不想使用像这样的代表 // Somewhere in a code { Func x = () […]

接线员’?’ 不能应用于’T’类型的操作数

试图使Feature通用,然后突然编译说 接线员’?’ 不能应用于’T’类型的操作数 这是代码 public abstract class Feature { public T Value { get { return GetValue?.Invoke(); } // here is error set { SetValue?.Invoke(value); } } public Func GetValue { get; set; } public Action SetValue { get; set; } } 可以使用此代码 get { if (GetValue != null) return GetValue(); return default(T); } 但我想知道如何修复那个漂亮的C#6.0单线程。

C#删除事件处理程序

我已经这样做了一段时间,但我没有注意到每次删除事件处理程序时我都在使用new 。 我应该创建一个新对象吗? 基本上1和2之间有区别吗? ethernetdevice.PcapOnPacketArrival -= new SharpPcap.PacketArrivalEvent(ArrivalResponseHandler); ethernetdevice.PcapOnPacketArrival -= ArrivalResponseHandler; 编辑:好的,这是重复的。 对于那个很抱歉。 答案发布在这里 。 具有相同目标,方法和调用列表的两个相同类型的委托被认为是相等的。

C#编译错误:“在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。”

我刚刚发布了一个关于如何让代理人在另一个表单上更新文本框的问题。 正当我以为我有使用Invoke的答案时…这种情况发生了。 这是我的代码: 主要表格代码: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Data.OleDb; using System.Collections.Specialized; using System.Text; using System.Threading; delegate void logAdd(string message); namespace LCR_ShepherdStaffupdater_1._0 { public partial class Main : Form { public Main() { InitializeComponent(); } public void add(string message) { this.Log.Items.Add(message); } public void logAdd(string […]

为事件定义空委托主体是一个好习惯吗?

可能重复: 在事件声明中添加匿名空委托是否有缺点? 为事件定义一个空的委托主体是一个好习惯,这样你就不必担心引发一个没有事件处理程序的事件了吗? (无需检查事件是否为空)。 像下面的代码: public event EventHandler LoadedData = delegate { };

没有EndInvoke的C#异步调用?

以下面的课程为例。 public class A { // … void Foo(S myStruct){…} } public class B { public A test; // … void Bar() { S myStruct = new S(); test.Foo(myStruct); } } 现在,我希望方法调用test.Foo(myStruct)是一个异步调用(’fire-and-forget’)。 条形方法需要尽快返回。 代理,BeginInvoke,EndInvoke,ThreadPool等文档不能帮助我找到解决方案。 这是有效的解决方案吗? // Is using the `EndInvoke` method as the callback delegate valid? foo.BeginInvoke(myStruct, foo.EndInvoke, null);

在另一个应用程序中侦听事件

假设我有两个用C#编写的应用程序。 第一个是第三方应用程序,它引发一个名为“OnEmailSent”的事件。 第二个是我编写的自定义应用程序,我想以某种方式订阅“OnEmailSent”,即使是第一个应用程序。 有什么方法可以以某种方式将第二个应用程序附加到第一个应用程序的实例来侦听“OnEmailSent”事件? 因此,为了进一步说明,我的具体方案是我们有一个用c#编写的自定义第三方应用程序,它引发了一个“OnEmailSent”事件。 我们可以看到使用reflection器存在事件。 我们想要做的是在此组件发送电子邮件时进行一些其他操作。 我们能够想到的最有效的方法是能够使用某种forms的IPC作为anders建议并监听由第三方组件引发的OnEmailSent事件。 因为组件是用C#编写的,所以我们想要编写另一个可以将自己附加到执行进程的C#应用​​程序,当它检测到OnEmailSent事件已经被提升时,它将执行它自己的事件处理代码。 我可能会遗漏一些东西,但据我所知,远程处理的工作方式是需要一台服务器来定义客户可以订阅的某种合同。 我更考虑的是某人编写了一个独立的应用程序,例如outlook,它暴露了我想从另一个应用程序订阅的事件。 我想我正在考虑的场景是.net调试器以及它如何附加到执行程序集以在代码运行时检查代码。