Exemplo n.º 1
0
        /// <summary>
        /// Returns the property value in a fresh instance of the class or null
        /// if it doesn't exists
        /// </summary>
        /// <typeparam name="T">Type of the property</typeparam>
        /// <param name="type">Type to get the property from</param>
        /// <param name="propName">Property name</param>
        private static T GetPropertyValueOrDefault <T> (Type type, String propName, MangaConnector inst) where T : class
        {
            // Attempts to get the property descriptor
            var prop = type.GetProperty(propName);

            if (prop == null)
            {
                return(null); // Null for classes and idk what for other types
            }
            // Gets the value and attempts to convert it to T (as returns null
            // upon failure)
            var value = prop.GetValue(inst);

            return(value as T);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks a connector for exposed properties and methods, and throws an
        /// exception when one of them is missing
        /// </summary>
        /// <param name="connType">Connector's <see cref="Type" /></param>
        private static void ValidateConnector(Type connType, MangaConnector inst)
        {
            // Properties being checked
            var properties = new[]
            {
                "ID",
                "Website"
            };

            // Checks each of them
            foreach (var property in properties)
            {
                var propVal = GetPropertyValueOrDefault <String> (connType, property, inst);
                if (propVal == null)
                {
                    throw new Exception($"Connector is missing the \"{property}\" property or value is null.");
                }
            }

            // Methods being checked
            var methods = new[]
            {
                "InitHTTP",
                //"DownloadChapterAsync", // -> moved to base class
                "UpdateMangaListAsync",
                "GetChaptersAsync",
                "GetPageLinksAsync",
                "GetImageLinkAsync"
            };

            // Checks each of them
            foreach (var method in methods)
            {
                if (!MethodExists(connType, method))
                {
                    throw new Exception($"Connector is missing the \"{method}\" method.");
                }
            }
        }