コード例 #1
0
 public void ShouldCorrectlyDetermineIfObjectIsOfTypeType() //this is not a typo!
 {
     XAssert.All(assert =>
     {
         assert.False(TypeOfType.Is <object>());
         assert.False(TypeOfType.Is <int>());
         assert.True(TypeOfType.Is <Type>());
     });
 }
コード例 #2
0
 public void ShouldCorrectlyDetermineIfObjectIsOfTypeType() //this is not a typo!
 {
     Assert.Multiple(() =>
     {
         Assert.False(TypeOfType.Is <object>());
         Assert.False(TypeOfType.Is <int>());
         Assert.True(TypeOfType.Is <Type>());
     });
 }
コード例 #3
0
        /// <summary>
        /// 根据类型所实现的接口,以及类型的类型,来获得本Dll下所有符合条件的Type。
        /// </summary>
        /// <param name="interfaceType">接口类型。</param>
        /// <param name="type">类型的类型。</param>
        /// <returns>Type集合。</returns>
        public static List <Type> GetTypesByInterface(Type interfaceType, TypeOfType type)
        {
            List <Type> typeList = new List <Type>();

            switch (type)
            {
            case TypeOfType.Interface:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsInterface)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Class:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsClass)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Enum:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsEnum)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }
            }
            return(typeList);
        }
コード例 #4
0
        /// <summary>
        /// 根据类型所实现的父类,以及类型的类型,来获得本Dll下所有符合条件的Type。
        /// </summary>
        /// <param name="interfaceType">父类类型。</param>
        /// <param name="type">类型的类型。</param>
        /// <returns>Type集合。</returns>
        public static List <Type> GetTypesByBaseClass(Type baseClassType, TypeOfType type)
        {
            List <Type> typeList = new List <Type>();

            switch (type)
            {
            case TypeOfType.Interface:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(baseClassType) && types[j].IsInterface)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Class:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(baseClassType) && types[j].IsClass)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Enum:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(baseClassType) && types[j].IsEnum)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }
            }
            return(typeList);
        }
コード例 #5
0
        /// <summary>
        /// 根据Dll名称,以及类型所实现的接口,来获得所有符合条件的所有Type。
        /// </summary>
        /// <param name="dllName">Dll名称。</param>
        /// <param name="interfaceType">接口类型。</param>
        /// <param name="type">类型的类型。</param>
        /// <returns>Type集合。</returns>
        public static List <Type> GetTypes(string dllName, Type interfaceType, TypeOfType type)
        {
            switch (type)
            {
            case TypeOfType.Interface:
            {
                try
                {
                    System.Reflection.Assembly assembly;
                    List <Type> listType = new List <Type>();
                    assembly = System.Reflection.Assembly.LoadFrom(dllName);
                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsInterface)
                        {
                            listType.Add(types[j]);
                        }
                    }
                    return(listType);
                }
                catch
                {
                    return(null);
                }
            }

            case TypeOfType.Class:
            {
                try
                {
                    System.Reflection.Assembly assembly;
                    List <Type> listType = new List <Type>();
                    assembly = System.Reflection.Assembly.LoadFrom(dllName);
                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsClass)
                        {
                            listType.Add(types[j]);
                        }
                    }
                    return(listType);
                }
                catch
                {
                    return(null);
                }
            }

            case TypeOfType.Enum:
            {
                try
                {
                    System.Reflection.Assembly assembly;
                    List <Type> listType = new List <Type>();
                    assembly = System.Reflection.Assembly.LoadFrom(dllName);
                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsEnum)
                        {
                            listType.Add(types[j]);
                        }
                    }
                    return(listType);
                }
                catch
                {
                    return(null);
                }
            }
            }
            return(null);
        }
コード例 #6
0
 public bool Applies()
 {
     return
         (TypeOfType.Is <T>() ||
          typeof(T) == typeof(MethodInfo));
 }
コード例 #7
0
        /// <summary>
        /// 根据类型所实现的父类,以及类型的类型,来获得本Dll下所有符合条件的Type。
        /// </summary>
        /// <param name="dllName">Dll名称。</param>
        /// <param name="interfaceType">父类类型。</param>
        /// <param name="type">类型的类型。</param>
        /// <returns>Type集合。</returns>
        public static List <Type> GetTypesByBaseClass(string dllName, Type baseClassType, TypeOfType type)
        {
            Assembly assembly;

            assembly = Assembly.LoadFrom(dllName);
            Type[]      types    = assembly.GetTypes();
            List <Type> typeList = new List <Type>();

            switch (type)
            {
            case TypeOfType.Interface:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(baseClassType) && types[j].IsInterface)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Class:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(baseClassType) && types[j].IsClass)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Enum:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(baseClassType) && types[j].IsEnum)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }
            }
            return(typeList);
        }
コード例 #8
0
        /// <summary>
        /// 根据Dll名称,以及类型所实现的接口,来获得所有符合条件的Type(除抽象类外)。
        /// </summary>
        /// <param name="dllName">Dll名称。</param>
        /// <param name="interfaceType">接口类型。</param>
        /// <param name="type">类型的类型。</param>
        /// <returns>Type集合。</returns>
        public static List <Type> GetTypesByInterfaceWithOutAbstract(string dllName, Type interfaceType, TypeOfType type)
        {
            Assembly assembly;

            assembly = Assembly.LoadFrom(dllName);
            Type[]      types    = assembly.GetTypes();
            List <Type> typeList = new List <Type>();

            switch (type)
            {
            case TypeOfType.Interface:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsInterface && !types[j].IsAbstract)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Class:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsClass && !types[j].IsAbstract)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }

            case TypeOfType.Enum:
            {
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].GetInterface(interfaceType.Name) != null && types[j].IsEnum && !types[j].IsAbstract)
                    {
                        typeList.Add(types[j]);
                    }
                }
                break;
            }
            }
            return(typeList);
        }
コード例 #9
0
 public bool AppliesTo(Type type)
 {
     return
         (TypeOfType.Is(type) ||
          type == typeof(MethodInfo));
 }