コード例 #1
0
    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);
    }
コード例 #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);
    }
コード例 #3
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("}");
    }
コード例 #4
0
    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);
    }
コード例 #5
0
    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);
    }
コード例 #6
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is char c))
        {
            return(false);
        }

        callback.AppendSingleValue($"'{c}'");
        return(true);
    }
コード例 #7
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (request.Instance != null)
        {
            return(false);
        }

        callback.AppendSingleValue("null");
        return(true);
    }
コード例 #8
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is bool b))
        {
            return(false);
        }

        callback.AppendSingleValue(DisplayBoolean(b));
        return(true);
    }
コード例 #9
0
    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);
    }
コード例 #10
0
    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);
    }
コード例 #11
0
    public bool Process(CustomTypeHandlerRequest request, ICsharpExpressionDumperCallback callback)
    {
        if (!(request.Instance is Type t))
        {
            return(false);
        }

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

        return(true);
    }
コード例 #12
0
    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);
    }
コード例 #13
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);
    }
コード例 #14
0
    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);
    }
コード例 #15
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);
    }
コード例 #16
0
    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);
    }
コード例 #17
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);
    }
コード例 #18
0
    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);
    }
コード例 #19
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("[]");
    }
コード例 #20
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("{");
    }
コード例 #21
0
    private void DoProcessRecursive(object?instance, Type?type, StringBuilder builder, int level)
    {
        var instanceType     = type ?? instance?.GetType();
        var instanceRequest  = new CustomTypeHandlerRequest(instance, instanceType, level);
        var instanceIsCustom = _customTypeHandlers.ProcessUntilSuccess(x => x.Process(instanceRequest, _instanceCallback));

        if (!instanceIsCustom && instanceType != null)
        {
            var isAnonymousType = instanceType.IsAnonymousType();
            _instanceCallback.Append("new ");

            if (!isAnonymousType)
            {
                _instanceCallback.AppendTypeName(instanceType);
            }

            var objectHandlerCommand = new ObjectHandlerRequest(instance, instanceType, level, type, isAnonymousType);
            var success = _objectHandlers.ProcessUntilSuccess(x => x.ProcessInstance(objectHandlerCommand, _instanceCallback));
            if (!success)
            {
                throw new InvalidOperationException($"There is no object handler which supports object of type [{instanceType?.FullName}]");
            }
        }
    }
 public bool IsPropertyCustom(CustomTypeHandlerRequest propertyCommand, string prefix, string suffix)
 => _typeHandlers.ProcessUntilSuccess(x => x.Process(propertyCommand, CreateNestedCallback(prefix, suffix)));