示例#1
0
        private bool LoadArguments()
        {
            try
            {
                using (var taskDc = new AryaServicesDbDataContext())
                    using (var aryaDc = new AryaDbDataContext())
                    {
                        var task     = taskDc.AryaTasks.Single(t => t.ID == TaskId);
                        var taskInfo = (from up in aryaDc.UserProjects
                                        where up.Project.ID == task.ProjectID && up.User.ID == task.SubmittedBy
                                        select
                                        new
                        {
                            JobType = task.DisplayJobType,
                            Project = up.Project.DisplayName,
                            SubmittedBy = up.User.FullName,
                            SubmittedOn = task.SubmittedOn.ToString(),
                            Status = task.Status.Spacify()
                        }).First();

                        _status = taskInfo.Status;

                        var properties = taskInfo.GetType().GetProperties();
                        foreach (var prop in properties)
                        {
                            var value = prop.GetValue(taskInfo).ToString();
                            phArguments.Controls.Add(new Label {
                                Text = prop.Name.Spacify() + ":", CssClass = "PropertyName"
                            });
                            phArguments.Controls.Add(new Label {
                                Text = value, CssClass = "PropertyValue"
                            });
                            phArguments.Controls.Add(new LiteralControl("<br />"));
                        }
                    }
            }
            catch (Exception ex)
            {
                return(false);
            }
            try
            {
                var arguments  = new SharpSerializer().Deserialize(Path.Combine(Basepath, ArgumentsPath, "Arguments.xml"));
                var properties = (from prop in arguments.GetType().GetProperties()
                                  let category =
                                      prop.IsDefined(typeof(CategoryAttribute), false)
                                          ? prop.GetCustomAttributes(typeof(CategoryAttribute), false)
                                      .Cast <CategoryAttribute>()
                                      .Single()
                                      .Category
                                          : "Other"
                                      let name =
                                          prop.IsDefined(typeof(DisplayNameAttribute), false)
                                          ? prop.GetCustomAttributes(typeof(DisplayNameAttribute), false)
                                          .Cast <DisplayNameAttribute>()
                                          .Single()
                                          .DisplayName
                                          : prop.Name
                                          let displayOrder =
                                              prop.IsDefined(typeof(PropertyOrderAttribute), false)
                                          ? prop.GetCustomAttributes(typeof(PropertyOrderAttribute), false)
                                              .Cast <PropertyOrderAttribute>()
                                              .Single()
                                              .Order
                                          : 999
                                              let shouldSerializeMethod = arguments.GetType().GetMethod("ShouldSerialize" + prop.Name)
                                                                          let shouldSerialize =
                                                  shouldSerializeMethod == null || (bool)shouldSerializeMethod.Invoke(arguments, new object[] { })
                                                  where shouldSerialize
                                                  orderby displayOrder
                                                  select new { Property = prop, Category = category, Name = name, displayOrder }).ToList();

                foreach (var prop in properties)
                {
                    var propertyType = prop.Property.PropertyType;
                    if (propertyType == typeof(Guid) || propertyType == typeof(Guid[]))
                    {
                        continue;
                    }

                    var basevalue = prop.Property.GetValue(arguments) ?? string.Empty;
                    if (string.IsNullOrWhiteSpace(basevalue.ToString()))
                    {
                        continue;
                    }

                    if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                    {
                        phArguments.Controls.Add(new Label
                        {
                            Text     = prop.Name.Spacify() + ":",
                            CssClass = "PropertyName",
                        });

                        phArguments.Controls.Add(new LiteralControl("<br />"));

                        var kvps = (IDictionary)basevalue;
                        foreach (DictionaryEntry kvp in kvps)
                        {
                            phArguments.Controls.Add(new Label
                            {
                                Text     = kvp.Key.ToString().Spacify(),
                                CssClass = "PropertyValueLeft"
                            });
                            phArguments.Controls.Add(new Label
                            {
                                Text     = kvp.Value.ToString().Spacify(),
                                CssClass = "PropertyValue"
                            });
                            phArguments.Controls.Add(new LiteralControl("<br />"));
                        }
                        continue;
                    }

                    var stringValue = basevalue.ToString();
                    if (propertyType.IsArray)
                    {
                        stringValue = string.Empty;
                        var values = (Array)prop.Property.GetValue(arguments);
                        for (var i = 0; i < values.Length; i++)
                        {
                            stringValue += (i > 0 ? ", " : string.Empty) + values.GetValue(i);
                        }
                    }

                    if (string.IsNullOrWhiteSpace(stringValue) || stringValue == new DateTime().ToString())
                    {
                        continue;
                    }

                    if (prop.Name.ToLower().EndsWith("path"))
                    {
                        try
                        {
                            var path = Path.GetFullPath(stringValue);
                            stringValue = Path.GetFileName(stringValue);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    phArguments.Controls.Add(new Label {
                        Text = prop.Name.Spacify() + ":", CssClass = "PropertyName"
                    });
                    phArguments.Controls.Add(new Label {
                        Text = stringValue, CssClass = "PropertyValue"
                    });
                    phArguments.Controls.Add(new LiteralControl("<br />"));
                }

                pnlArguments.Visible = true;
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }