Пример #1
0
        // (Integer, "3") return object of new Integer(3)
        public object Parse(Type c, string s)
        {
            Type d = ReflectionUtilities.BoxClass(c);

            foreach (Type e in ReflectionUtilities.ClassAndAncestors(d))
            {
                // get all the super classes of Integer for example
                string name = ReflectionUtilities.GetAssemblyQualifiedName(e);
                if (parsers.ContainsKey(name))
                {
                    object ret = Parse(name, s);

                    // check if ret can be cast as c
                    if (c.IsAssignableFrom(ret.GetType()))
                    {
                        return(ret);
                    }
                    else
                    {
                        var ex = new InvalidCastException("Cannot cast from " + ret.GetType() + " to " + c);
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                    }
                }
            }
            return(Parse(d.Name, s));
        }
Пример #2
0
        // private static void assertIsSubclassOf(Class<?> named_parameter, Class<?> default_class, Type argClass) {
        private static void AssertIsSubclassOf(Type namedparameter, Type defaultclass, Type argClass)
        {
            bool   isSubclass   = false;
            string argClassName = ReflectionUtilities.GetAssemblyQualifiedName(argClass);

            foreach (Type t in ReflectionUtilities.ClassAndAncestors(defaultclass))
            {
                if (argClassName.Equals(ReflectionUtilities.GetAssemblyQualifiedName(t)))
                {
                    isSubclass = true;
                }
            }
            if (!isSubclass)
            {
                var e = new ClassHierarchyException(namedparameter + " defines a default class "
                                                    + ReflectionUtilities.GetName(defaultclass) + " with a type that does not extend of its target's type " + argClass);
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }
        }
Пример #3
0
        public object Parse(Type c, String s)
        {
            Type d = ReflectionUtilities.BoxClass(c);

            foreach (Type e in ReflectionUtilities.ClassAndAncestors(d))
            {
                string name = e.FullName;
                if (parsers.ContainsKey(name))
                {
                    object ret = Parse(name, s);
                    if (c.IsAssignableFrom(ret.GetType()))
                    {
                        return(ret);
                    }
                    else
                    {
                        throw new InvalidCastException("Cannot cast from " + ret.GetType() + " to " + c);
                    }
                }
            }
            return(Parse(d.Name, s));
        }