コード例 #1
0
        /// <summary>
        /// Load class defined into the string parameters.
        /// Use 'classname' tag to define class and try to pass string parameters to configure it. Class must be IVarFormatter.
        /// </summary>
        /// <returns></returns>
        protected virtual void LoadCustomClass()
        {
            IDictionary <string, string> parameters = GetFormatterParametersData();

            if (parameters == null)
            {
                throw new ArgumentNullException("Class Name");
            }

            if (!parameters.ContainsKey(ClassNameTag))
            {
                throw new ArgumentNullException("Class Name");
            }

            string className = parameters[ClassNameTag];
            Type   tLoad     = AssembliesTypesLoader.GetType(className);

            if (tLoad == null)
            {
                throw new Exception("Class '" + className + "' not found!");
            }

            object objLoad = null;

            try{
                objLoad = Activator.CreateInstance(tLoad, new object[] { FormatterParameters });                 //string constructor first
            }catch (Exception) {
                //if crashes use anonymous constructor..
                try{
                    objLoad = Activator.CreateInstance(tLoad, new object[] {});
                }catch (Exception) {      }
            }

            if (objLoad == null)
            {
                throw new Exception("Object invocation failed!");
            }

            if (!(objLoad is IVarFormatter))
            {
                throw new InvalidCastException("Object isn't IVarFormatter");
            }

            this._customFormatter = (IVarFormatter)objLoad;
        }
コード例 #2
0
        //20130406 :: mellorasinxelas
        /// <summary>
        /// Build programmer custom element.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="fontAttrs"></param>
        /// <returns></returns>
        public DrawElement BuildCustomElement(XmlNode node, XmlAttributeCollection fontAttrs)
        {
            //load object name
            string classname = XmlHelper.GetAttributeValue(DefaultCustomElement.ClassNameAttributeCustomElement, node.Attributes, null);

            if (classname == null)
            {
                throw new NullReferenceException("Custom elements needs Class Name!");
            }

            //load VARS

            List <Variable> vars = new List <Variable>();

            XmlNodeList varNodes = node.SelectNodes(VarElementConstant);

            foreach (XmlNode varNode in varNodes)
            {
                string name = XmlHelper.GetAttributeValue(Variable.NameAttributeConstant, varNode.Attributes, "");

                if (name != string.Empty)
                {
                    //load formatter
                    DefaultFormat?formatter       = null;
                    string        formatterParams = null;
                    formatter = BasicVarFormatter.Parse(XmlHelper.GetAttributeValue(FormatableVariable.FormatterAttributeConstant, varNode.Attributes, null));
                    if (formatter != null)
                    {
                        formatterParams = XmlHelper.GetAttributeValue(FormatableVariable.FormatterParametersAttributeConstant, varNode.Attributes, null);
                    }

                    if (UseOptionalTags)
                    {
                        bool optional = XmlHelper.GetAttributeBoolean(Variable.OptionalAttributeConstant, varNode.Attributes, false);
                        if (formatter == null)
                        {
                            vars.Add(new Variable(name, optional));
                        }
                        else
                        {
                            vars.Add(new FormatableVariable(name, optional, formatter.Value, formatterParams));
                        }
                    }
                    else
                    {
                        if (formatter == null)
                        {
                            vars.Add(new Variable(name));
                        }
                        else
                        {
                            vars.Add(new FormatableVariable(name, formatter.Value, formatterParams));
                        }
                    }
                }
            }

            //load object

            Type tLoad = null;

            try
            {
                tLoad = AssembliesTypesLoader.GetType(classname);
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
            }

            if (tLoad == null)
            {
                throw new Exception("Custom element '" + classname + "' type don't found!");
            }

            object objLoad = null;

            try
            {
                objLoad = Activator.CreateInstance(tLoad, new object[] { fontAttrs, node.Attributes, vars });
            }
            catch (Exception ex2)
            {
                try
                {
                    objLoad = Activator.CreateInstance(tLoad, new object[] { fontAttrs, node.Attributes });
                }
                catch (Exception ex3)
                {
                    Console.WriteLine("Custom element must have (fontAttr, textAttr) builder or (fontAttr, textAttr, List<Variables>)");
                    Console.WriteLine(ex2.ToString());
                    Console.WriteLine(ex3.ToString());
                }
            }
            if (objLoad == null)
            {
                throw new NullReferenceException("Object '" + classname + "' don't loaded!");
            }
            if (!(objLoad is DrawElement))
            {
                throw new Exception("Object '" + classname + "' type doesn't match!. DrawElement is required!");
            }

            return((DrawElement)objLoad);
        }