简体   繁体   English

可以设置未在目标类型上定义的MetaType回调吗?

[英]Possible to set MetaType callbacks not defined on target type?

I am moving part of my codebase from using protobuf-net via the precompiler and attribute markup to manually adding types and their subtypes and fields to RuntimeTypeModel.Default. 我正在将代码库的一部分从通过预编译器和属性标记使用protobuf-net移至将类型及其子类型和字段手动添加到RuntimeTypeModel.Default。

The intent is to disjoin protobuf-net details from the data classes and gain some easily overviewed manoeuvrability for some versioned auto-updating. 目的是使Protobuf-net详细信息与数据类脱节,并获得一些易于版本化的概述,以便进行某些版本化的自动更新。

So far, so good. 到现在为止还挺好。 I've hit a few snags which I'll bash my head against a bit more to see them solved, however one brick wall does seem to impede progress fairly stubbornly: 我遇到了一些障碍,我将用力猛击一下,以解决它们,但是一堵砖墙似乎确实顽固地阻碍了进步:

I cannot seem to bind OnDeserialized and similar callbacks for my MetaType to anything not defined on the target type. 我似乎无法将我的MetaType的OnDeserialized和类似的回调绑定到目标类型上未定义的任何内容。

Initially I was surprised to find MethodInfo variables holding these callbacks rather than simple delegates or events, but figured that combining the string variant of the register call with extension methods for the target type would do the trick. 最初,我很惊讶地发现拥有这些回调的MethodInfo变量而不是简单的委托或事件,但是我发现,将注册调用的字符串变体与目标类型的扩展方法结合起来可以解决问题。 Unfortunately this does not seem to be the case. 不幸的是,事实并非如此。

Note that the data classes and the serialisation code exists in separate assemblies, so unfortunately I am not able to less-neatly work around this with partial classes either. 请注意,数据类和序列化代码存在于单独的程序集中,因此,不幸的是,我也无法通过部分类来更好地解决此问题。

Am I missing something or is it simply not possible to register callbacks for the serialisation of a type, defined somewhere other than on that type? 我是否遗漏了某些东西,或者根本无法为类型的序列化(在该类型以外的其他地方定义)注册回调?

For reference, I am currently using protobuf-net r668. 供参考,我目前正在使用protobuf-net r668。

Edit: Unable to delay further, I have opted for manually running these events around the ProtoBuf serialisation callbacks. 编辑:无法进一步延迟,我选择了在ProtoBuf序列化回调周围手动运行这些事件。 Still curious if this could be done though. 仍然好奇是否可以这样做。

That is currently not possible. 目前尚不可能。 It is expected that callbacks will be instance methods on the type being used. 预期回调将是所使用类型的实例方法。 Note, however, that some other parts of the code allow static methods accepting the instance as a parameter , so in theory it would be possible to change the code to allow something like this to be used as a callback: 但是请注意,代码的其他一些部分允许static方法将实例作为参数接受,因此从理论上讲 ,可以更改代码以允许将类似以下内容用作回调:

public static class Bar
{
    public static void Run(Foo foo)
    {
        Console.WriteLine("Got here");
    }
}

This is not possible right now, however, and could lead to confusion - especially over accessibility. 但是,这目前尚不可能,并且可能导致混乱-尤其是在可访问性方面。 The IL code pulls some tricks to mean that non-public methods can be used as callbacks, but to do this requires declaring the IL to be contextual to the DTO type; IL代码有一些技巧,意味着可以将非公共方法用作回调,但是要做到这一点,需要声明IL与DTO类型相关。 it can't cheat the accessibility by pretending the context is also the callback type without introducing a lot more complexity. 它不能通过假装上下文也是回调类型来欺骗可访问性,而不会引入更多复杂性。

So: 所以:

  • not currently 不是现在
  • possible in theory... 理论上可能的...
  • ...but I am not sure of the utility (how useful this would be, etc) of doing so ...但是我不确定这样做的实用性(这将有多大用处等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM