Exemplo n.º 1
0
        protected void CheckEnhancedTypeConsistency(Type type)
        {
            IdentityHashSet <MethodInfo> allMethods = new IdentityHashSet <MethodInfo>();

            foreach (Type interf in type.GetInterfaces())
            {
                allMethods.AddAll(interf.GetMethods());
            }
            Type currType = type;

            while (currType != typeof(Object) && currType != null)
            {
                allMethods.AddAll(currType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly));
                currType = currType.BaseType;
            }
            if (allMethods.Count == 0)
            {
                throw new Exception("Type invalid (not a single method): " + type);
            }
            if (type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).Length == 0)
            {
                throw new Exception("Type invalid (not a single constructor): " + type);
            }
            if (!type.IsAbstract)
            {
                foreach (MethodInfo method in allMethods)
                {
                    MethodInfo method2 = ReflectUtil.GetDeclaredMethod(true, type, method.ReturnType, method.Name, TypeUtil.GetParameterTypesToTypes(method.GetParameters()));
                    if (method2 == null || method2.IsAbstract)
                    {
                        throw new Exception("Type is not abstract but has at least one abstract method: " + method);
                    }
                }
            }
            Type[] interfaces = type.GetInterfaces();
            foreach (Type interf in interfaces)
            {
                MethodInfo[] interfaceMethods = ReflectUtil.GetDeclaredMethods(interf);
                foreach (MethodInfo interfaceMethod in interfaceMethods)
                {
                    try
                    {
                        if (type.GetMethod(interfaceMethod.Name, TypeUtil.GetParameterTypesToTypes(interfaceMethod.GetParameters())) == null)
                        {
                            throw new Exception("Type is not abstract but has at least one abstract method: " + interfaceMethod);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Type is not abstract but has at least one abstract method: " + interfaceMethod, e);
                    }
                }
            }
        }