简体   繁体   中英

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

I am using the 3rd party library Google VR in Unity. Within one C#-source file of that lib, I add a static method call to a class LibClass like this:

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. I can not subclass LibClass, because the library would always call LibClass and not LibSubClass. Is there a way to programmatically add the call to staticMethod() to 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. 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. 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).

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.

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." 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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