.Net平台下实例类型无法转换成接口类型?

首先这种情况出现在应用程序启动前的方法里面。

本想通过发射来实现一些功能。谁知道被这个坑了。

碰到这种问题。已经相当无语了。同时也不知道该如何解决。望有能之士帮忙解答

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.Hosting;
using Infrastructure;

[assembly: PreApplicationStartMethod(typeof(PreApplicationInit), "InitializePlugins")]
namespace Infrastructure
{
    public class PreApplicationInit
    {
        static PreApplicationInit()
        {
            var pluginsPath = HostingEnvironment.MapPath("~/plugins");
            
            var pluginsTempPath = HostingEnvironment.MapPath("~/plugins/temp");

            if (pluginsPath == null || pluginsTempPath == null)
	            throw new DirectoryNotFoundException("plugins");

            PluginFolder = new DirectoryInfo(pluginsPath);
            TempPluginFolder = new DirectoryInfo(pluginsTempPath);
        }

        /// <summary>
        /// The source plugin folder from which to copy from
        /// </summary>
        /// <remarks>
        /// This folder can contain sub folders to organize plugin types
        /// </remarks>
        private static readonly DirectoryInfo PluginFolder;

        /// <summary>
        /// The folder to  copy the plugin DLLs to use for running the app
        /// </summary>
        private static readonly DirectoryInfo TempPluginFolder;

        /// <summary>
        /// Initialize method that registers all plugins
        /// </summary>
        public static void InitializePlugins()
        {
            var assemblies = PluginFolder.GetFiles("*.dll", SearchOption.AllDirectories)
                    .Select(x => Assembly.LoadFrom(x.FullName));

            foreach (var assembly in assemblies)
            {
                var type = assembly.GetTypes().FirstOrDefault(t => t.GetInterface(typeof(IModule).Name) != null);
                if (type == null) continue;
                //Add the plugin as a reference to the application
                if (AppDomain.CurrentDomain.GetAssemblies().All(a => a.FullName != assembly.FullName))
                {
                    BuildManager.AddReferencedAssembly(assembly);
                }

                //Add the modules to the PluginManager to manage them later
                var module = (IModule)Activator.CreateInstance(type);
                PluginManager.Current.Modules.Add(module, assembly);
            }
        }
    }
}

  上面是代码。

如果通过Type.IsAssignableFrom方法来查找接口的实现,根本查不到。

 

这是目录结构

这是Module的实现

using System;
using Infrastructure;

namespace NewsPlugin
{
    public class Module:IModule
    {
        public string Name {
            get
            {
                return "NewsPlugin";
            }
            set
            {
                if (value == null) throw new ArgumentNullException("value");
                this.Name = value;
            }
        }
    }
}

 

 

转载于:https://www.cnblogs.com/showstyle/p/3706188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值