Tag: 委托

如何将函数指针从C#传递给C ++ Dll?

C ++ dll中定义的函数是: static double (*Func1)(double); EXTERN_C __declspec(dllexport) __stdcall double TestDelegate(double (*fun)(double)) { Func1 = fun; return Func1(25.0); } void My_Real_purpose() { SomeClass a; a.SetFunction(Func1);//Define behaviour of a by C# in runtime a.DoSomething();//Even I want it runs in another thread! } 我试着用C#这样称呼它: class A { [DllImport(“DllName.dll”)] public extern static double TestDelegate(IntPtr f); public delegate double […]

多播代理必须具有返回类型void。 为什么?

多播代理必须具有返回类型void否则将引发exception。 我想知道它背后的原因是什么,如果多个方法可以具有与委托相同的返回类型呢?

滚动时通过线程更新DataGridView

我遇到了我的这个问题,如果有人解决了这个问题,那将会很有帮助 我的问题 我想做的是: 1)在表单加载事件中初始化DataTable数据表并将其defaultview分配给a datagridview dgvresult 2)点击一个按钮启动一个STA线程(我实际上正在使用WatIN IE,因此 需要使线程STA)调用一个创建相同DataTable的方法 dt作为在步骤1中创建的数据表,然后向此数据表添加300行。 3)调用一个委托,它将这个dt与datatable合并,从而更新dgvresult这里是我的问题: 这是我刚才描述的步骤的代码片段: static class Program { /// /// The main entry point for the application.This method is made STAThread as I need to intialize WatIN IE in the form load of frmMain /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmMain()); } } /// […]

混淆`Action`委托和lambda表达式

private void StringAction(string aString) // method to be called { return; } private void TestDelegateStatement1() // doesn’t work { var stringAction = new System.Action(StringAction(“a string”)); // Error: “Method expected” } private void TestDelegateStatement2() // doesn’t work { var stringAction = new System.Action(param => StringAction(“a string”)); // Error: “System.Argument doesn’t take 1 arguments” stringAction(); } private […]

委托中变量的范围

我发现以下内容相当奇怪。 然后,我主要使用动态语言中的闭包,这对于同一个“bug”不应该是可疑的。 以下使编译器不满意: VoidFunction t = delegate { int i = 0; }; int i = 1; 它说: 名为“i”的局部变量不能在此范围内声明,因为它会为“i”赋予不同的含义,“i”已在“子”范围中用于表示其他内容 所以这基本上意味着在委托中声明的变量将具有声明的函数的范围。不完全是我所期望的。 我甚至没有试过调用这个函数。 至少Common Lisp有一个function,你可以说变量应该有一个动态名称,如果你真的希望它是本地的。 这在创建不泄漏的宏时尤其重要,但这样的东西也会有所帮助。 所以我想知道其他人如何解决这个问题呢? 为了澄清我正在寻找一个解决方案,其中我在delegete中声明的变量不会干扰委托后声明的变量。 我仍然希望能够捕获委托之前声明的变量。

编译器生成的事件的后备字段是否始终保证使用与事件相同的名称?

C#允许我们创建自定义事件访问器 。 Action _custom; public event Action Custom { add { _custom = (Action)Delegate.Combine( _custom, value ); } remove { _custom = (Action)Delegate.Remove( _custom, value ); } } 如果未指定它们, 编译器将为您创建它们 。 C#语言规范: 编译类似字段的事件时,编译器会自动创建存储以保存委托,并为事件创建访问器,以便向委托字段添加或删除事件处理程序。 反编译的源代码使用dotPeek进行简单的public event Action Public; 看起来如下: private Action Public; public event Action Public { add { Action action = this.Public; Action comparand; do […]

参数计数与Invoke不匹配?

下面的代码块导致错误:TargetParameterCountException未被用户代码处理。 参数计数不匹配。 public void AddListViewItem(string[] Data) { if (InvokeRequired) { Invoke(new Action(AddListViewItem), Data); } else { ListViewData.Items.Add(Data[0]).SubItems.AddRange ( new string[] { Data[1], Data[2], Data[3], } ); } } 有任何想法吗?

C#ToDictionary lambda选择索引和元素?

我有一个像string strn = “abcdefghjiklmnopqrstuvwxyz” ,想要一个字典,如: Dictionary(){ {‘a’,0}, {‘b’,1}, {‘c’,2}, … } 我一直在尝试这样的事情 strn.ToDictionary((x,i) => x,(x,i)=>i); …但是我一直在得到关于委托的各种错误,没有采用两个参数,以及未指明的参数等。 我究竟做错了什么? 我更喜欢对答案的提示,所以我对下次需要做的事情有一个精神上的痕迹,但根据Stackoverflow的性质,答案也很好。

为什么匿名委托可以省略参数,但lambdas不能?

//ok Action CallbackWithParam1 = delegate { }; //error CS1593: Delegate ‘System.Action’ does not take 0 arguments Action CallbackWithParam2 = () => { }; 只是想知道为什么差异真的。 : – /

从属性getter或setter方法创建委托

要从方法创建委托,您可以使用compile type-safe语法: private int Method() { … } // and create the delegate to Method… Func d = Method; 属性是getter和setter方法的包装器,我想创建一个属性getter方法的委托。 就像是 public int Prop { get; set; } Func d = Prop; // or… Func d = Prop_get; 不幸的是,这不起作用。 我必须创建一个单独的lambda方法,当getter方法匹配委托签名时,这似乎是不必要的: Func d = () => Prop; 为了直接使用委托方法,我必须使用讨厌的reflection,这不是编译类型安全的: // something like this, not tested… MethodInfo m […]