public bool ProcessInstance(ObjectHandlerRequest command, ICsharpExpressionDumperCallback callback)
    {
        if (command.Instance == null)
        {
            return(false);
        }

        var type = command.Type ?? command.InstanceType;

        if (type == null)
        {
            return(false);
        }

        var level               = command.Level + 1;
        var first               = true;
        var ctor                = callback.ResolveConstructor(type);
        var properties          = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        var processedProperties = new List <string>();

        first = AppendReadOnlyProperties(command, callback, level, first, ctor, properties, processedProperties);

        level--;

        AppendFinalize(command, callback, level, first, properties, processedProperties);

        return(true);
    }
Beispiel #2
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is IEnumerable enumerable) ||
            request.InstanceType == null)
        {
            return(false);
        }

        var items      = enumerable.Cast <object>().ToArray();
        var typeSuffix = GetTypeSuffix(items, request.Instance);

        AppendInitialization(request, callback, request.InstanceType, typeSuffix);
        var level = request.Level + 1;

        foreach (var item in items)
        {
            callback.ChainAppend(new string(' ', level * 4))
            .ChainProcessRecursive(item, item.GetType(), level)
            .ChainAppendLine(",");
        }
        level--;
        callback.Append(new string(' ', level * 4));
        if (TypeIsGenericSequence(request.InstanceType))
        {
            callback.Append("} )");
        }
        else
        {
            callback.Append("}");
        }
        callback.AppendSuffix();
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (request.Instance == null ||
            (request.InstanceType?.IsGenericType) != true ||
            (request.InstanceType.GetGenericTypeDefinition()?.FullName.StartsWith("System.ValueTuple`")) != true)
        {
            return(false);
        }

        var genericArguments = request.InstanceType.GetGenericArguments();

        AppendInitialization(callback, genericArguments);

        var t     = request.Instance.GetType();
        var first = true;

        for (int i = 1; i <= genericArguments.Length; i++)
        {
            first = AppendSeparator(callback, first);
            var item = t.GetField($"Item{i}").GetValue(request.Instance);
            callback.ProcessRecursive(item, item?.GetType(), request.Level);
        }

        callback.ChainAppend(")")
        .ChainAppendSuffix();

        return(true);
    }
Beispiel #4
0
    public static void ProcessWritableProperties(this ObjectHandlerRequest command,
                                                 ICsharpExpressionDumperCallback callback,
                                                 IEnumerable <PropertyInfo> properties)
    {
        callback.ChainAppendLine()
        .ChainAppend(new string(' ', command.Level * 4))
        .ChainAppendLine("{");

        var level = command.Level + 1;

        foreach (var property in properties)
        {
            var propertyValue = property.GetValue(command.Instance);
            var propertyType  = propertyValue?.GetType();
            callback.Append(new string(' ', level * 4));
            var propertyCommand  = new CustomTypeHandlerRequest(propertyValue, propertyType, level);
            var propertyIsCustom = callback.IsPropertyCustom(propertyCommand, $"{property.Name} = ", ",");
            if (!propertyIsCustom)
            {
                callback.ChainAppend(property.Name)
                .ChainAppend(" = ")
                .ChainProcessRecursive(propertyValue, propertyValue?.GetType(), level)
                .ChainAppend(",");
            }

            callback.AppendLine();
        }

        level--;

        callback.ChainAppend(new string(' ', level * 4))
        .ChainAppend("}");
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (request.Instance == null ||
            (request.InstanceType?.IsGenericType) != true ||
            request.InstanceType.GetGenericTypeDefinition() != typeof(KeyValuePair <,>))
        {
            return(false);
        }

        var genericArguments = request.InstanceType.GetGenericArguments();

        callback.ChainAppendPrefix()
        .ChainAppend("new ")
        .ChainAppendTypeName(typeof(KeyValuePair <,>))
        .ChainAppend("<")
        .ChainAppendTypeName(genericArguments[0])
        .ChainAppend(", ")
        .ChainAppendTypeName(genericArguments[1])
        .ChainAppend(">")
        .ChainAppend("(");

        var t     = request.Instance.GetType();
        var key   = t.GetProperty("Key").GetValue(request.Instance);
        var value = t.GetProperty("Value").GetValue(request.Instance);

        callback.ChainProcessRecursive(key, key?.GetType(), request.Level)
        .ChainAppend(", ")
        .ChainProcessRecursive(value, value?.GetType(), request.Level)
        .ChainAppend(")")
        .ChainAppendSuffix();

        return(true);
    }
