Tag: action

C#创建函数队列

我写了一个名为QueueManager的类: class QueueManager { Queue functionsQueue; public bool IsEmpty { get { if (functionsQueue.Count == 0) return true; else return false; } } public QueueManager() { functionsQueue = new Queue(); } public bool Contains(Action action) { if (functionsQueue.Contains(action)) return true; else return false; } public Action Pop() { return functionsQueue.Dequeue() as Action; } public void […]

如何向wix安装项目添加自定义操作

我的解决方案中有两个项目: 1)。 自定义操作类(CustomAction) 2)。 Wix安装项目(TestSetup) CustomAction项目中有CustomAction.cs: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using Microsoft.Deployment.WindowsInstaller; namespace CustomAction { public class CustomActions { [CustomAction] public static ActionResult CustomAction1(Session session) { File.Create(@”c:\installed.txt”); return ActionResult.Success; } } } Product.wxs: 安装项目构建没有问题,但是当我尝试运行它时,我收到一条错误消息:“此Windows Installer程序包存在问题。无法运行此安装所需的DLL。请联系您的支持人员或包装供应商“ 我认为这是因为二进制源文件值不正确。 你会说明如何解决它吗?

封装Action 和Func ?

我正在尝试为某种IExecutable接口进行设计。 我不会详细介绍,但重点是我有几个需要从基类执行的动作。 它们可能采用不同的参数(没什么大不了的),它们可能/可能不会返回值。 到目前为止,这是我的设计: public abstract class ActionBase { // … snip … } public abstract class ActionWithResultBase: ActionBase { public abstract T Execute(); } public abstract class ActionWithoutResultBase: ActionBase { public abstract void Execute(); } 到目前为止,我的每个具体动作都需要来自ActionWithResultBase或ActionWithoutResult基础的孩子,但我真的不喜欢这样。 如果我可以将Execute的定义移动到ActionBase,考虑到具体类可能会或可能不会返回值,我将实现我的目标。 有人告诉我这可以通过使用Func和Action来完成,我完全同意,但我找不到一种方法将它放到一个单独的类中,以便调用者知道该操作是否将返回一个值或不。 简介:我想做的事情如下: // Action1.Execute() returns something. var a = new Action1(); var result = a.Execute(); // Action2.Execute() […]

C#动作/function列表

我有一个程序必须根据Enum执行一个函数,我想知道如果还有另一种方法,那么: enum FunctionType { Addition = 0, Substraction = 1, Mutiplication = 2, Division = 3 } void ExecuteFunction(FunctionType Function) { switch(Function) { case FunctionType.Addition: Addition(); break; case FunctionType.Substraction: Subsctration(); break; … default: … } } (这不是我正在使用的代码,它只是代表我想要做的事情)。 这种方法应该可以正常工作,但是当你有更多function时会发生什么? 我不想要50线开关。 所以我想知道是否有办法简化它,这样的事情可能是: enum FunctionType : Action { Addition = new Action(Addition); Substraction = new Action(Substraction); …. } […]

用C#写我的第一个DSL,然后挂上func &Action

我正在努力编写我的第一个DSL用于工作中的简单工具。 我正在使用构建器模式来设置复杂的父对象,但我遇到了砖墙,用于构建父对象的子集合。 这是一个示例: 使用: var myMorningCoffee = Coffee.Make.WithCream().WithOuncesToServe(16); 关闭的样本(我认为这就是他们所谓的): var myMorningCoffee = Coffee.Make.WithCream().PourIn( x => { x.ShotOfExpresso.AtTemperature(100); x.ShotOfExpresso.AtTemperature(100).OfPremiumType(); } ).WithOuncesToServe(16); 示例类(没有子PourIn()方法,因为这是我想要弄清楚的。) public class Coffee { private bool _cream; public Coffee Make { get new Coffee(); } public Coffee WithCream() { _cream = true; return this; } public Coffee WithOuncesToServe(int ounces) { _ounces = ounces; return […]

C#2.0generics:如何创建零参数的Action对象

首先,我使用的是VS2005和C#2.0。 我正在尝试从SelectedIndexChanged事件中设置一个combobox’Text属性。 从StackOverflow上的另一个线程开始,提出了以下方法: BeginInvoke(new Action(() => someCombobox.Text = “x” )); 现在,首先,这将为我返回编译器错误。 我相信这是因为Action对象在两种语言规范中表现不同。 在C#2.0中,Action对象似乎在所有声明中都需要结构。 也许我错了,但我想澄清一下。 工作如下: BeginInvoke(new Action( delegate { someCombobox.Text = “x”; }), new object[] { “” }); 但是,我似乎很奇怪,我必须使用类型参数定义Action对象(特别是因为我不打算传递任何参数)! 以某种方式删除此参数也会使空的新对象[]过时,这就是我想要的。 任何人都可以帮我简化上述电话吗? 最后,是否保证BeginInvoke将在SelectedIndexChanged之后完成,从而使用正确的文本更新combobox’Text属性? 我真的很感激能够学习这些问题的答案。

如何将System.Linq.Enumerable.WhereListIterator 转换为List ?

在下面的示例中,如何轻松地将eventScores转换为List以便我可以将其用作prettyPrint的参数? Console.WriteLine(“Example of LINQ’s Where:”); List scores = new List { 1,2,3,4,5,6,7,8 }; var evenScores = scores.Where(i => i % 2 == 0); Action<List, string> prettyPrint = (list, title) => { Console.WriteLine(“*** {0} ***”, title); list.ForEach(i => Console.WriteLine(i)); }; scores.ForEach(i => Console.WriteLine(i)); prettyPrint(scores, “The Scores:”); foreach (int score in evenScores) { Console.WriteLine(score); }

没有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);

reflection性能 – 创建代理(属性C#)

我在使用reflection时遇到了性能问题。 所以我决定为我的对象的属性创建委托,到目前为止得到了这个: TestClass cwp = new TestClass(); var propertyInt = typeof(TestClass).GetProperties().Single(obj => obj.Name == “AnyValue”); var access = BuildGetAccessor(propertyInt.GetGetMethod()); var result = access(cwp); static Func BuildGetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), “o”); Expression<Func> expr = Expression.Lambda<Func>( Expression.Convert( Expression.Call( Expression.Convert(obj, method.DeclaringType), method), typeof(object)), obj); return expr.Compile(); } 结果非常令人满意,比使用传统方法快3-40-40倍( PropertyInfo.GetValue (obj, null); ) 问题是: 如何制作属性的SetValue ,其工作方式相同? […]