Exemplo n.º 1
0
        /// <summary>
        /// Helper method allows to retrieve initial assembly and it referenced (static and runtime) assemblies.
        /// </summary>
        static public ListUnique <Assembly> GetReferencedAndInitialAssembly(Assembly initialAssembly)
        {
            ListUnique <Assembly> assemblies = GetReferencedAssemblies(initialAssembly);

            assemblies.Add(initialAssembly);
            return(assemblies);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Helper method allows to retrieve initial assembly referenced (static and runtime) assemblies.
        /// </summary>
        static public ListUnique <Assembly> GetReferencedAssemblies(Assembly initialAssembly)
        {
            ListUnique <Assembly> result = new ListUnique <Assembly>();

            AssemblyName[] names = initialAssembly.GetReferencedAssemblies();
            for (int i = 0; i < names.Length; i++)
            {
                result.Add(Assembly.Load(names[i]));
            }

            lock (_dynamicReferencedAssemblies)
            {
                if (_dynamicReferencedAssemblies.ContainsKey(initialAssembly))
                {
                    result.AddRange(_dynamicReferencedAssemblies[initialAssembly]);
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Will create a control using reflection, corresponding to the object passed it. In order for the control to be recognized
        /// it must take as a constructor paramterer the type passed in.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="allowComponentBaseTypes">This indicates whether the search for corresponding control should also cover parent types of the given type</param>
        /// <returns></returns>
        static public CommonBaseControl CreateCorrespondingControl(object component, bool allowComponentBaseTypes)
        {
            Type componentType = component.GetType();
            ListUnique <Assembly> assemblies = ReflectionHelper.GetReferencedAndInitialAssembly(Assembly.GetEntryAssembly());

            assemblies.Add(Assembly.GetAssembly(componentType));
            List <Type> types = ReflectionHelper.GatherTypeChildrenTypesFromAssemblies(typeof(CommonBaseControl), true, false, assemblies, new Type[] { componentType });

            if (types.Count == 0 && allowComponentBaseTypes)
            {
                while (componentType != typeof(object) && types.Count == 0)
                {
                    componentType = componentType.BaseType;
                    types         = ReflectionHelper.GatherTypeChildrenTypesFromAssemblies(typeof(CommonBaseControl), true, false, assemblies, new Type[] { componentType });
                }
            }

            if (types.Count == 0)
            {// Type not found.
                return(null);
            }

            string typesNames = string.Empty;

            if (types.Count > 1)
            {
                foreach (Type type in types)
                {
                    typesNames += type.Name + "();";
                }

                SystemMonitor.CheckWarning(types.Count == 1, "More than 1 control found for this type [" + component.GetType().Name + "][" + typesNames + "] of component, creating the first one.");
            }

            // Return the first proper object.
            CommonBaseControl control = (CommonBaseControl)types[0].GetConstructor(new Type[] { componentType }).Invoke(new object[] { component });

            control.Tag = component;
            return(control);
        }