DelegateUtility.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// Delegate utility class.
  6. /// </summary>
  7. public static class DelegateUtility
  8. {
  9. /// <summary>
  10. /// Cast a delegate.
  11. /// </summary>
  12. /// <param name="source">Source delegate.</param>
  13. /// <param name="type">Type of the delegate.</param>
  14. /// <returns>Cast delegate.</returns>
  15. public static Delegate Cast(Delegate source, Type type)
  16. {
  17. if (source == null)
  18. return null;
  19. Delegate[] delegates = source.GetInvocationList();
  20. if (delegates.Length == 1)
  21. return Delegate.CreateDelegate(type,
  22. delegates[0].Target, delegates[0].Method);
  23. Delegate[] delegatesDest = new Delegate[delegates.Length];
  24. for (int nDelegate = 0; nDelegate < delegates.Length; nDelegate++)
  25. delegatesDest[nDelegate] = Delegate.CreateDelegate(type,
  26. delegates[nDelegate].Target, delegates[nDelegate].Method);
  27. return Delegate.Combine(delegatesDest);
  28. }
  29. }
  30. }