Exemplo n.º 1
0
        static string ComplexObjectToPseudoCode(ExpressionToCodeConfiguration config, object?val, int indent, int valueSize)
        {
            var retval = ObjectToCode.PlainObjectToCode(val);

            if (val is string)
            {
                return(ElidePossiblyMultilineString(config, retval ?? throw new InvalidOperationException("retval cannot be null for strings"), indent, valueSize).Trim());
            }
            else if (retval != null)
            {
                return(ElideAfter(retval, valueSize));
            }
            else if (val == null)
            {
                throw new Exception("Impossible: if val is null, retval cannot be");
            }
            else if (val is Array arrayVal)
            {
                return("new[] " + FormatEnumerable(config, arrayVal, indent, valueSize - 6));
            }
            else if (val is IEnumerable enumerableVal)
            {
                return(FormatTypeWithListInitializerOrNull(config, val, indent, valueSize, enumerableVal) ?? FormatEnumerable(config, enumerableVal, indent, valueSize));
            }
            else if (val is Expression exprVal)
            {
                return(ElideAfter(config.GetExpressionToCode().ToCode(exprVal), valueSize));
            }
            else if (val is IStructuralComparable tuple && val is IComparable && CSharpFriendlyTypeName.IsValueTupleType(val.GetType().GetTypeInfo()))
            {
                var collector = new NastyHackyTupleCollector();
                tuple.CompareTo(tuple, collector);
                var sb = new StringBuilder();
                sb.Append("(");
                for (var index = 0; index < collector.CollectedObjects.Count; index++)
                {
                    var item     = collector.CollectedObjects[index];
                    var asString = ComplexObjectToPseudoCode(config, item, indent + 4, valueSize);
                    if (index > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(asString);
                }

                sb.Append(")");
                return(ElidePossiblyMultilineString(config, sb.ToString(), indent, valueSize).Trim());
            }
        static string ComplexObjectToPseudoCode(ExpressionToCodeConfiguration config, object val, int indent, int valueSize)
        {
            var retval = ObjectToCode.PlainObjectToCode(val);

            if (val is string)
            {
                return(ElidePossiblyMultilineString(config, retval, indent, valueSize).Trim());
            }
            else if (retval != null)
            {
                return(ElideAfter(retval, valueSize));
            }
            else if (val is Array arrayVal)
            {
                return("new[] " + FormatEnumerable(config, arrayVal, indent, valueSize - 6));
            }
            else if (val is IEnumerable enumerableVal)
            {
                var type = val.GetType();
                if (type.GetConstructor(Type.EmptyTypes) is ConstructorInfo ci && ci.IsPublic)
                {
                    foreach (var pi in type.GetProperties())
                    {
                        if (
                            pi.Name == "Item" &&
                            pi.CanWrite &&
                            pi.GetIndexParameters() is ParameterInfo[] indexPars &&
                            indexPars.Length == 1 &&
                            typeof(IEnumerable <>).MakeGenericType(typeof(KeyValuePair <,>).MakeGenericType(indexPars[0].ParameterType, pi.PropertyType)) is Type keyEnumerableType &&
                            keyEnumerableType.IsAssignableFrom(type)
                            )
                        {
                            var typeName = type.ToCSharpFriendlyTypeName();
                            return("new " + typeName + " " + PrintInitializerContents(config, enumerableVal, indexPars[0].ParameterType, pi.PropertyType, indent, valueSize - typeName.Length));
                        }
                    }
                }

                return(FormatEnumerable(config, enumerableVal, indent, valueSize));
            }
            else if (val is Expression exprVal)
            {
                return(ElideAfter(config.GetExpressionToCode().ToCode(exprVal), valueSize));
            }
            else if (val is IStructuralComparable tuple && val is IComparable && CSharpFriendlyTypeName.IsValueTupleType(val.GetType().GetTypeInfo()))
            {
                var collector = new NastyHackyTupleCollector();
                tuple.CompareTo(tuple, collector);
                var sb = new StringBuilder();
                sb.Append("(");
                for (var index = 0; index < collector.CollectedObjects.Count; index++)
                {
                    var item     = collector.CollectedObjects[index];
                    var asString = ComplexObjectToPseudoCode(config, item, indent + 4, valueSize);
                    if (index > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(asString);
                }

                sb.Append(")");
                return(ElidePossiblyMultilineString(config, sb.ToString(), indent, valueSize).Trim());
            }