static void Main(string[] args) { /*finge que aqui é uma injeção de dependencia por construtor*/ IImposto iss = new ISS(); IImposto icms = new ICMS(); IImposto xpto = new XPTO(); IImposto example = new Example(); /*fim da injeção*/ Orcamento orcamento = new Orcamento(500.0); CalculadorDeImpostos context; context = new CalculadorDeImpostos(iss); context.RealizaCalculo(orcamento); context = new CalculadorDeImpostos(icms); context.RealizaCalculo(orcamento); context = new CalculadorDeImpostos(xpto); context.RealizaCalculo(orcamento); context = new CalculadorDeImpostos(example); context.RealizaCalculo(orcamento); }
public static void Main1() { // Create obj to be used as "this" parameter DelegateIntro obj = new DelegateIntro(); // // Create delegate // Action action = new Action(DelegateIntro.MStatic); Action action1 = new Action(obj.MInstance); // Get method name + this //Action action1 = new Action(obj.MInstance(10));// Error, we are calling the method here // Or: Action action2 = DelegateIntro.MStatic; Action action3 = obj.MInstance; // // Invoke // action.Invoke(10); action1.Invoke(20); // Or: action(10); // Using "Method" property + reflection action.Method.Invoke(null, new object[] { 10 }); action1(20); // // Using GenericAction // GenericAction <int> ga = obj.MInstance; ga(99); ga.Invoke(99); // // Associate lambda // //Action lambda = new Action( (int i) => Console.WriteLine("Lambda, i = {0}", i)); // Simpler syntax, without calling new Action //Action lambda = (int i) => Console.WriteLine("Lambda, i = {0}", i); //Action lambda = i => Console.WriteLine("Lambda, i = {0}", i); //Action lambda = i => { Console.WriteLine("Lambda, i = {0}", i); return 0; }; // Error ///////////////////////////////////////// //Action lambda1 = i => obj.MInstance(i); // In order to capture the context (variable obj), // the above line generates an auxiliary class (nested class), e.g., // public class XPTO { // public DelegateIntro obj; // // public void Anonymous1(int i) { // obj.MInstance(i); // } // } // Main: // XPTO xpto = new XPTO(); // // Capture variable "obj" // xpto.obj = obj; // Action lambda1 = xpto.Anonymous1; // // // COMMENT/UNCOMMENT // XPTO xpto = new XPTO(); // Capture variable "obj" xpto.obj = obj; Action lambda1 = xpto.Anonymous1; lambda1(40); ///////////////////////////////////////// // Invoke //lambda(30); //// Or: //lambda.Invoke(30); }