Exemplo n.º 1
0
		public string BuildAction(ManagerAction action, string internalActionId)
		{
			PropertyInfo getter;
			object[] attrs;
			object value;
			StringBuilder sb = new StringBuilder();
			string valueAsString = string.Empty;
			bool ignoreValue = false;

			if (typeof(Action.ProxyAction).IsAssignableFrom(action.GetType()))
				sb.Append(string.Concat("ProxyAction: ", action.Action, Common.LINE_SEPARATOR));
			else
				sb.Append(string.Concat("Action: ", action.Action, Common.LINE_SEPARATOR));

			if (string.IsNullOrEmpty(internalActionId))
				valueAsString = action.ActionId;
			else
				valueAsString = string.Concat(internalActionId, Common.INTERNAL_ACTION_ID_DELIMITER, action.ActionId);

			if (!string.IsNullOrEmpty(valueAsString))
				sb.Append(string.Concat("ActionID: ", valueAsString, Common.LINE_SEPARATOR));

			Dictionary<string, PropertyInfo> getters = Helper.GetProperties(action.GetType(), Helper.PropertyMask.WithGet);

			foreach (string name in getters.Keys)
			{
				string nameLower = name.ToLower(Helper.CultureInfo);
				if (nameLower == "class" || nameLower == "action" || nameLower == "actionid")
					continue;

				getter = getters[name];
				Type propType = getter.PropertyType;
				if (!(propType == typeof(string)
					|| propType == typeof(bool)
					|| propType == typeof(double)
					|| propType == typeof(DateTime)
					|| propType == typeof(int)
					|| propType == typeof(long)
					|| propType == typeof(Dictionary<string, string>)
					)
					)
					continue;

				try
				{
					value = getter.GetValue(action, new object[] { });
				}
				catch (UnauthorizedAccessException ex)
				{
#if LOGGER
					logger.Error("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
					continue;
#else
					throw new ManagerException("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
#endif
				}
				catch (TargetInvocationException ex)
				{
#if LOGGER
					logger.Error("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
					continue;
#else
					throw new ManagerException("Unable to retrieve property '" + name + "' of " + action.GetType(), ex);
#endif
				}

				if (value == null)
					continue;

				attrs = getter.GetCustomAttributes(typeof(ConditionIgnoreAttribute), true);

				foreach (var item in attrs)
				{
					var attr = item as ConditionIgnoreAttribute;

					if (attr.Value.Equals(value))
					{
						ignoreValue = true;
						break;
					}
				}

				if (ignoreValue)
				{
					ignoreValue = false;
					continue;
				}

				if (value is string)
				{
					valueAsString = (string)value;
					if (valueAsString.Length == 0)
						continue;
				}
				else if (value is bool)
					valueAsString = ((bool)value ? "true" : "false");
				else if (value is DateTime)
					valueAsString = value.ToString();
				else if (value is IDictionary)
				{
					valueAsString = Helper.JoinVariables((IDictionary)value, Common.LINE_SEPARATOR, ": ");
					if (valueAsString.Length == 0)
						continue;
					sb.Append(valueAsString);
					sb.Append(Common.LINE_SEPARATOR);
					continue;
				}
				else
					valueAsString = value.ToString();

				sb.Append(string.Concat(name, ": ", valueAsString, Common.LINE_SEPARATOR));
			}

			sb.Append(Common.LINE_SEPARATOR);
			return sb.ToString();
		}