基本信息
源码名称:C# 插件式编程框架源码下载
源码大小:0.16M
文件格式:.rar
开发语言:C#
更新时间:2016-02-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

插件式编程主要步骤:

(1)、定义公共的接口以及抽象类。

(2)、定义和实现相关组件。

(3)、实现通用程序集反射操作类。

其中,公共的接口和抽象类定义在组件Jasen.Framework.Core中,该组件中提供通用程序集反射操作类AssemblyUtility;具体实现的相关组件为Jasen.Framework.Oracle、Jasen.Framework.Access和Jasen.Framework.SqlServer,它们都实现了Jasen.Framework.Core中的公共接口。客户端可以根据实际情况来进行相应的操作。相关组件图如下:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace Jasen.Framework.Core
{
    public static class AssemblyUtility
    {
        public static IEnumerable<Type> GetImplementdTypesByDirectory<T>(string baseDirectory)
        {
           IList<Assembly> assemblies= GetAssemblies(baseDirectory);
           List<Type> types = new List<Type>();
           foreach (Assembly assembly in assemblies)
           {
               types.AddRange(GetImplementdTypes<T>(assembly));
           }

           return types;
        }

        public static IEnumerable<Type> GetImplementdTypes<T>(string assemblyFile)
        {
            if (!File.Exists(assemblyFile))
            {
                return null;
            }
            try
            {
                return GetImplementdTypes<T>(Assembly.LoadFile(assemblyFile));
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static IEnumerable<Type> GetImplementdTypes<T>(Assembly assembly)
        {
            if (assembly == null)
            {
                return null;
            }

            return assembly.GetExportedTypes().Where(p =>
               p.IsSubclassOf(typeof(T)) && (!p.IsAbstract) && (!p.IsInterface));
        }

        public static IList<T> GetImplementedObjectsByDirectory<T>(string baseDirectory)
        {
            IList<Assembly> assemblies = GetAssemblies(baseDirectory);
            List<T> entities = new List<T>();
            foreach (Assembly assembly in assemblies)
            {
                entities.AddRange(GetImplementedObjects<T>(assembly));
            }

            return entities;
        }

        public static IList<T> GetImplementedObjects<T>(string assemblyFile)
        {
            if (!File.Exists(assemblyFile))
            {
                return null;             
            }
            try
            {
                return GetImplementedObjects<T>(Assembly.LoadFile(assemblyFile));
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static IList<T> GetImplementedObjects<T>(Assembly assembly)
        {
            if (assembly == null)
            {
                return null;
            }

            IEnumerable<Type> types = GetImplementdTypes<T>(assembly);
            var result = new List<T>();

            foreach (Type type in types)
            {
                ConstructorInfo constructor = type.GetConstructor(new Type[0]);
                if (constructor == null)
                {
                    continue;
                }

                object instance = Activator.CreateInstance(type);
                if (instance is T)
                {
                    result.Add((T)instance);
                }
            }

            return result;
        }

        public static IList<Assembly> GetAssemblies(string baseDirectory)
        {
            if (!Directory.Exists(baseDirectory))
            {
                return new List<Assembly>();
            }

            string[] files = Directory.GetFiles(baseDirectory, "*.dll");

            return GetAssemblies(files);
        }

        public static IList<Assembly> GetAssemblies(string[] assemblyFiles)
        {
            IList<Assembly> assemblies = new List<Assembly>();
            try
            {
                foreach (string file in assemblyFiles)
                {
                    if (!File.Exists(file)||(!file.EndsWith(".dll",StringComparison.InvariantCultureIgnoreCase)))
                    {
                        continue;
                    }
                    assemblies.Add(Assembly.LoadFile(file));
                }
            }
            catch (Exception ex)
            {
                return new List<Assembly>();
            }

            return assemblies;
        }
    }
}