English 中文(简体)
如何安全地将电传信号除去,而不提出例外?
原标题:How to safely deserialize MessagePack messages without throwing exceptions?
The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty. Szyszka947 is looking for an answer from a reputable source.

It s my DTO:

public class InvocationMessage
{
    public string MethodName { get; set; }
    public object?[]? Args { get; set; }
}

最初,我使用<代码>MessagePackSerializer.Serialize(Msg,TreatylessStandardResolver.Options);。

接着,在降水时,我想将<条码>目标从<条码>改为具体类型。 如果不采用习惯格式,这种情况就不可能发生。 因此,我写道:

public InvocationMessage Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
    var result = new InvocationMessage
    {
        MethodName = reader.ReadString()
    };

    // Hardcoded for testing purpose
    var argTypes = new Type[] { typeof(SomeType) };
    if (reader.TryReadArrayHeader(out int count))
    {
        result.Args = new object?[count];

        for (int i = 0; i < argTypes.Length; i++)
        {
            result.Args[i] = MessagePackSerializer.Deserialize(argTypes[i], ref reader,
                ContractlessStandardResolver.Options.WithSecurity(MessagePackSecurity.UntrustedData));
        }
    }

    return result;
}

public void Serialize(ref MessagePackWriter writer, InvocationMessage value, MessagePackSerializerOptions options)
{
    var methodName = Encoding.UTF8.GetBytes(value.MethodName);

    writer.WriteString(methodName);

    if (value.Args is not null && value.Args.Length > 0)
    {
        writer.WriteArrayHeader(value.Args.Length);

        foreach (var arg in value.Args)
        {
            MessagePackSerializer.Serialize(ref writer, arg, ContractlessStandardResolver.Options);
        }
    }
}

Unfortunelly, the exception is always thrown when I try to deserialize e.g. something like this:

var msg = new InvocationMessage
{
    MethodName = "sometestname",
    Args = new object?[]
        {
            new 
            {
                Test = 3
            },
            new
            {
                ReceiverId = "12ds",
                Content = "s",
            },
            new 
            {
                Test2 = 5
            },
        }
};

这是因为我想将一些不为人所知的类型降为我的<代码>SomeType,或者因为我的<代码>SomeType想要在财产上<代码>上<>载明<>>,并取到<编码>int/code>。 有许多案件。

但我需要避免沉 throw,因为它给我带来很大的业绩问题。 没有任何东西,例如<代码>TryDeserialize方法,因此我没有任何想法。

问题回答

你可以安全地淡化电文,而不通过利用试捕组处理在脱硫期间可能出现的例外情况而放弃例外。 例如:

public InvocationMessage Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
    var result = new InvocationMessage
    {
        MethodName = reader.ReadString()
    };

    if (reader.TryReadArrayHeader(out int count))
    {
        result.Args = new object?[count];

        for (int i = 0; i < count; i++)
        {
            try
            {
                result.Args[i] = MessagePackSerializer.Deserialize<object>(ref reader, options);
            }
            catch (Exception ex)
            {
                result.Args[i] = null;
            }
        }
    }

    return result;
}

如果出现例外情况,则将相应的<代码>Args要素至null(或任何其他违约数值)并继续进行脱硫。 这样,你就可以避免在继续处理数据的同时打破你的帝国化进程。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签