转储使用 C# 中的 Clay 构建的对象

发布于 2024-10-19 00:06:00 字数 685 浏览 4 评论 0原文

有什么方法可以使用 C# 中的 ClaySharp 将对象转储到控制台/日志文件构建吗?

Clay 是一种动态 C# 类型,使您能够像在 JavaScript 或其他动态语言中一样轻松地雕刻任何形状的对象。 项目链接

使用 Clay 构建 Ex 对象:

// 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");

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");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2024-10-26 00:06:00

我认为这可能需要向 Clay 添加一个功能,因为我们所追求的详细信息位于 PropBehaviour 的私有字段内名为 _behaviour (集合)的私有字段<属性的 code>_props 集合,以及数组的 ArrayBehaviour 的私有字段 _data 内。

也许是一个新的 ClayBehavior(或更可能是 PropBehaviour 的扩展),类似于 ArrayBehaviourGetEnumerator 魔法,列出了所有属性...


更新: 试试这个:

    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,
};

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,
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文