DefaultTriggerAttribute.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections;
  3. using System.Globalization;
  4. namespace HandyControl.Interactivity;
  5. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)]
  6. public sealed class DefaultTriggerAttribute : Attribute
  7. {
  8. private readonly object[] _parameters;
  9. public DefaultTriggerAttribute(Type targetType, Type triggerType, object parameter) : this(targetType,
  10. triggerType, new[] { parameter })
  11. {
  12. }
  13. public DefaultTriggerAttribute(Type targetType, Type triggerType, params object[] parameters)
  14. {
  15. if (!typeof(TriggerBase).IsAssignableFrom(triggerType))
  16. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
  17. ExceptionStringTable.DefaultTriggerAttributeInvalidTriggerTypeSpecifiedExceptionMessage,
  18. new object[] { triggerType.Name }));
  19. TargetType = targetType;
  20. TriggerType = triggerType;
  21. _parameters = parameters;
  22. }
  23. public IEnumerable Parameters =>
  24. _parameters;
  25. public Type TargetType { get; }
  26. public Type TriggerType { get; }
  27. public TriggerBase Instantiate()
  28. {
  29. object obj2 = null;
  30. try
  31. {
  32. obj2 = Activator.CreateInstance(TriggerType, _parameters);
  33. }
  34. catch
  35. {
  36. // ignored
  37. }
  38. return (TriggerBase) obj2;
  39. }
  40. }