Tag: 代表

@Delegate,@Mixin和Groovy中的特性之间的区别?

有人会解释什么时候我想要使用Groovy Traits vs. Mixins(@Mixin)还是Delegates(@Delegate)? 也许一些权衡和devise问题会有所帮助。 他们似乎都允许重复使用多个“类”的行为。 谢谢。 🙂 这个SO线程也很有帮助: Groovy中的@Delegate和@Mixin AST转换之间的区别

为什么试图理解代表感觉就像试图理解宇宙的本质?

我读过两本书,一堆例子。 他们依然对我毫无意义。 我可以写一些使用代表的代码,但是我不知道为什么。 我是唯一有这个问题的人,还是我只是一个白痴? 如果有人能够向我解释什么时候,在哪里,为什么我会真正使用一个代表,我会永远爱你。

活动 – 命名约定和风格

我正在学习C#中的Events / Delegates。 请问您对我select的命名/编码风格(从Head First C#书中获得)的意见? 我明天正在教这个朋友,并试图用最优雅的方式来解释这个概念。 (认为​​理解一个主题的最好方法就是尝试教它!) class Program { static void Main() { // setup the metronome and make sure the EventHandler delegate is ready Metronome metronome = new Metronome(); // wires up the metronome_Tick method to the EventHandler delegate Listener listener = new Listener(metronome); metronome.OnTick(); } } public class Metronome { // […]

我可以通过它的types获取C#委托的签名吗?

如果您有types信息,是否有直接的方法使用reflection来获取委托的参数列表? 举个例子,如果我声明一个委托types如下 delegate double FooDelegate (string param, bool condition); 并稍后获取该委托types的types信息,如下所示 Type delegateType = typeof(FooDelegate); 是否有可能从该types的信息对象中检索返回types(double)和参数列表({string,bool})?

行动<T>与委托事件

我已经看到开发人员使用下面的代码相当不错。 这些之间的确切区别是什么,哪些符合标准? 它们是否相同, Action和Func<T>也是一个委托: public event Action<EmployeeEventAgs> OnLeave; public void Leave() { OnLeave(new EmployeeEventAgs(this.ID)); } VS public delegate void GoOnLeave(EmployeeEventAgs e); public event GoOnLeave OnLeave; public void Leave() { OnLeave(new EmployeeEventAgs(this.ID)); }

为什么我们需要C#代表

我似乎从未理解为什么我们需要代表? 我知道它们是持有引用的方法的不可变引用types,但为什么我们不能直接调用方法,而不是通过委托调用它? 谢谢

如何正确注销一个事件处理程序

在代码审查中,我偶然发现了这个(简化的)代码片段来取消注册一个事件处理程序: Fire -= new MyDelegate(OnFire); 我以为这不会取消注册事件处理程序,因为它创build了一个以前从未注册过的新代理。 但是searchMSDN我发现了几个使用这个习惯用法的代码示例。 所以我开始了一个实验: internal class Program { public delegate void MyDelegate(string msg); public static event MyDelegate Fire; private static void Main(string[] args) { Fire += new MyDelegate(OnFire); Fire += new MyDelegate(OnFire); Fire("Hello 1"); Fire -= new MyDelegate(OnFire); Fire("Hello 2"); Fire -= new MyDelegate(OnFire); Fire("Hello 3"); } private static void OnFire(string […]

自动创build空的C#事件处理程序

在C#中没有附加处理程序的事件是不可能的。 所以在每次调用之前,都需要检查事件是否为空。 if ( MyEvent != null ) { MyEvent( param1, param2 ); } 我想保持我的代码尽可能干净,摆脱这些空检查。 我不认为这会影响performance,至less不会影响我的performance。 MyEvent( param1, param2 ); 现在我通过手动为每个事件添加一个空的内联处理程序来解决这个问题。 这是错误的容易,因为我需要记住这样做等。 void Initialize() { MyEvent += new MyEvent( (p1,p2) => { } ); } 有没有办法使用reflection和一些CLR魔法自动生成给定类的所有事件的空处理程序?

从不兼容的types'ViewController * const_strong'分配给'id <Delegate>'

在我的应用程序中,当我设置ViewController.delegate = self时,出现语义问题警告。 我已经search并find类似的post,但没有人能够解决我的问题。 ViewController.m: GameAddViewController *gameAddViewContoller = [[navigationController viewControllers] objectAtIndex:0]; gameAddViewContoller.delegate=self; 设置.delegate=self时出现错误信息。 GameAddViewController.h: @protocol GameAddViewControllerDelegate <NSObject> – (void)gameAddViewControllerDidCancel:(GameAddViewController *)controller; – (void)gameAddViewController:(GameAddViewController *)controller didAddGame:(Game *) game; @end @interface GameAddViewController : UITableViewController <GameAddViewControllerDelegate> { sqlite3 *pitchcountDB; NSString *dbPath; } @property (nonatomic, strong) id <GameAddViewControllerDelegate> delegate; … @end ViewController.h: #import "GameAddViewController.h" @class ViewController; @protocol ViewControllerDelegate <NSObject> – […]

Eclipse尝试启动Android应用程序时卡住了

我试图在Motorola Milestone A853上运行helloandroid应用程序。 我input“adb设备”,移动设备被正确识别。 但是,当我尝试运行应用程序时,Eclipse总是停留在27%“启动代理”。 这可能是原因?