Exemplo n.º 1
0
        /// <summary>
        ///     Initializes the class.
        /// </summary>
        /// <param name="type">The type.</param>
        public static void InitializeClass(Type type)
        {
            Console.WriteLine(
                type.GetMembers(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Count());
            foreach (var member in GetFieldsAndProperties(type))
            {
                var valueType = member.GetMemberType();
                var import    = member.GetCustomAttribute <ResourceImportAttribute>();
                var value     = JsonFactory.JsonResource(import.File, valueType);
                if (import.Filter != null)
                {
                    if (
                        !import.Filter.GetInterfaces()
                        .Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IFilter <>)))
                    {
                        throw new Exception($"{nameof(import.Filter)} does not implement {nameof(IFilter)}");
                    }

                    var filterInstance = Activator.CreateInstance(import.Filter);
                    var apply          = import.Filter.GetMethod("Apply", BindingFlags.Public | BindingFlags.Instance);

                    value = apply.Invoke(filterInstance, new[] { value });
                }

                member.SetValue(null, value);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes this instance.
        /// </summary>
        /// <exception cref="Exception"></exception>
        public static void Initialize()
        {
            var importClasses =
                typeof(ResourceLoader).Assembly.DefinedTypes.Where(
                    t => t.IsClass && t.IsDefined(typeof(ResourceImportAttribute), false));

            var tasks = new List <Task>();

            foreach (var c in importClasses)
            {
                Parallel.ForEach(
                    GetFieldsAndProperties(c),
                    member =>
                {
                    tasks.Add(
                        Task.Factory.StartNew(
                            () =>
                    {
                        var valueType = member.GetMemberType();
                        var import    = member.GetCustomAttribute <ResourceImportAttribute>();
                        var value     = JsonFactory.JsonResource(import.File, valueType);

                        if (import.Filter != null)
                        {
                            if (
                                !import.Filter.GetInterfaces()
                                .Any(
                                    x =>
                                    x.IsGenericType &&
                                    x.GetGenericTypeDefinition() == typeof(IFilter <>)))
                            {
                                throw new Exception(
                                    $"{nameof(import.Filter)} does not implement {nameof(IFilter)}");
                            }

                            var filterInstance = Activator.CreateInstance(import.Filter);
                            var apply          = import.Filter.GetMethod(
                                "Apply",
                                BindingFlags.Public | BindingFlags.Instance);

                            value = apply.Invoke(filterInstance, new[] { value });
                        }

                        member.SetValue(null, value);
                    }));
                });

                Data.Cache[c] = (IDataType)Activator.CreateInstance(c, true);
            }

            Task.WaitAll(tasks.ToArray());
        }