How to override a class from an external project to call a function

Asked

Viewed 77 times

0

I have a solution where contains 2 projects.

One of them is the project I pulled from github, so I don’t want to modify it because every time I upgrade I will lose my settings.

In this project there is a class called:

public class Cielo
{
        String sendHttpRequest (String message)
        {
//Quero q toda vez que o sistema chamar esse metódio chame um código meu, ex:
//var gravaXML = new SalvaXML();
//gravaXML.GravarFisicamente(message);
        }
}

But I want to inject this function through my 2nd project, I don’t want to modify the ready-made project coming from git. Exists as?

1 answer

0


You want to create the Mycielo that inherits from the original Cielo class.

In this class you can change any behavior of virtual functions and add new methods smoothly.

What you won’t be able to do is tamper with private variables and methods. If you can do everything you need in sendHttpRequest using the protected and public part of the class it’s even easy. :)


Step by step:

1) Add library as reference.

In visual studio, for example, you should add the lib generated by project 1 to the correct VS folder. (Can be "C: Microsoft Visual Studio 14.0 VC Program Files include" for VS2015).

There you create a folder for the project with all the code. And include it as a reference in References by right-clicking and passing the library.

2) Create the new class.

At the top you add the file that points to the class you want to extend.

using Cielo;

And in this file you create the class with all the functions you need.

public class MyCielo : Cielo
{
        String sendHttpRequest (String message)
        {
//Quero q toda vez que o sistema chamar esse metódio chame um código meu, ex:
//var gravaXML = new SalvaXML();
//gravaXML.GravarFisicamente(message);
        }
}
  • could put a very generic example?

  • this #include <projectExtern/PATH filename/File. H is C# ?

  • but only one question,the other object should no longer call the Cielo base class but my Mycielo Class which inherits all methods from Cielo and adds a new function?

  • That’s right... you use Mycielo instead of Cielo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.