为什么我不能得到Type.GetType()找到我的插件实例中的app.config引用的类型? [英] Why can't I get Type.GetType() to find the type of my plugin instance referenced in app.config?

查看:104
本文介绍了为什么我不能得到Type.GetType()找到我的插件实例中的app.config引用的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里的交易。我有我的解决方案,其中有几个项目是:

So here's the deal. I've got my solution which has a few projects in it:

  • 系统包装项目 - 这只是一个控制台应用程序,目前,真实站在了窗口服务在调试过程中
  • 在一个工人的项目 - 这包含了code胆量。这样我可以轻松地调试$ C $下没有头痛的窗口服务。
  • 在一个插件库项目 - 这包含一个插件工厂插件的新的一个具体的实例
  • 在一个插件项目 - 这包含了具体的实现我的插件

我的包装的应用程序包含了我的app.config其中我的插件应直接引用自我配置。这样,我的包装的应用程序并不需要知道任何其他比它需要调用适配器工厂到插件的新出的实例。

My wrapper application contains my app.config which my plugin should reference directly for self configuration. This way my wrapper application doesn't need to know anything other than it needs to call the adapter factory to new up the 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>

包装项目:

using Worker;

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

职工的项目:

Worker project:

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();
            }
        }
    }
}

插件库:

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

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

插件:

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's plugin is working.");
        }

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

好了,这样设置舞台。在哪里我来脱胶是我的工人项目时,我引用 Type.GetType(pluginTypeName)。我的 pluginTypeName 正在从配置正确的拉升,但 Type.GetType(pluginTypeName)返回。我试过newing我的直接插件的实例为在code相同的地方:

Okay, so that sets the stage. Where I'm coming unstuck is in my worker project when I reference Type.GetType(pluginTypeName). My pluginTypeName is being pulled from the configuration correctly, but Type.GetType(pluginTypeName) returns null. I've tried newing up an instance of my plugin directly at that same place in the code:

var obj = new BensPlugin();

这工作得很好,和 obj.GetType()的ToString()返回完全相同的字符串作为我已经配置在应用程序。配置

This works just fine, and obj.GetType().ToString() returns the exact same string as I have configured in the app.config.

谁能告诉我为什么我就新的一个具体的物体,在code,但 Type.GetType(pluginTypeName)这一点会失败?

Can anyone tell me why I can new up a concrete object at that point in the code, but Type.GetType(pluginTypeName) would fail?

推荐答案

这可能是因为你需要指定组件类型位于:

It could be that you need to specify which assembly the type is located in:

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

这篇关于为什么我不能得到Type.GetType()找到我的插件实例中的app.config引用的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