예제 #1
0
        public static void WriteMethod(TextWriter textWriter, object obj, string methodName, params object[] parameters)
        {
            Assert.IsNotNull(textWriter);
            Assert.IsNotNull(obj);

            var type           = obj.GetType();
            var methodInfo     = type.GetMethod(methodName);
            var parameterInfos = methodInfo.GetParameters();
            var typeName       = TypeNameCollection.GetTypeName(methodInfo.ReturnType);
            var line           = typeName + " " + methodName + "(" + Environment.NewLine;
            var length         = Math.Min(parameters.Length, parameterInfos.Length);

            for (var i = 0; i < length; i++)
            {
                typeName = TypeNameCollection.GetTypeName(parameterInfos[i].ParameterType);

                line +=
                    "  " + typeName + " " +
                    parameterInfos[i].Name + " = " +
                    parameters[i];

                if (i < length - 1)
                {
                    line += "," + Environment.NewLine;
                }
            }

            line += ')';

            textWriter.Write(line);
        }
예제 #2
0
    public void Write(TextWriter textWriter)
    {
        string typeName;
        Type   type = null;

        if (Value != null)
        {
            type     = Value.GetType();
            typeName = TypeNameCollection.GetTypeName(type);
        }
        else
        {
            typeName = "object";
        }

        textWriter.Write("  " + typeName + " " + Name + " = ");

        if (type != null)
        {
            if (type.IsArray)
            {
                var elementType = type.GetElementType();

                if (elementType == typeof(byte))
                {
                    var inArray = (byte[])Value;
                    var base64  = System.Convert.ToBase64String(inArray);
                    textWriter.WriteLine(base64);
                }
                else
                {
                    var array = (Array)Value;

                    if (array.Length > 0)
                    {
                        textWriter.WriteLine();

                        var index = 0;

                        foreach (var arrayItem in array)
                        {
                            textWriter.Write("    [" + index + "] = ");
                            Write(arrayItem, textWriter);
                            index++;
                        }
                    }
                }
            }
            else
            {
                Write(Value, textWriter);
            }
        }
        else
        {
            Write(Value, textWriter);
        }
    }
예제 #3
0
        /// <summary>
        /// Constructs a parse tree for an Inherits declaration.
        /// </summary>
        /// <param name="inheritedTypes">The types inherited or implemented.</param>
        /// <param name="span">The location of the parse tree.</param>
        /// <param name="comments">The comments for the parse tree.</param>
        public InheritsDeclaration(TypeNameCollection inheritedTypes, Span span, IList <Comment> comments) : base(TreeType.InheritsDeclaration, span, comments)
        {
            if (inheritedTypes == null)
            {
                throw new ArgumentNullException("inheritedTypes");
            }

            SetParent(inheritedTypes);

            _InheritedTypes = inheritedTypes;
        }
    public static void Write(XmlWriter xmlWriter, ConfigurationAttributeCollection attributes)
    {
        foreach (var attribute in attributes)
        {
            using (xmlWriter.WriteElement(ConfigurationElementName.Attribute))
            {
                xmlWriter.WriteAttributeString("name", attribute.Name);
                var value = attribute.Value;

                if (value != null)
                {
                    var type = value.GetType();

                    if (type != typeof(string))
                    {
                        var typeName = TypeNameCollection.GetTypeName(type);
                        xmlWriter.WriteAttributeString("type", typeName);
                    }

                    var    typeCode = Type.GetTypeCode(type);
                    string strValue;

                    switch (typeCode)
                    {
                    case TypeCode.Object:
                        if (type == typeof(TimeSpan))
                        {
                            var timeSpan = (TimeSpan)value;
                            strValue = timeSpan.ToString();
                            xmlWriter.WriteAttributeString("value", strValue);
                        }
                        else if (type.IsArray)
                        {
                            var array = (Array)value;

                            for (var j = 0; j < array.Length; j++)
                            {
                                using (xmlWriter.WriteElement("a"))
                                {
                                    value    = array.GetValue(j);
                                    strValue = value.ToString();
                                    xmlWriter.WriteAttributeString("value", strValue);
                                }
                            }
                        }
                        else
                        {
                            var xmlSerializer = new XmlSerializer(type);
                            xmlSerializer.Serialize(xmlWriter, value);
                        }

                        break;

                    default:
                        strValue = value.ToString();
                        xmlWriter.WriteAttributeString("value", strValue);
                        break;
                    }
                }
                else
                {
                    xmlWriter.WriteAttributeString("isNull", bool.TrueString);
                }
            }
        }
    }