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

0

I have a solution where it contains 2 projects.

One of them is the project I pulled through github, so I do not want to modify it because every time I update 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 do not want to modify the project already ready from git. Is there such a thing as?

    
asked by anonymous 29.03.2016 / 21:32

1 answer

0

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

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

What you will not be able to do is tinker 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. :)

Walkthrough:

1) Add the library as a reference.

In visual studio, for example, you must add the lib generated by project 1 to the correct VS folder. (It can be "C: \ Program Files \ Microsoft Visual Studio 14.0 \ VC \ 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);
        }
}
    
29.03.2016 / 21:36