title: Generic function for generating dynamic components
body: <p>i would like to write a function in my service handling generating dynamic components in to some viewchild reference ...</p>
  
  <p>i tried like </p>
  
  <pre><code>public GenerateDynamicComponent(ComponentName: string,viewContainerRef: ViewContainerRef, data?: any) {   
   switch (ComponentName.toUpperCase()) {
   case 'DYNAMICFORMS':
     const componentFactory = this.resolver.resolveComponentFactory(DynamicFormsComponent);
     const formref = viewContainerRef.createComponent(componentFactory);
     formref.instance.Data = data;
     return formref;
     break;
  
   default:
    return null;
   }
  </code></pre>
  
  <p>and it does work well but i dont want to use this switch and send this componentname via string</p>
  
  <p>so it would need to be like</p>
  
  <pre><code> public GenerateDynamicComponent&lt;T&gt;(viewContainerRef: ViewContainerRef,data?: any ) {
   const componentFactory = this.resolver.resolveComponentFactory&lt;T&gt;(typeof T);
   const formref = viewContainerRef.createComponent(componentFactory);
   formref.instance.Data = data;
   return formref;
   }
  </code></pre>
  
  <p>but off course this does not work because typeof T &lt;> component type...
  is it possible to do or it need to be as in 1st example ?</p>
  
  <p>thanks a lot!</p>