简体   繁体   English

是否可以覆盖第三方库中的方法?

[英]Is it possible to override a method in a 3rd party library?

I am using the 3rd party library Google VR in Unity. 我在Unity中使用第三方库Google VR。 Within one C#-source file of that lib, I add a static method call to a class LibClass like this: 在该库的一个C#源文件中,我向类LibClass添加一个静态方法调用,如下所示:

void onPrerender() {
    MyOwnClass.staticMethod();
    (continue with original method call...)
}

But every time I update the library, the call to MyOwnClass.staticMethod() is lost and I have to re-add it manually. 但是,每次我更新库时,对MyOwnClass.staticMethod()的调用都会丢失,我必须手动重新添加它。 I can not subclass LibClass, because the library would always call LibClass and not LibSubClass. 我不能继承LibClass,因为该库将始终调用LibClass而不是LibSubClass。 Is there a way to programmatically add the call to staticMethod() to LibClass? 有没有办法以编程方式将对staticMethod()的调用添加到LibClass? Or any other way? 还是其他方式?

Edit: I've been asked to describe the problem better: 编辑:我被要求更好地描述问题:

OnPreRender is a method of the MonoBehaviour class of Unity. OnPreRender是Unity的MonoBehaviour类的方法。 This method is called before the image of the camera is rendered. 在渲染摄像机图像之前调用此方法。 My problem is I need Google VR, which handles 2 separate cameras for each eye, to draw one of the camera images slightly different than the other. 我的问题是我需要Google VR,该VR为每只眼睛处理2个独立的摄像头,以绘制其中一个摄像头图像与另一个摄像头图像稍有不同。 Since OnPreRender is called for each of the cameras, I used this as an entry point for calling a static method in my own class (static only because that way I don't have to instantiate my own class in Google VRs class). 由于每个摄像机都调用OnPreRender,因此我将其用作在我自己的类中调用静态方法的入口(仅因为这样我不必在Google VRs类中实例化自己的类,所以它才是静态方法)。

This works fine for many months now, but the Google VR lib is constantly updated and each time I do that, the method call to my own class is lost. 现在可以正常工作数月,但是Google VR库会不断更新,每次执行此操作时,对我自己的类的方法调用都会丢失。

So I was wondering wether it is possible to kind of programmatically say "You there, OnPreRender() in that external class, when you are called by the library, jump over here and do some extra work." 因此,我想知道是否有可能以编程方式说“在那儿,外部类中的OnPreRender(),当库调用您时,跳过这里并做一些额外的工作。” Does that make sense? 那有意义吗?

You can try using extension methods. 您可以尝试使用扩展方法。 Bare in mind this work only for the static calls to object functions. 切记,此功能仅适用于对对象函数的静态调用。

You can find more: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods 您可以找到更多信息: https : //docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

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

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