在 Microsoft Bond 的子命名空间中使用十进制转换失败

Decimal conversion fails when used in sub-namespace in Microsoft Bond

按照此处的示例:https://github.com/Microsoft/bond/tree/master/examples/cs/core/decimal

我试图在不在基本命名空间中的结构中使用 decimal 并得到异常 "Expression of type 'System.Decimal' cannot be used for assignment to type 'System.ArraySegment`1[System.Byte]'"。

如果我将所有结构保存在同一个命名空间中,一切都会很好。我必须做某种类型的排位赛吗?

我整理了一个小项目和几个单元测试来演示:https://github.com/oculuss/BondDecimalExample

TestA 的所有内容都在同一个命名空间中。 TestB 有几个子命名空间(并且是什么中断)。

必须定义一个 BondTypeAliasConverter "in the same assembly and namespace as the class representing the Bond schema(s) in which the type alias is used or assembly/namespace of one of the types being converted." 它不能在父命名空间中定义。搜索算法不是那么聪明。 :-) 因此,在 TestB 中,您需要将 BondTypeAliasConverter class 放在 C# 命名空间 "BondExampleB.Global.SecondType.SecondTypeA" 中(或者您在 C# 中将其映射到的任何位置)。

有一个开放的设计提案可以使这更容易一些。请参阅 Bond GitHub 项目中的 issue #594

在此之前,如果您想为不同命名空间中的类型使用相同的转换器,则需要执行以下操作:

namespace Util
{
    public static class BondTypeAliasConverter
    {
        public decimal Convert(ArraySegment<byte> blob, decimal unused) { ... }
        public ArraySegment<byte> Convert(decimal d, ArraySegment<byte> unused) { ... }
    }
}

namespace First
{
    public static class BondTypeAliasConverter
    {
        public decimal Convert(ArraySegment<byte> blob, decimal unused)
        {
            return Util.BondTypeAliasConverter.Convert(blob, unused);
        }

        ....
    }
}

namespace First.Second
{
    public static class BondTypeAliasConverter
    {
        public decimal Convert(ArraySegment<byte> blob, decimal unused)
        {
            return Util.BondTypeAliasConverter.Convert(blob, unused);
        }

        ....
    }
}