Beispiel #6
0
    public bool ProcessInstance(ObjectHandlerRequest command, ICsharpExpressionDumperCallback callback)
    {
        var type = command.Type ?? command.InstanceType;

        if (type == null)
        {
            return(false);
        }

        var level               = command.Level + 1;
        var first               = true;
        var properties          = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        var processedProperties = new List <string>();

        foreach (var property in properties.Where(x => callback.IsPropertyValid(command, x)))
        {
            if (type.GetMethods().Any(x => x.Name == $"With{property.Name}"))
            {
                first = ProcessBuilderMethod(command, callback, level, first, processedProperties, property, "With");
            }
            else if (type.GetMethods().Any(x => x.Name == $"Add{property.Name}"))
            {
                first = ProcessBuilderMethod(command, callback, level, first, processedProperties, property, "Add");
            }
        }

        if (first)
        {
            // No 'With' or 'Add' methods found, let the default object handler handle this
            return(false);
        }

        return(true);
    }
    private static void AppendFinalize(ObjectHandlerRequest command,
                                       ICsharpExpressionDumperCallback callback,
                                       int level,
                                       bool first,
                                       PropertyInfo[] properties,
                                       List <string> processedProperties)
    {
        var writableProperties = properties.Where
                                 (
            property =>
            (command.IsAnonymousType || !property.IsReadOnly()) &&
            !processedProperties.Contains(property.Name) &&
            callback.IsPropertyValid(command, property)
                                 ).ToArray();

        if (!first)
        {
            callback.ChainAppend(new string(' ', level * 4))
            .ChainAppend(")");
        }
        else if (!command.IsAnonymousType && writableProperties.Length == 0)
        {
            callback.Append("()");
        }

        if (writableProperties.Length > 0)
        {
            command.ProcessWritableProperties(callback, writableProperties);
        }
    }
Beispiel #8
0
    private static bool ProcessBuilderMethod(ObjectHandlerRequest command,
                                             ICsharpExpressionDumperCallback callback,
                                             int level,
                                             bool first,
                                             List <string> processedProperties,
                                             PropertyInfo property,
                                             string methodPrefix)
    {
        if (first)
        {
            callback.Append("()");
            first = false;
        }
        var propertyValue = property.GetValue(command.Instance);

        var addedSomething = false;

        if (methodPrefix == "Add" &&
            !(propertyValue is string) &&
            propertyValue is IEnumerable enumerable)
        {
            var firstVal = true;
            level++;
            foreach (var value in enumerable.OfType <object>())
            {
                if (firstVal)
                {
                    firstVal       = false;
                    addedSomething = true;
                    callback.ChainAppendLine()
                    .ChainAppend(new string(' ', (level - 1) * 4))
                    .ChainAppend($".{methodPrefix}{property.Name}(")
                    .ChainAppendLine()
                    .ChainAppend(new string(' ', level * 4));
                }
                else
                {
                    callback.ChainAppend(",")
                    .ChainAppendLine()
                    .ChainAppend(new string(' ', level * 4));
                }
                callback.ProcessRecursive(value, value?.GetType(), level);
            }
        }
        else
        {
            addedSomething = true;
            callback.ChainAppendLine()
            .ChainAppend(new string(' ', level * 4))
            .ChainAppend($".{methodPrefix}{property.Name}(")
            .ChainProcessRecursive(propertyValue, propertyValue?.GetType(), level);
        }
        if (addedSomething)
        {
            callback.Append(")");
        }
        processedProperties.Add(property.Name);
        return(first);
    }
 public CsharpExpressionDumper(IEnumerable <IObjectHandler> objectHandlers,
                               IEnumerable <ICustomTypeHandler> customTypeHandlers,
                               ICsharpExpressionDumperCallback instanceCallback)
 {
     _objectHandlers     = new List <IObjectHandler>(objectHandlers);
     _customTypeHandlers = new List <ICustomTypeHandler>(customTypeHandlers);
     _instanceCallback   = instanceCallback;
 }
