Why can't I get Type.GetType () to find the type of my plugin instance referenced in app.config?

So here's the deal. I have my solution in which there are several projects:

  • A wrapper project is just a console application that currently runs on a Windows service during debugging.
  • Working draft - this contains guts of code. This way I can easily debug code for a Windows service without a headache.
  • Plugin library project - contains the factory plugin for a new concrete instance of the plugin.
  • Plugin project - contains a specific implementation of my plugin.

My wrapper application contains the app.config file, which my plugin should reference directly to its own configuration. Thus, my wrapper application does not need to know anything except how it needs to call the factory adapter for a new instance of the plugin.

<configuration>
  <appSettings>
    <add key="Plugin" value="Prototypes.BensPlugin" />
    <add key="Prototypes.BensPlugin.ServiceAddress" value="http://localhost/WebServices/Plugin.asmx" />
    <add key="Prototypes.BensPlugin.Username" value="TestUserName" />
    <add key="Prototypes.BensPlugin.Password" value="TestPassword" />
  </appSettings>
</configuration>

Project Wrapper:

using Worker;

public class Program
{
    public static void Main()
    {
        var serviceProc = new ServiceProcess();
        serviceProc.DoYourStuff();
    }
}

Working draft:

using PluginLibrary;

namespace Worker
{
    public class ServiceProcess
    {
        public string GetRequiredAppSetting(string settingName)
        {
            /* Code to get a required configuration setting */
        }

        public void DoYourStuff()
        {
            string pluginTypeName = GetRequiredAppSetting("Plugin"); 
            //At this point, pluginTypeName = "Prototypes.BensPlugin"
            Type type = Type.GetType(pluginTypeName); //type == null, wth!?

            //string testTypeName = new BensPlugin().GetType().ToString();
            ///At this point, testTypeName = "Prototypes.BensPlugin"
            //Type testType = Type.GetType(testTypeName)
            ///And testType still equals null!

            if (type != null)
            {
                IPlugin plugin = PluginFactory.CreatePlugin(type);
                if (plugin != null)
                plugin.DoWork();
            }
        }
    }
}

Plugin Library:

namespace PluginLibrary
{
    public interface IPlugin
    {
        void DoWork();
    }

    public class PluginFactory
    {
        public IPlugin CreatePlugin(Type pluginType)
        {
            return (IPlugin)Activator.CreateInstance(pluginType);
        }
    }
}

Plugin:

using PluginLibrary;

namespace Prototypes
{
    public class BensPlugin : IPlugin
    {
        public string ServiceAddress { get; protected set; }
        public string username { get; protected set; }
        public string password { get; protected set; }

        public void DoWork()
        {
            Trace.WriteLine("Ben plugin is working.");
        }

        public BensPlugin()
        {
            /* Code to self configure from app.config */
        }
    }
}

Good, so it sets the scene. Where I will stick up in my working draft when I refer to Type.GetType(pluginTypeName). My pluginTypeNameis retrieved from the configuration correctly, but Type.GetType(pluginTypeName)returns null. I tried updating the instance of my plugin right in the same place in the code:

var obj = new BensPlugin();

This works fine, but obj.GetType().ToString()returns the same string as in app.config.

Can someone tell me why I can create a specific object at this point in the code, but Type.GetType(pluginTypeName)failed?

+3
3

, , :

<add key="Plugin" value="Prototypes.BensPlugin, TheAssemblyName" />
+8

Type.GetType(pluginTypeName) Type.GetType(pluginTypeName, true). , , null - , () , GetType null

+2

pluginTypeName ? , pluginTypeName, ?

If you use the name assigned to Assembly, then .NET should be able to automatically download the assembly for you; if you do not, I would suggest that it Type.GetType()returns null, as it cannot find the assembly containing the requested type.

+1
source

Source: https://habr.com/ru/post/1722123/


All Articles