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方法,因此我没有任何想法。