示例#1
0
        private static void DescribeParameter(int recursionDepth, DescriptorBuilder parameterDescription, IEnumerable value)
        {
            Contract.Requires(parameterDescription != null);

            if (RecursionDepthExceeded(recursionDepth, parameterDescription))
            {
                return;
            }

            if (value == null)
            {
                parameterDescription.AppendLine("null");
                return;
            }

            bool isFirst = true;

            parameterDescription.AppendLine("[");
            parameterDescription.Indent();
            foreach (object item in value)
            {
                if (!isFirst)
                {
                    parameterDescription.AppendLine(",");
                }
                isFirst = false;

                DescribeParameter(recursionDepth + 1, parameterDescription, item.GetType(), item);
            }

            parameterDescription.Append("]");
            parameterDescription.Outdent();
        }
示例#2
0
        private static void DescribeClassParameter(int recursionDepth, DescriptorBuilder descriptorBuilder, object value)
        {
            Contract.Requires(descriptorBuilder != null);
            Contract.Requires(value != null);

            if (RecursionDepthExceeded(recursionDepth, descriptorBuilder))
            {
                return;
            }

            // Describing a class parameter requires describing each property, each of which can also
            // be class parameters. Therefore there is an element of recursion.

            var properties = value.GetType().GetProperties();

            descriptorBuilder.Append(value.GetType().FullName);
            descriptorBuilder.AppendLine("[");
            descriptorBuilder.Indent();
            bool isFirst = true;

            foreach (var propertyInfo in properties)
            {
                if (!isFirst)
                {
                    descriptorBuilder.AppendLine(",");
                }

                isFirst = false;

                descriptorBuilder.Append(propertyInfo.Name);
                descriptorBuilder.Append(": ");
                try
                {
                    var propertyValue = propertyInfo.GetValue(value, BindingFlags.GetProperty, null, null,
                                                              CultureInfo.InvariantCulture);
                    DescribeParameter(recursionDepth + 1, descriptorBuilder, propertyInfo.PropertyType, propertyValue);
                }
                catch (NullReferenceException)
                {
                    descriptorBuilder.Append("null");
                }
                catch (Exception exception)
                {
                    descriptorBuilder.Append(exception.Message);
                }
            }

            descriptorBuilder.Append("]");
            descriptorBuilder.Outdent();
        }