コード例 #1
0
        /// <summary>
        /// Loads a object from a JSON-formatted file.
        /// </summary>
        /// <param name="type">Type to return.</param>
        /// <returns>
        /// Returns an object containing the data from the file.
        /// If the file doesn't exist an empty instance of the object is returned.
        /// </returns>
        public object Load(Type type)
        {
            if (!File.Exists(_configurationFilePath))
            {
                return(Activator.CreateInstance(type.GetCType()));
            }

            var json = ReadFile();

            return(JsonConvert.DeserializeObject(json, type, JsonSerializerSettings));
        }
コード例 #2
0
        public Dictionary <string, T> LoadAssemblies <T>(string filespec, string pluginFolder)
        {
            this.filespec     = filespec;
            this.pluginFolder = pluginFolder;

            var types = GetTypesFromPluginAssemblies().FindAll(delegate(CType t) {
                var interfaceTypes = new List <CType>(t.GetInterfaces());
                return(interfaceTypes.Contains(typeof(T)));
            });
            var loadedAssemblies = types.Select(t => (T)Activator.CreateInstance(t)).ToList();

            return(loadedAssemblies.ToDictionary(a => a.GetType().Name, a => a));
        }
コード例 #3
0
        /// <summary>
        /// Load a section of a JSON-Formatted file
        /// </summary>
        /// <param name="type">Type to return</param>
        /// <param name="sectionName">Property to return</param>
        /// <returns>
        /// Returns on object containing the requested datat.
        /// If the file doesn't exist, doesn't contain any data, or doesn't contain the requested section, an empty object is returned
        /// </returns>
        public object LoadSection(Type type, string sectionName)
        {
            if (!File.Exists(_configurationFilePath) || new FileInfo(_configurationFilePath).Length == 0)
            {
                return(Activator.CreateInstance(type.GetCType()));
            }

            var json = ReadFile();

            var settingsSection = GetSectionJToken(json, sectionName);

            return(settingsSection == null
                ? Activator.CreateInstance(type.GetCType())
                : JsonConvert.DeserializeObject(settingsSection.ToString(), type, JsonSerializerSettings));
        }