/mcs/class/referencesource/System.Workflow.ComponentModel/AuthoringOM/Fault.cs

https://github.com/pruiz/mono · C# · 224 lines · 190 code · 34 blank · 0 comment · 46 complexity · b104245b14baaf0b9632fe97850d0705 MD5 · raw file

  1. namespace System.Workflow.ComponentModel
  2. {
  3. using System;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Collections;
  7. using System.CodeDom;
  8. using System.ComponentModel;
  9. using System.ComponentModel.Design;
  10. using System.ComponentModel.Design.Serialization;
  11. using System.Drawing;
  12. using System.Runtime.Serialization;
  13. using System.Globalization;
  14. using System.Workflow.ComponentModel;
  15. using System.Workflow.ComponentModel.Design;
  16. using System.Collections.Generic;
  17. using System.Workflow.Runtime;
  18. using System.Workflow.ComponentModel.Compiler;
  19. [SRDescription(SR.FaultActivityDescription)]
  20. [ToolboxItem(typeof(ActivityToolboxItem))]
  21. [Designer(typeof(ThrowDesigner), typeof(IDesigner))]
  22. [ToolboxBitmap(typeof(ThrowActivity), "Resources.Throw.png")]
  23. [SRCategory(SR.Standard)]
  24. [Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
  25. public sealed class ThrowActivity : Activity, ITypeFilterProvider, IDynamicPropertyTypeProvider
  26. {
  27. [Browsable(false)]
  28. public static readonly DependencyProperty FaultProperty = DependencyProperty.Register("Fault", typeof(Exception), typeof(ThrowActivity));
  29. [Browsable(false)]
  30. public static readonly DependencyProperty FaultTypeProperty = DependencyProperty.Register("FaultType", typeof(Type), typeof(ThrowActivity));
  31. #region Constructors
  32. public ThrowActivity()
  33. {
  34. }
  35. public ThrowActivity(string name)
  36. : base(name)
  37. {
  38. }
  39. #endregion
  40. protected internal override void Initialize(IServiceProvider provider)
  41. {
  42. if (this.Parent == null)
  43. throw new InvalidOperationException(SR.GetString(SR.Error_MustHaveParent));
  44. base.Initialize(provider);
  45. }
  46. protected internal override sealed ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
  47. {
  48. if (this.Fault == null && this.FaultType == null)
  49. {
  50. throw new InvalidOperationException(SR.GetString(CultureInfo.CurrentCulture, SR.Error_PropertyNotSet, FaultProperty.Name));
  51. }
  52. if (this.FaultType != null && !typeof(Exception).IsAssignableFrom(this.FaultType))
  53. {
  54. throw new InvalidOperationException(SR.GetString(CultureInfo.CurrentCulture, SR.Error_ExceptionTypeNotException, this.FaultType, FaultTypeProperty.Name));
  55. }
  56. if (this.Fault != null && this.FaultType != null && !this.FaultType.IsInstanceOfType(this.Fault))
  57. {
  58. throw new InvalidOperationException(SR.GetString(CultureInfo.CurrentCulture, SR.Error_FaultIsNotOfFaultType));
  59. }
  60. if (this.Fault != null)
  61. throw this.Fault;
  62. ConstructorInfo cInfo = this.FaultType.GetConstructor(new Type[] { });
  63. if (cInfo != null)
  64. throw (Exception)cInfo.Invoke(null);
  65. throw new InvalidOperationException(SR.GetString(CultureInfo.CurrentCulture, SR.Error_FaultTypeNoDefaultConstructor, this.FaultType));
  66. }
  67. [TypeConverter(typeof(FaultConverter))]
  68. [SRCategory(SR.Handlers)]
  69. [SRDescription(SR.FaultDescription)]
  70. [MergableProperty(false)]
  71. [DefaultValue(null)]
  72. public Exception Fault
  73. {
  74. get
  75. {
  76. return base.GetValue(FaultProperty) as Exception;
  77. }
  78. set
  79. {
  80. base.SetValue(FaultProperty, value);
  81. }
  82. }
  83. [Editor(typeof(TypeBrowserEditor), typeof(System.Drawing.Design.UITypeEditor))]
  84. [SRCategory(SR.Handlers)]
  85. [SRDescription(SR.FaultTypeDescription)]
  86. [MergableProperty(false)]
  87. [DefaultValue(null)]
  88. [TypeConverter(typeof(FaultTypeConverter))]
  89. public Type FaultType
  90. {
  91. get
  92. {
  93. return base.GetValue(FaultTypeProperty) as Type;
  94. }
  95. set
  96. {
  97. base.SetValue(FaultTypeProperty, value);
  98. }
  99. }
  100. #region ITypeFilterProvider Members
  101. bool ITypeFilterProvider.CanFilterType(Type type, bool throwOnError)
  102. {
  103. bool isAssignable = TypeProvider.IsAssignable(typeof(Exception), type);
  104. if (throwOnError && !isAssignable)
  105. throw new Exception(SR.GetString(SR.Error_ExceptionTypeNotException, type, "Type"));
  106. return isAssignable;
  107. }
  108. string ITypeFilterProvider.FilterDescription
  109. {
  110. get
  111. {
  112. return SR.GetString(SR.FilterDescription_FaultHandlerActivity);
  113. }
  114. }
  115. #endregion
  116. #region IDynamicPropertyTypeProvider Members
  117. Type IDynamicPropertyTypeProvider.GetPropertyType(IServiceProvider serviceProvider, string propertyName)
  118. {
  119. if (!String.IsNullOrEmpty(propertyName) && propertyName.Equals("Fault", StringComparison.Ordinal))
  120. return FaultType;
  121. else
  122. return null;
  123. }
  124. AccessTypes IDynamicPropertyTypeProvider.GetAccessType(IServiceProvider serviceProvider, string propertyName)
  125. {
  126. return AccessTypes.Read;
  127. }
  128. #endregion
  129. #region Class FaultConverter
  130. private sealed class FaultConverter : TypeConverter
  131. {
  132. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  133. {
  134. if (sourceType == typeof(string))
  135. return false;
  136. return base.CanConvertFrom(context, sourceType);
  137. }
  138. }
  139. #endregion
  140. #region Class FaultTypeConverter
  141. private sealed class FaultTypeConverter : TypeConverter
  142. {
  143. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  144. {
  145. if (sourceType == typeof(string))
  146. return true;
  147. return base.CanConvertFrom(context, sourceType);
  148. }
  149. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  150. {
  151. object convertedValue = value;
  152. string stringValue = value as string;
  153. ITypeProvider typeProvider = context.GetService(typeof(ITypeProvider)) as ITypeProvider;
  154. if (context != null && typeProvider != null && !String.IsNullOrEmpty(stringValue))
  155. {
  156. Type type = typeProvider.GetType(stringValue, false);
  157. if (type != null)
  158. {
  159. ITypeFilterProvider typeFilterProvider = context.Instance as ITypeFilterProvider;
  160. if (typeFilterProvider != null)
  161. typeFilterProvider.CanFilterType(type, true);
  162. convertedValue = type;
  163. }
  164. }
  165. else if (stringValue != null && stringValue.Length == 0)
  166. {
  167. convertedValue = null;
  168. }
  169. return convertedValue;
  170. }
  171. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  172. {
  173. if (destinationType == typeof(string))
  174. return true;
  175. return base.CanConvertTo(context, destinationType);
  176. }
  177. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  178. {
  179. if (destinationType == typeof(string))
  180. {
  181. Type type = value as Type;
  182. if (type != null)
  183. return type.FullName;
  184. }
  185. return base.ConvertTo(context, culture, value, destinationType);
  186. }
  187. }
  188. #endregion
  189. }
  190. }