Tag: 委托

我希望能够使用lambda表达式来指定要通过wcf服务返回的值范围

我不知道这是否可能……但它会很酷。 问题是,是否有可能,但如果可能的话,还有一些例子。 我不确定您将使用什么方法签名来传递lambda表达式。 例如方法IList GetGroups() 你如何修改它以便能够将lambda表达式传递给它? 接下来的问题是如何编写lambda表达式以返回所有Group对象,例如 where .deleted == false或 where .DateAdded > aDate 是的,我想把月亮放在棍子上;)先谢谢你。 (编辑我认为这实际上有点不合理,因为实际上会获取数据的数据访问层……但是假设您正在通过服务查询某些对象集合而不必担心dal )。

如何将任意方法(或委托)作为参数传递给函数?

我需要能够将任意方法传递给某个函数myFunction : void myFunction(AnyFunc func) { … } 应该可以使用其他静态,实例,公共或私有方法甚至委托来执行它: myFunction(SomeClass.PublicStaticMethod); myFunction(SomeObject.PrivateInstanceMethod); myFunction(delegate(int x) { return 5*x; }); 传递方法可以具有任意数量的参数和任何返回类型。 还应该可以通过reflection了解myFunction的实际参数数量及其类型。 myFunction定义中的AnyFunc会满足这些要求吗? 有几个重载版本的myFunction是可以接受的。

在“for”循环中添加委托更改迭代的方法 – C#问题?

我遇到了C#代码的一些问题。 添加方法以“for”循环委托“i”加1,所以“for(int i = 0,i <x; i ++)”必须更改为“for(int i = -1,i <x- 1; i ++)“正常工作。 这是为什么? 下面的代码抛出IndexOutOfRangeException string[] names = new string[] { “John”, “Madeline”, “Jack”, “Gabby” }; Action showNameDelegate = null; for (int i = 0; i global::System.Console.WriteLine(names[i]); } foreach (Action showName in showNameDelegate.GetInvocationList()) { showName(); } 正确的代码是(查看迭代器“i”,它从-1开始,但“names [-1]”不存在): string[] names = new string[] […]

如何在C#中为委托类型的参数提供默认值?

在C#中,我们可以提供参数的默认值: void Foo(int i =0) {} 但是,当方法签名是: void FooWithDelegateParam(Func predicate) {} 我们如何传递默认参数: void FooWithDelegateParam(Func predicate = (string,string x)=> {return y;}) {} 但这不会编译。 那么,这样做的正确语法是什么? 注意:我正在尝试提供一种通过委托为输出字符串映射器指定输入字符串的方法,如果没有提供,我只想返回输入字符串。 因此,对于实现这一目标的任何替代方法的建议也受到高度赞赏。 谢谢。

如何在C#3.5中取消异步委托?

我上下搜索谷歌,但我找不到关于该主题的任何适当信息。 我想做的是: 用户在文本框中键入单个搜索字符串。 我等了0.5秒然后我开始BeginInvoke我的委托指向搜索方法。 如果用户再次键入char,我想取消搜索并开始新的搜索,并输入新的字符串。 不得阻止UI-Thread! 我怎么能用C#3.5做到这一点? 更新 : 查看 : private void OnTextChanged(…) { if (SearchFormatEvent != null) { ICollection collection = SearchFormatEvent(“MySearchString”); // Do stuff on the returned collection } } SearchProvider : // This is the delegate invoked for the async search taking the searchstring typed by the user public delegate ICollection […]

使用ref参数委派

有没有办法在下面的代码中保持相同的function,但无需创建委托? 我正在与包含许多各种DeleteSomethingX(ref IntPtr ptr)方法的第三方API接口,我正在尝试集中IntPtr.Zero检查的代码。 private void delegate CleanupDelegate(ref IntPtr ptr); … private void Cleanup(ref IntPtr ptr, CleanupDelegate cleanup) { if (ptr != IntPtr.Zero) { cleanup(ref ptr); } }

使用带有可选参数的方法的C#委托

有机会使这段代码有效吗? 当然我可以对Foo做出第二个定义,但我觉得它有点不优雅;) delegate int Del(int x); static int Foo(int a, int b = 123) { return a+b; } static void Main() { Del d = Foo; }

行动代表。 如何获取调用该方法的实例

我有一个Action,我想知道如何访问调用该方法的实例。 例: this.FindInstance(() => this.InstanceOfAClass.Method()); this.FindInstance(() => this.InstanceOfAClass2.Method()); this.FindInstance(() => this.InstanceOfAClass3.Method()); public void FindInstance(Action action) { // The action is this.InstanceOfAClass.Method(); and I want to get the “Instance” // from “action” } 谢谢

在这种情况下,为什么C#无法解决正确的重载?

我遇到了一个非常模糊的奇怪情况,但过载解析器并不这么认为。 考虑: public static class Program { delegate int IntDel(); delegate string StringDel(); delegate void ParamIntDel(int x); delegate void ParamStringDel(string x); static void Test(IntDel fun) { } static void Test(StringDel fun) { } static void ParamTest(ParamIntDel fun) { } static void ParamTest(ParamStringDel fun) { } static int X() { return 42; } static void PX(int […]

铸造/制图代表

我有一个方法 public List GetUsers(Func expression) { var users = new List(); using(UserContext context = new UserContext()) { // obviously an error users = context.Users.ToList(); } return users; } 注意DTO.User(一个DTO)和Domain.User(一个来自EF的域实体)所以我使用AutoMapper来映射像这样的实体 public List GetUsers() { var users = new List(); using(UserContext context = new UserContext()) { Mapper.CreateMap(); users = Mapper.Map<List,List>(context.Users.ToList()); } return users; } 好吧,这看起来不错但是..我希望GetUser方法接受委托表达式作为参数。 我在ui中有一个显示用户列表的网格,它有很多过滤选项,所以我希望我的UI只调用1方法而不是每个filter创建方法。 // […]