Ejemplo n.º 1
0
        /// <summary>
        /// Creates a field instance from his XML schema representation, taking the field structure from a schema and the constraints from another.
        /// </summary>
        /// <param name="schema">
        /// A <see cref="XmlSchemaSet"/> which contains the whole form to render
        /// </param>
        /// <param name="elm">
        /// A <see cref="XmlSchemaElement"/> which contains the definition of the field to instantiate
        /// </param>
        /// <returns>
        /// A <see cref="Field"/> built following the schemas above
        /// </returns>
        public static IField GetInstance(XmlSchemaSet schema, XmlSchemaObject scobj)
        {
            if (scobj is XmlSchemaElement && !((XmlSchemaElement)scobj).SchemaTypeName.IsEmpty)
            {
                XmlSchemaElement elm = (XmlSchemaElement)scobj;
                try
                {
                    foreach (XmlSchema xsd in schema.Schemas())
                    {
                        string searchname = elm.SchemaTypeName.ToString();
                        //controlla se nello schema è presente l'elemento cercato
                        if (xsd.SchemaTypes.Contains(new XmlQualifiedName(searchname)))
                        {
                            foreach (XmlSchemaObject obj in xsd.Items)
                            {
                                if (obj is XmlSchemaComplexType && ((XmlSchemaComplexType)obj).Name.ToString() == searchname)
                                {
                                    XmlSchema fieldschema = new XmlSchema();
                                    fieldschema.Items.Add(obj);
                                    XmlSchemaComplexContent          xcc  = (XmlSchemaComplexContent)((XmlSchemaComplexType)obj).ContentModel;
                                    XmlSchemaComplexContentExtension xext = (XmlSchemaComplexContentExtension)xcc.Content;

                                    Type   tipo = Type.GetType("Fields." + xext.BaseTypeName.ToString(), true);
                                    Type[] par  = { fieldschema.GetType(), typeof(string) };
                                    //elm.Name prende il nome dell'elemento contenuto in elm
                                    Object[] vals = { fieldschema, elm.Name };
                                    return((IField)tipo.GetConstructor(par).Invoke(vals));
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    return(null);
                }
                return(null);
            }
            return(null);
        }