title: Dynamic function call for service in typescript
body: <p>I am working on Angular2 project in which i need to generate dynamic function which will be able to call the service provided under the service class. The service class has some 10 get functions as the following.</p>
  
  <p>eg:</p>
  
  <p>my service class</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  
  @Injectable()
  export class service {
  
    constructor() { }
  
    get function1(){
      return 1;
    }
  
    get function2(){
      return 2;
    }
  
    get function2(){
      return 3;
    }
  }
  </code></pre>
  
  <p>I am trying to create a function which take parameter as the function name and return the corresponding answer.</p>
  
  <p>eg:</p>
  
  <p>my app.component.ts</p>
  
  <pre><code>import { Component} from '@angular/core';
  import {service} from "./service";
  
  @Component({
        selector: 'app',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css'],
        providers : [service]
   })
  
   export class AppComponent(){
  
    constructor(private _service:service){}
  
    let one = getVal(function1); /// This should return 1
    let two = getVal(function2); /// this should return 2
  
    getVal(val){
       return this._service.val; // but i am getting error val not exist on type service
      }
  
  }
  </code></pre>
  
  <p>Is their any solution for this since it will help me to reduce my code and performance.</p>
  
  <p>Thanks in advance</p>