Exemplo n.º 1
0
        /// <summary>
        /// Creates an instance of the specified type.
        /// </summary>
        /// <typeparam name="T">The type to create.</typeparam>
        /// <param name="assembly">The assmblies to search.</param>
        /// <param name="preferences">Preferences to use in selecting a type to create.</param>
        public static T Create <T>(Assembly assembly, TypePreferences preferences) where T : class
        {
            Type[] types;
            try {
                types = assembly.GetTypes();
            } catch {
                try {
                    types = assembly.GetExportedTypes();
                } catch {
                    types = Array.Empty <Type>();
                }
            }

            if (!types.Any())
            {
                IgnoreAssembly(assembly);
                return(null);
            }

            var feedback = CreateWithFeedback <T>(types, preferences);

            if (feedback.AttributesExisted)
            {
                return(feedback.Instance);
            }
            IgnoreAssembly(assembly);
            return(null);
        }
Exemplo n.º 2
0
        private static TypeCreationFeedback <T> CreateWithFeedback <T>(Type[] types, TypePreferences preferences) where T : class
        {
            bool attributesExist = false;

            foreach (Type t in types)
            {
                var definitions = t.GetCustomAttributes <ClassDefinitionAttribute>();
                if (definitions != null && definitions.Any())
                {
                    attributesExist = true;
                    foreach (ClassDefinitionAttribute definition in definitions)
                    {
                        if (typeof(T).IsAssignableFrom(t) && string.Compare(preferences?.Code, definition.Code, StringComparison.CurrentCultureIgnoreCase) == 0)
                        {
                            T rtn = CreateFromType <T>(t, preferences);
                            if (rtn != null)
                            {
                                return(new TypeCreationFeedback <T>()
                                {
                                    Instance = rtn,
                                    AttributesExisted = true
                                });
                            }
                        }
                    }
                }
            }

            return(new TypeCreationFeedback <T>()
            {
                Instance = null,
                AttributesExisted = attributesExist
            });
        }
Exemplo n.º 3
0
        private static T CreateFromType <T>(Type type, TypePreferences preferences) where T : class
        {
            var c = type.GetConstructor(Array.Empty <Type>());

            if (c == null)
            {
                return(null);
            }

            int expectedAssignments = preferences?.Properties?.Count ?? 0;
            List <Tuple <PropertyInfo, object> > assignments = null;

            if (preferences?.Properties != null)
            {
                var propertyNames = preferences.Properties.Keys;
                foreach (string propertyName in propertyNames)
                {
                    var property = type.GetProperty(propertyName);
                    if (property != null && PropertyAllowsValue(property, preferences.Properties[propertyName]))
                    {
                        if (assignments == null)
                        {
                            assignments = new List <Tuple <PropertyInfo, object> >();
                        }
                        assignments.Add(new Tuple <PropertyInfo, object>(property, preferences.Properties[propertyName]));
                    }
                }
            }

            if (expectedAssignments != (assignments?.Count ?? 0))
            {
                return(null);
            }

            T rtn = c.Invoke(Array.Empty <object>()) as T;

            if (assignments != null)
            {
                foreach (Tuple <PropertyInfo, object> assingment in assignments)
                {
                    assingment.Item1.SetMethod.Invoke(rtn, new[] { assingment.Item2 });
                }
            }

            return(rtn);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates an instance of the specified type.
        /// </summary>
        /// <typeparam name="T">The type to create.</typeparam>
        /// <param name="preferences">Preferences to use in selecting a type to create.</param>
        public static T Create <T>(TypePreferences preferences) where T : class
        {
            T rtn = null;

            foreach (Assembly assembly in from a in AssemblyCache.ToArray() select a)
            {
                rtn = Create <T>(assembly, preferences);
                if (rtn != null)
                {
                    return(rtn);
                }
            }

            if (preferences?.SearchPaths != null && preferences.SearchPaths.Count > 0)
            {
                var loadedFileNames = from a in AssemblyCache.ToArray() where !a.IsDynamic select a.Location;
                foreach (string path in from p in preferences.SearchPaths.Distinct() where !string.IsNullOrWhiteSpace(p) select p)
                {
                    var fileNames = from n in Directory.GetFiles(path, "*.dll") where !loadedFileNames.Contains(n) select n;
                    if (fileNames != null)
                    {
                        foreach (string fileName in fileNames)
                        {
                            var unknownAssembly = Assembly.LoadFile(fileName);
                            rtn = Create <T>(unknownAssembly, preferences);
                            if (rtn != null)
                            {
                                CacheAssembly(unknownAssembly);
                                return(rtn);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates an instance of the specified type.
 /// </summary>
 /// <typeparam name="T">The type to create.</typeparam>
 /// <param name="types">The types to search.</param>
 /// <param name="preferences">Preferences to use in selecting a type to create.</param>
 public static T Create <T>(Type[] types, TypePreferences preferences) where T : class
 {
     return(CreateWithFeedback <T>(types, preferences).Instance);
 }