schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Linq;
using System.Reflection;
 
namespace Sodao.FastSocket.SocketBase.Utils
{
    /// <summary>
    /// 反射帮助类。
    /// </summary>
    public static class ReflectionHelper
    {
        /// <summary>
        /// 获取实现了指定类口类型的基类实例。
        /// </summary>
        /// <typeparam name="T">接口类型</typeparam>
        /// <param name="assembly">指定的程序集</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">assembly is null</exception>
        static public T[] GetImplementObjects<T>(Assembly assembly)
        {
            if (assembly == null) throw new ArgumentNullException("assembly");
 
            return assembly.GetExportedTypes().Where(c =>
            {
                if (c.IsClass && !c.IsAbstract)
                {
                    var interfaces = c.GetInterfaces();
                    if (interfaces != null) return interfaces.Contains(typeof(T));
                }
                return false;
            }).Select(c => (T)c.GetConstructor(new Type[0]).Invoke(new object[0])).ToArray();
        }
    }
}