delegates

Is there a way to do this with delegates in .Net 3.5?

烂漫一生 提交于 2021-01-29 14:23:42
问题 We have a system where we need to dynamically change what happens at the end of a set of steps. We're doing this using a delegate. However the methods that we need to call at the end of the set of steps have different signatures. We have a choice at the moment to either do this using a standard delegate signature (probably copying the event handler one from ASP.Net) or doing it some other way (undertermined!) Is there a way to do this with delegates in .Net 3.5 ? Or could we do this using C#

How can I wrap this Powershell cmdlet into a timeout function?

与世无争的帅哥 提交于 2021-01-29 07:00:44
问题 I've tried (and failed) to get [System.Delegate]::CreateDelegate( to work, New-Object [System.Threading.Tasks.Task]::Run(, to work, Start-Job with Wait-Job, Powershell does not seem to be setup to do an asynchronous task with a wait timeout? If anyone has any ideas, that'd be awesome. do { Sleep 2; $testShare = Test-Path "\\$server\c$"; Write-Host "Share availability is:" $testShare; }while($testShare -eq $true) # wait for the share to become unavailable Adams suggestion do { Sleep 1; #

Run different task in same asynchronous thread in C# [duplicate]

痴心易碎 提交于 2021-01-28 22:03:41
问题 This question already has an answer here : How do I create a scheduler which never executes more than one Task at a time using async-await? (1 answer) Closed 14 days ago . I had to ask since I cannot found the same way as UI BeginInvoke asynchronous done. My sample program running on Winforms and all the delegate method is calling in the main UI thread. I have log the status and found it run on same thread on different BeginInvoke . Log from Winforms UI: 13/01/2021 11:57:23

Get compiler generated delegate for an event

别来无恙 提交于 2021-01-28 14:49:48
问题 I need to know what handlers are subsribed to the CollectionChanged event of the ObservableCollection class. The only solution I found would be to use Delegate.GetInvocationList() on the delegate of the event. The problem is, I can't get Reflection to find the compiler generated delegate. AFAIK the delegate has the same name as the event. I used the following piece of code: PropertyInfo notifyCollectionChangedDelegate = collection.GetType().GetProperty("CollectionChanged", BindingFlags

.NET single vs. multicast delegates [duplicate]

前提是你 提交于 2021-01-28 12:52:39
问题 This question already has answers here : Simple Delegate (delegate) vs. Multicast delegates (6 answers) Closed 6 years ago . I have been reading a bit about delegates in depth, it is confusing that a delegate with one method could be different than a multicast delegate. However, via reflection, you can see plainly that even with only a single method, a delegate is indeed deriving from MulticastDelegate , and not immediately deriving from a Delegate object. class Program { public delegate void

.NET single vs. multicast delegates [duplicate]

偶尔善良 提交于 2021-01-28 12:51:54
问题 This question already has answers here : Simple Delegate (delegate) vs. Multicast delegates (6 answers) Closed 6 years ago . I have been reading a bit about delegates in depth, it is confusing that a delegate with one method could be different than a multicast delegate. However, via reflection, you can see plainly that even with only a single method, a delegate is indeed deriving from MulticastDelegate , and not immediately deriving from a Delegate object. class Program { public delegate void

.NET single vs. multicast delegates [duplicate]

↘锁芯ラ 提交于 2021-01-28 12:50:57
问题 This question already has answers here : Simple Delegate (delegate) vs. Multicast delegates (6 answers) Closed 6 years ago . I have been reading a bit about delegates in depth, it is confusing that a delegate with one method could be different than a multicast delegate. However, via reflection, you can see plainly that even with only a single method, a delegate is indeed deriving from MulticastDelegate , and not immediately deriving from a Delegate object. class Program { public delegate void

Create Delegate from MethodInfo with Output Parameters, without Dynamic Invoke

孤街醉人 提交于 2021-01-28 12:00:59
问题 I am trying to get a Delegate from a MethodInfo object that has Output Parameters. My code follows: static void Main(string[] args) { MethodInfo m = typeof(Program).GetMethod("MyMethod2"); IEnumerable<Type> paramsTypes = m.GetParameters().Select(p => p.ParameterType); Type methodType = Expression.GetDelegateType(paramsTypes.Append(m.ReturnType).ToArray()); Delegate d = m.CreateDelegate(methodType); Action a = (Action)d; a(); } I'm getting is a System.InvalidCastException: Unable to cast

Display banner for NSUserNotification while app is frontmost

无人久伴 提交于 2021-01-28 07:45:47
问题 I want to display user notifications while the app is frontmost. I found the code below, but I'm not sure how to use the delegate: it seems to just return a boolean value. class MyNotificationDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate { func applicationDidFinishLaunching(aNotification: NSNotification) { NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self } func userNotificationCenter(center: NSUserNotificationCenter,

Correct usage of generics in event declaration

你离开我真会死。 提交于 2021-01-28 06:41:42
问题 I have a data object which I pass around : public class TimedResult<T> { public ResultRequest Request { get; set; } public T Data { get; set; } public TimeSpan TimeTaken { get; set; } } In my Manager class I have some work done which raises an event : public void Execute(ResultRequest request) { var result = new TimedResult<DataTable>(); // fill data object here OnResult(result); } I have made the OnResult method generic : protected virtual void OnResult<T>(TimedResult<T> result) { if (Result