博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net平台下实例类型无法转换成接口类型?
阅读量:7056 次
发布时间:2019-06-28

本文共 2774 字,大约阅读时间需要 9 分钟。

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

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

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

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);        }        ///         /// The source plugin folder from which to copy from        ///         /// 
/// This folder can contain sub folders to organize plugin types ///
private static readonly DirectoryInfo PluginFolder; /// /// The folder to copy the plugin DLLs to use for running the app /// private static readonly DirectoryInfo TempPluginFolder; /// /// Initialize method that registers all plugins /// 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

你可能感兴趣的文章
三元运算符判断分数类型
查看>>
通过QC远程运行QTP脚本,QTP自动崩溃关闭的解决方法
查看>>
HTML资源定位器-URL
查看>>
定时器setTimeout()和setInterval()使用心得整理
查看>>
C#学习笔记③——手动调试与错误处理
查看>>
Oracle all_parameters 视图
查看>>
StringBuilder拼接字符串,“,”在前还是在后问题
查看>>
Linux 内核中断内幕【转】
查看>>
Linux内核驱动--mmap设备方法【原创】
查看>>
ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程
查看>>
网页中内容的显示问题
查看>>
JAVA编程思想三
查看>>
加密工具类
查看>>
ThinkPHP配置简单的mysql读写分离
查看>>
AngularJS Select(选择框)
查看>>
EXT.NET入门必读
查看>>
数据结构定义
查看>>
实验报告二201521460014
查看>>
sql中的Replace
查看>>
POJ 1068 AC 2014-01-07 15:24 146人阅读 评论(0) 收藏...
查看>>