Beispiel #10
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (request.Instance != null)
        {
            return(false);
        }

        callback.AppendSingleValue("null");
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is bool b))
        {
            return(false);
        }

        callback.AppendSingleValue(DisplayBoolean(b));
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is char c))
        {
            return(false);
        }

        callback.AppendSingleValue($"'{c}'");
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if ((request.InstanceType?.IsValueType) != true)
        {
            return(false);
        }

        callback.AppendSingleValue(string.Format(CultureInfo.InvariantCulture, "{0}", request.Instance));
        return(true);
    }
    private static bool AppendSeparator(ICsharpExpressionDumperCallback callback, bool first)
    {
        if (!first)
        {
            callback.Append(", ");
        }
        else
        {
            first = false;
        }

        return(first);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is Type t))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend("typeof(")
        .ChainAppendTypeName(t)
        .ChainAppend(")");

        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is Uri uri))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend("new ")
        .ChainAppendTypeName(typeof(Uri))
        .ChainAppend($@"(""{uri.AbsoluteUri}"")")
        .ChainAppendSuffix();
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is TimeSpan timeSpan))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend($"new ")
        .ChainAppendTypeName(typeof(TimeSpan))
        .ChainAppend($"({timeSpan.Days}, {timeSpan.Hours}, {timeSpan.Minutes}, {timeSpan.Seconds}, {timeSpan.Milliseconds})")
        .ChainAppendPrefix();
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is Version version))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend("new ")
        .ChainAppendTypeName(typeof(Version))
        .ChainAppend($"({version.Major}, {version.Minor}, {version.Build}, {version.Revision})")
        .ChainAppendSuffix();
        return(true);
    }
Beispiel #19
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (request.Instance == null ||
            (request.InstanceType?.IsEnum) != true)
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppendTypeName(request.InstanceType)
        .ChainAppend('.')
        .ChainAppend(request.Instance)
        .ChainAppendSuffix();
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is string stringValue))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend('@')
        .ChainAppend(Quote)
        .ChainAppend(Format(stringValue))
        .ChainAppend(Quote)
        .ChainAppendSuffix();

        return(true);
    }
Beispiel #21
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (request.Instance is MyImmutableClass imm)
        {
            callback.ChainAppendPrefix()
            .ChainAppend("MyImmutableClass.Create(")
            .ChainAppendFormattedString(imm.Property1)
            .ChainAppend(", ")
            .ChainAppend(imm.Property2)
            .ChainAppend(')')
            .ChainAppendSuffix();
            return(true);
        }

        return(false);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is DateTime dateTime))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend($"new ")
        .ChainAppendTypeName(typeof(DateTime))
        .ChainAppend($"({dateTime.Year}, {dateTime.Month}, {dateTime.Day}, {dateTime.Hour}, {dateTime.Minute}, {dateTime.Second}, {dateTime.Millisecond}, ")
        .ChainAppendTypeName(typeof(DateTimeKind))
        .ChainAppend($".{dateTime.Kind})")
        .ChainAppendSuffix();
        return(true);
    }
