Пример #1
0
        static void Main(string[] args)
        {
            using (SourceCode.Forms.Management.FormsManager manager = new FormsManager("localhost", 5555))
            {
                ControlTypeExplorer explorer = manager.GetControlTypes();

                //REMOVE ALL CONTROL TYPES THAT ARE NOT IN THE SOURCECODE NAMESPACE!
                //foreach (ControlTypeInfo ctype in explorer.ControlTypes)
                //{

                //    if (!ctype.FullName.StartsWith("SourceCode"))
                //    {
                //        try
                //        {
                //            manager.DeleteControlType(ctype.Name);
                //            Console.WriteLine("Removed {0}", ctype.FullName);
                //        }
                //        catch (Exception ex)
                //        {
                //            Console.WriteLine("{0} not removed. In use? Exception: {1}", ctype.FullName, ex.Message);
                //        }
                //    }
                //}
                //return;

                Assembly controlAssembly = Assembly.Load("K2Field.Smartforms.Controls");
                Type[]   types           = controlAssembly.GetTypes();
                foreach (Type type in types)
                {
                    RegisterControlType[] custAttr = type.GetCustomAttributes(typeof(RegisterControlType), true) as RegisterControlType[];
                    if (custAttr.Length == 1)
                    {
                        RegisterControlType regContType = custAttr[0];
                        Console.WriteLine("Discovered {0}", type.FullName);

                        /*
                         *
                         * object controlObj = t.Assembly.CreateInstance(t.FullName);
                         *
                         * PropertyInfo propInfo = t.GetProperty("Name");
                         * string controlName = propInfo.GetValue(controlObj, null) as string;
                         * propInfo = t.GetProperty("FriendlyName");
                         * string friendlyName = propInfo.GetValue(controlObj, null) as string;
                         * if (string.IsNullOrEmpty(controlName) || string.IsNullOrEmpty(friendlyName))
                         * {
                         *  Console.WriteLine("ERROR: Not installing. Name or FriendlyName not set!");
                         *  continue;
                         * }*/



                        bool            updateControl = true;
                        ControlTypeInfo controlType   = explorer.ControlTypes.SingleOrDefault(s => s.Name == type.Name);
                        if (controlType == null)
                        {
                            updateControl = false;
                            controlType   = new ControlTypeInfo();
                        }

                        //Set the control type information
                        controlType.Name        = type.Name;
                        controlType.FullName    = type.FullName + ", " + controlAssembly.FullName;
                        controlType.DisplayName = regContType.DisplayName;
                        controlType.Category    = regContType.Category;
                        controlType.Group       = regContType.Group;
                        controlType.Properties  = regContType.GetPropertyXML();

                        // Specify runtime functions and validation
                        if (!string.IsNullOrEmpty(regContType.GetValueMethod))
                        {
                            controlType.GetValueMethod = regContType.GetValueMethod;
                        }

                        if (!string.IsNullOrEmpty(regContType.SetValueMethod))
                        {
                            controlType.SetValueMethod = regContType.SetValueMethod;
                        }

                        if (!string.IsNullOrEmpty(regContType.SetPropertyMethod))
                        {
                            controlType.SetPropertyMethod = regContType.SetPropertyMethod;
                        }

                        if (!string.IsNullOrEmpty(regContType.GetPropertyMethod))
                        {
                            controlType.GetPropertyMethod = regContType.GetPropertyMethod;
                        }

                        if (!string.IsNullOrEmpty(regContType.GetDefaultValueMethod))
                        {
                            controlType.GetDefaultValueMethod = regContType.GetDefaultValueMethod;
                        }

                        if (!string.IsNullOrEmpty(regContType.ValidationMethod))
                        {
                            controlType.ValidationMethod = regContType.ValidationMethod;
                        }

                        if (!string.IsNullOrEmpty(regContType.SetItemsMethod))
                        {
                            controlType.SetItemsMethod = regContType.SetItemsMethod;
                        }


                        // DataTypes
                        controlType.DataTypes.Clear();
                        RegisterDataType[] dataTypes = type.GetCustomAttributes(typeof(RegisterDataType), true) as RegisterDataType[];
                        foreach (RegisterDataType dataType in dataTypes)
                        {
                            Console.WriteLine("\tAdding datatype: {0}", dataType.DataType.ToString());
                            controlType.DataTypes.Add(dataType.DataType);
                        }

                        //Events
                        controlType.Events.Clear();
                        bool            defaultEventSet = false;
                        RegisterEvent[] events          = type.GetCustomAttributes(typeof(RegisterEvent), true) as RegisterEvent[];
                        foreach (RegisterEvent evt in events)
                        {
                            Console.WriteLine("\tAdding event: {0}", evt.EventName);
                            controlType.Events.Add(evt.EventName);
                            if (evt.IsDefaultEvent && !defaultEventSet)
                            {
                                controlType.DefaultEvent = evt.EventName;
                                defaultEventSet          = true;
                            }
                        }
                        if (!defaultEventSet && controlType.Events.Count > 0)
                        {
                            controlType.DefaultEvent = controlType.Events[0];
                        }



                        if (updateControl)
                        {
                            Console.WriteLine("Updating....");
                            manager.SaveControlType(controlType);
                        }
                        else
                        {
                            Console.WriteLine("Installing....");
                            manager.CreateControlType(controlType);
                        }
                        Console.WriteLine();
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            using (SourceCode.Forms.Management.FormsManager manager = new FormsManager("localhost", 5555))
            {
                ControlTypeExplorer explorer = manager.GetControlTypes();
                
                //REMOVE ALL CONTROL TYPES THAT ARE NOT IN THE SOURCECODE NAMESPACE! 
                //foreach (ControlTypeInfo ctype in explorer.ControlTypes)
                //{

                //    if (!ctype.FullName.StartsWith("SourceCode"))
                //    {
                //        try
                //        {
                //            manager.DeleteControlType(ctype.Name);
                //            Console.WriteLine("Removed {0}", ctype.FullName);
                //        }
                //        catch (Exception ex)
                //        {
                //            Console.WriteLine("{0} not removed. In use? Exception: {1}", ctype.FullName, ex.Message);
                //        }
                //    }
                //}
                //return;

                Assembly controlAssembly = Assembly.Load("K2Field.Smartforms.Controls");
                Type[] types = controlAssembly.GetTypes();
                foreach (Type type in types)
                {
                    RegisterControlType[] custAttr = type.GetCustomAttributes(typeof(RegisterControlType), true) as RegisterControlType[];
                    if (custAttr.Length == 1)
                    {
                        RegisterControlType regContType = custAttr[0];
                        Console.WriteLine("Discovered {0}", type.FullName);
                        /*
                         * 
                        object controlObj = t.Assembly.CreateInstance(t.FullName);
                       
                        PropertyInfo propInfo = t.GetProperty("Name");
                        string controlName = propInfo.GetValue(controlObj, null) as string;
                        propInfo = t.GetProperty("FriendlyName");
                        string friendlyName = propInfo.GetValue(controlObj, null) as string;
                        if (string.IsNullOrEmpty(controlName) || string.IsNullOrEmpty(friendlyName))
                        {
                            Console.WriteLine("ERROR: Not installing. Name or FriendlyName not set!");
                            continue;
                        }*/

                        

                        bool updateControl = true;
                        ControlTypeInfo controlType = explorer.ControlTypes.SingleOrDefault(s => s.Name == type.Name);
                        if (controlType == null)
                        {
                            updateControl = false;
                            controlType = new ControlTypeInfo();
                        }

                        //Set the control type information
                        controlType.Name = type.Name;
                        controlType.FullName = type.FullName + ", " + controlAssembly.FullName;
                        controlType.DisplayName = regContType.DisplayName;
                        controlType.Category = regContType.Category;
                        controlType.Group = regContType.Group;
                        controlType.Properties = regContType.GetPropertyXML();

                        // Specify runtime functions and validation
                        if (!string.IsNullOrEmpty(regContType.GetValueMethod))
                            controlType.GetValueMethod = regContType.GetValueMethod;

                        if (!string.IsNullOrEmpty(regContType.SetValueMethod))
                            controlType.SetValueMethod = regContType.SetValueMethod;

                        if (!string.IsNullOrEmpty(regContType.SetPropertyMethod) )
                            controlType.SetPropertyMethod = regContType.SetPropertyMethod;

                        if (!string.IsNullOrEmpty(regContType.GetPropertyMethod))
                            controlType.GetPropertyMethod = regContType.GetPropertyMethod;

                        if (!string.IsNullOrEmpty(regContType.GetDefaultValueMethod))
                            controlType.GetDefaultValueMethod = regContType.GetDefaultValueMethod;

                        if (!string.IsNullOrEmpty(regContType.ValidationMethod))
                            controlType.ValidationMethod = regContType.ValidationMethod;

                        if (!string.IsNullOrEmpty(regContType.SetItemsMethod))
                            controlType.SetItemsMethod = regContType.SetItemsMethod;


                        // DataTypes
                        controlType.DataTypes.Clear();
                        RegisterDataType[] dataTypes = type.GetCustomAttributes(typeof(RegisterDataType), true) as RegisterDataType[];
                        foreach (RegisterDataType dataType in dataTypes)
                        {
                            Console.WriteLine("\tAdding datatype: {0}", dataType.DataType.ToString());
                            controlType.DataTypes.Add(dataType.DataType);
                        }

                        //Events
                        controlType.Events.Clear();
                        bool defaultEventSet = false;
                        RegisterEvent[] events = type.GetCustomAttributes(typeof(RegisterEvent), true) as RegisterEvent[];
                        foreach (RegisterEvent evt in events)
                        {
                            Console.WriteLine("\tAdding event: {0}", evt.EventName);
                            controlType.Events.Add(evt.EventName);
                            if (evt.IsDefaultEvent && !defaultEventSet)
                            {
                                controlType.DefaultEvent = evt.EventName;
                                defaultEventSet = true;
                            }
                        }
                        if (!defaultEventSet && controlType.Events.Count > 0)
                        {
                            controlType.DefaultEvent = controlType.Events[0];
                        }


            
                        if (updateControl)
                        {
                            Console.WriteLine("Updating....");
                            manager.SaveControlType(controlType);
                        }
                        else
                        {
                            Console.WriteLine("Installing....");
                            manager.CreateControlType(controlType);
                        }
                        Console.WriteLine();

                    }
                }
            }
        }