Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.26 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Retreive Arguments of a Workflow (with default values)?
  2. // Just an holder for InArgument informations
  3. class InArgumentInfo
  4. {
  5.     public string InArgumentName { get; set; }
  6.     public string InArgumentDescription { get; set; }
  7.     public bool InArgumentIsRequired { get; set; }
  8. }
  9.  
  10. static ICollection<InArgumentInfo> GetInArgumentsInfos(Activity activity)
  11. {
  12.     var properties = activity.GetType()
  13.         .GetProperties()
  14.         .Where(p => typeof(InArgument).IsAssignableFrom(p.PropertyType))
  15.         .ToList();
  16.  
  17.     var argumentsCollection = new Collection<InArgumentInfo>();
  18.  
  19.     foreach (var property in properties)
  20.     {
  21.         var descAttribute = property
  22.             .GetCustomAttributes(false)
  23.             .OfType<DescriptionAttribute>()
  24.             .FirstOrDefault();
  25.  
  26.         string description = descAttribute != null && !string.IsNullOrEmpty(descAttribute.Description) ?
  27.             descAttribute.Description :
  28.             string.Empty;
  29.  
  30.         bool isRequired = property
  31.             .GetCustomAttributes(false)
  32.             .OfType<RequiredArgumentAttribute>()
  33.             .Any();
  34.  
  35.         argumentsCollection.Add(new InArgumentInfo
  36.         {
  37.             InArgumentName = property.Name,
  38.             InArgumentDescription = description,
  39.             InArgumentIsRequired = isRequired
  40.         });
  41.     }
  42.  
  43.     return argumentsCollection;
  44. }
  45.        
  46. var dynamicActivity = ActivityXamlServices.Load(foo) as DynamicActivity
  47. foreach(DynamicActivityProperty prop in dynamicActivity.Properties)
  48. {
  49.    // ...
  50. }
  51.        
  52. foreach (var prop in dynamicActivity .Properties)
  53. {
  54.     object defaultValue;
  55.     if (prop.Value == null)
  56.     {
  57.         defaultValue = null;
  58.     }
  59.     else
  60.     {
  61.         Type genericTypeDefinition = prop.Type.GetGenericTypeDefinition();
  62.         if (genericTypeDefinition == typeof(InArgument<>) || genericTypeDefinition == typeof(InOutArgument<>))
  63.         {
  64.             var valueProp = prop.Value.GetType().GetProperty("Expression", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly);
  65.             var expression = valueProp.GetValue(prop.Value, null);
  66.  
  67.             var expressionValueProp = expression.GetType().GetProperty("Value");
  68.             defaultValue = expressionValueProp.GetValue(expression, null);
  69.         }
  70.     }
  71. }