Beispiel #23
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is Guid guid))
        {
            return(false);
        }

        callback.ChainAppendPrefix()
        .ChainAppend($"new ")
        .ChainAppendTypeName(typeof(Guid))
        .ChainAppend("(\"")
        .ChainAppend(guid)
        .ChainAppend("\")")
        .ChainAppendSuffix();
        return(true);
    }
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if ((request.InstanceType?.IsGenericType) != true ||
            !new[] { typeof(IDictionary <,>), typeof(Dictionary <,>) }.Contains(request.InstanceType.GetGenericTypeDefinition()))
        {
            return(false);
        }

        var genericArguments = request.InstanceType.GetGenericArguments();

        callback.ChainAppendPrefix()
        .ChainAppend("new ")
        .ChainAppendTypeName(request.InstanceType.GetGenericTypeDefinition())
        .ChainAppend("<")
        .ChainAppendTypeName(genericArguments[0])
        .ChainAppend(", ")
        .ChainAppendTypeName(genericArguments[1])
        .ChainAppendLine(">")
        .ChainAppend(new string(' ', request.Level * 4))
        .ChainAppendLine("{");

        var level = request.Level + 1;

        if (request.Instance is IEnumerable enumerable)
        {
            foreach (var kvp in enumerable)
            {
                var t     = kvp.GetType();
                var key   = t.GetProperty("Key").GetValue(kvp);
                var value = t.GetProperty("Value").GetValue(kvp);
                callback.ChainAppend(new string(' ', level * 4))
                .ChainAppend("[")
                .ChainProcessRecursive(key, key?.GetType(), level)
                .ChainAppend("] = ")
                .ChainProcessRecursive(value, value?.GetType(), level)
                .ChainAppendLine(",");
            }
        }

        level--;

        callback.ChainAppend(new string(' ', level * 4))
        .ChainAppend("}")
        .ChainAppendSuffix();

        return(true);
    }
    private static void AppendInitialization(ICsharpExpressionDumperCallback callback, Type[] genericArguments)
    {
        callback.ChainAppendPrefix()
        .ChainAppend("new ")
        .ChainAppendTypeName(typeof(ValueTuple <>))
        .ChainAppend("<");

        var first = true;

        foreach (var itemType in genericArguments)
        {
            first = AppendSeparator(callback, first);
            callback.AppendTypeName(itemType);
        }

        callback.ChainAppend(">(");
    }
    private static bool AppendInitialization(ICsharpExpressionDumperCallback callback,
                                             int level,
                                             bool first)
    {
        if (first)
        {
            first = false;
            callback.ChainAppendLine()
            .ChainAppend(new string(' ', (level - 1) * 4))
            .ChainAppendLine("(");
        }
        else
        {
            callback.AppendLine(",");
        }

        return(first);
    }
Beispiel #27
0
    private void AppendCustomInitialization(CustomTypeHandlerRequest request,
                                            ICsharpExpressionDumperCallback callback,
                                            Type?typeSuffix,
                                            Type collectionType)
    {
        callback.ChainAppendPrefix()
        .ChainAppend("new ")
        .ChainAppendTypeName(collectionType)
        .ChainAppend('<')
        .ChainAppendTypeName(request.InstanceType !.GetGenericArguments()[0])
        .ChainAppend(">(new");

        if (typeSuffix != null)
        {
            callback.ChainAppend(" ")
            .ChainAppendTypeName(typeSuffix);
        }
        callback.AppendLine("[]");
    }
    private static bool AppendReadOnlyProperties(ObjectHandlerRequest command,
                                                 ICsharpExpressionDumperCallback callback,
                                                 int level,
                                                 bool first,
                                                 ConstructorInfo?ctor,
                                                 PropertyInfo[] properties,
                                                 List <string> processedProperties)
    {
        if (!command.IsAnonymousType && ctor != null)
        {
            var arguments = ctor.GetParameters();
            if (arguments.Length > 0)
            {
                foreach (var argument in arguments)
                {
                    var readOnlyProperty = callback.ResolveReadOnlyProperty(properties, ctor, argument);
                    if (readOnlyProperty == null)
                    {
                        continue;
                    }

                    first = AppendInitialization(callback, level, first);

                    processedProperties.Add(readOnlyProperty.Name);

                    var value = readOnlyProperty.GetValue(command.Instance);

                    callback.ChainAppend(new string(' ', level * 4))
                    .ChainAppend(argument.Name)
                    .ChainAppend(": ")
                    .ChainProcessRecursive(value, value?.GetType(), level);
                }

                if (!first)
                {
                    callback.AppendLine();
                }
            }
        }

        return(first);
    }
Beispiel #29
0
    private void AppendInitialization(CustomTypeHandlerRequest request,
                                      ICsharpExpressionDumperCallback callback,
                                      Type instanceType,
                                      Type?typeSuffix)
    {
        if (TypeIsGenericSequence(request.InstanceType))
        {
            AppendCustomInitialization(request, callback, typeSuffix, instanceType.GetGenericTypeDefinition());
        }
        else
        {
            callback.ChainAppendPrefix()
            .ChainAppend("new");

            if (typeSuffix != null)
            {
                callback.ChainAppend(" ")
                .ChainAppendTypeName(typeSuffix);
            }
            callback.AppendLine("[]");
        }
        callback.ChainAppend(new string(' ', request.Level * 4))
        .ChainAppendLine("{");
    }
 public static ICsharpExpressionDumperCallback ChainAppendTypeName(this ICsharpExpressionDumperCallback callback, Type type)
 {
     callback.AppendTypeName(type);
     return(callback);
 }