title: How to create ServiceBase class using System.Reflection
body: <p>I want to create a service installer in my project, and I have several serviceBase classes in my project. This is the main program code:</p>
  
  <pre><code>static class Program
  {
      static void Main()
      {
          string[] temp = ConfigurationUtils.ReadSetting("XServices").Split(',');
          string serviceNamespace = ConfigurationUtils.ReadSetting("Services Namespace");
          ServiceBase[] servicesToRun;// = new ServiceBase[temp.Length];
  
          for (int i = 0; i &lt; temp.Length;i++ )
          {
              Type typeService = Type.GetType(serviceNamespace + "." + temp[i]);
              servicesToRun[i] = (ServiceBase) Activator.CreateInstance(typeService, true);
          }
  
          ServiceBase.Run(servicesToRun);
      }
  }
  </code></pre>
  
  <p>In this program I read the class (can be more than one class) that I want to create from app.config. And then I use Reflection to create the service (based on the app.config).
  I use app.config to make the services that I want to add configurable.</p>
  
  <p>Then, I can build my project and install the service. But, when I start my service, it can't load my service class.
  I think it is because the class that I just created cannot behave as my service class.</p>
  
  <p>This is my app.config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
      &lt;configuration&gt;
        &lt;appSettings&gt;
            &lt;add key="log_directory_performancelog" value="C:\Wcms\Modules\XPerformance\Data\" /&gt;
            &lt;add key="log_directory_website_monitoring" value="C:\Wcms\Modules\XWebsiteMonitoring\Data\"/&gt;
            &lt;add key="config_path_performance" value="C:\Program Files\Wcms\XLogClient\config.txt" /&gt;
            &lt;add key="config_path_website_monitoring" value="C:\Program Files\Wcms\XLogClient\website.monitor.config.txt" /&gt;
            &lt;add key="XServices" value="XLogClientPerformance,XLogClientWebsiteMonitoring" /&gt;
            &lt;add key="Services Namespace" value="XLog.Client" /&gt;
            &lt;add key="ClientSettingsProvider.ServiceUri" value="" /&gt;
      &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>