Dump objects build with Clay in C#

927 Views Asked by At

Is any way to dump objects to console / logfile build with ClaySharp in C#?

Clay is a dynamic C# type that will enable you to sculpt objects of any shape just as easily as in JavaScript or other dynamic languages. Project Link

Ex object build with Clay:

// Build a Clay object
dynamic New = new ClayFactory();

dynamic directory = New.Array(
    New.Person(
        FirstName: "Louis",
        LastName: "Dejardin",
        Aliases: new[] { "Lou" }
    ),
    New.Person(
        FirstName: "Bertrand",
        LastName: "Le Roy"
    ).Aliases("bleroy", "boudin"),
    New.Person(
        FirstName: "Renaud",
        LastName: "Paquay"
    ).Aliases("Your Scruminess", "Chef")
).Name("Some Orchard folks");
1

There are 1 best solutions below

0
On

I think this might take adding a feature to Clay, as the details we are after live under a private field called _behaviour (a collection) inside the PropBehaviour's private field _props collection for properties, and also inside the ArrayBehaviour's private field _data for Arrays.

Perhaps a new ClayBehavior (or more likely an extension to PropBehaviour) similar to ArrayBehaviour's GetEnumerator magic that lists all the properties...


Update: Try this:

    public string DumpClay(dynamic clay, int indent = 0, bool appendLine = true)
    {
        var indentStr = new string('\t', indent);

        if (!(clay is Clay))
        {
            return indentStr + clay.ToString()
                + (appendLine ? ((indent == 0) ? ";" : ",") + Environment.NewLine : "");
        }


        var sb = new StringBuilder();

        if (IsArray(clay))
        {
            sb.Append(indentStr).AppendLine("[");
            foreach (var item in clay)
            {
                sb.AppendLine(DumpClay(item, indent + 2));
            }
            sb.Append(indentStr).Append("]");
        }

        PropBehavior behaviour;
        if (IsProp(clay, out behaviour))
        {
            if (sb.Length > 0)
            {
                sb.AppendLine();
            }

            sb.Append(indentStr).AppendLine("{");
            foreach (var pair in GetProps(behaviour))
            {
                sb.Append(DumpClay(pair.Key, indent + 1, appendLine: false)).AppendLine(":")
                    .Append(DumpClay(pair.Value, indent + 2));
            }
            sb.Append(indentStr).Append("}");
        }

        if (appendLine)
        {
            sb.AppendLine((indent == 0) ? ";" : ",");
        }

        return sb.ToString();
    }

    private static bool IsArray(Clay clay)
    {
        var behaviours = GetBehaviorCollection(clay);

        var arrayBehaviour = behaviours.FirstOrDefault(clayBehavior => clayBehavior is ArrayBehavior);

        return (arrayBehaviour != null);
    }

    private static bool IsProp(Clay clay, out PropBehavior propBehavior)
    {
        var behaviours = GetBehaviorCollection(clay);

        propBehavior = (PropBehavior)behaviours.FirstOrDefault(clayBehavior => clayBehavior is PropBehavior);

        return (propBehavior != null);
    }

    private static IEnumerable<IClayBehavior> GetBehaviorCollection(Clay clay)
    {
// ReSharper disable PossibleNullReferenceException
        return (ClayBehaviorCollection)typeof(Clay)
            .GetField("_behavior", BindingFlags.NonPublic | BindingFlags.Instance)
            .GetValue(clay);
// ReSharper restore PossibleNullReferenceException
    }

    private static IEnumerable<KeyValuePair<object, object>> GetProps(PropBehavior propBehavior)
    {
// ReSharper disable PossibleNullReferenceException
        return (Dictionary<object, object>)typeof(PropBehavior)
            .GetField("_props", BindingFlags.NonPublic | BindingFlags.Instance)
            .GetValue(propBehavior);
// ReSharper restore PossibleNullReferenceException
    }


Sample output of the above:

[
        {
            ShapeName:
                Person,
            FirstName:
                Louis,
            LastName:
                Dejardin,
            Aliases:
                System.String[],
        },
        {
            ShapeName:
                Person,
            FirstName:
                Bertrand,
            LastName:
                Le Roy,
            Aliases:
                [
                        bleroy,
                        boudin,
                ]
                {
                },
        },
        {
            ShapeName:
                Person,
            FirstName:
                Renaud,
            LastName:
                Paquay,
            Aliases:
                [
                        Your Scruminess,
                        Chef,
                ]
                {
                },
        },
]
{
    Name:
        Some Orchard folks,
};