fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4.  
  5. static class Extensions
  6. {
  7. public static Delegate ConvertTo(this Delegate self, Type type)
  8. {
  9. if (type == null) { throw new ArgumentNullException("type"); }
  10. if (self == null) { return null; }
  11.  
  12. return Delegate.Combine(
  13. self.GetInvocationList()
  14. .Select(i => Delegate.CreateDelegate(type, i.Target, i.Method))
  15. .ToArray());
  16. }
  17. }
  18.  
  19. class Program
  20. {
  21. static void Main()
  22. {
  23. Action x = delegate { };
  24. x += new Action(delegate { });
  25.  
  26. ThreadStart badCopy = new ThreadStart(x);
  27. ThreadStart goodCopy = (ThreadStart)x.ConvertTo(typeof(ThreadStart));
  28.  
  29. Console.WriteLine("x == badCopy: {0}", x.Equals(badCopy));
  30. Console.WriteLine("x == goodCopy: {0}", x.Equals(goodCopy));
  31. }
  32. }
Success #stdin #stdout 0.03s 33872KB
stdin
Standard input is empty
stdout
x == badCopy: False
x == goodCopy: True