public virtual void OnReadFromXmlNode(IXmlCodeReader serializer, XmlNode node)
        {
            object v;

            _name            = XmlSerialization.GetAttribute(node, XmlSerialization.XMLATT_NAME);
            _static          = XmlSerialization.GetAttributeBool(node, XmlSerialization.XMLATT_STATIC);
            MakeUIThreadSafe = XmlSerialization.GetAttributeBool(node, XmlSerialization.XMLATT_UIThreadSafe);
            if (XmlSerialization.ReadValueFromChildNode(node, XmlSerialization.XML_DESCRIPT, out v))
            {
                _desc = (string)v;
            }
            XmlNode nd = node.SelectSingleNode(XmlSerialization.XML_RETURNTYPE);

            if (nd != null)
            {
                _returnType = (RaisDataType)XmlSerialization.ReadFromXmlNode(serializer, nd);
            }
            else
            {
                _returnType = new RaisDataType(typeof(double));
            }
            string      qry   = XmlSerialization.FormatString("{0}/{1}", XmlSerialization.XML_PARAMLIST, XmlSerialization.XML_PARAM);
            XmlNodeList nodes = node.SelectNodes(qry);

            if (nodes != null && nodes.Count > 0)
            {
                _parameters = new Parameter[nodes.Count];
                for (int i = 0; i < nodes.Count; i++)
                {
                    _parameters[i] = (Parameter)XmlSerialization.ReadFromXmlNode(serializer, nodes[i]);
                }
            }
            this.MethodID = XmlSerialization.GetAttributeUInt(node, XmlSerialization.XMLATT_ID);
        }
 public void SetType(RaisDataType t)
 {
     this.DevType          = t.DevType;
     this.Name             = t.Name;
     this.LibType          = t.LibType;
     Variable.VariableType = t;
 }
 public Parameter(MethodType method, RaisDataType rt)
     : this(method)
 {
     _method = null;
     SetType(rt);
     _method = method;
 }
 public bool IsSameType(RaisDataType type)
 {
     if (type == null)
     {
         return(false);
     }
     if (this.IsLibType)
     {
         if (_libType == null)
         {
             return(false);
         }
         if (type.IsLibType)
         {
             return(this.LibType.Equals(type.LibType));
         }
     }
     else
     {
         if (!type.IsLibType)
         {
             if (DevType == null)
             {
                 return(false);
             }
             return(this.DevType.IsSameType(type.DevType));
         }
     }
     return(false);
 }
 public MethodRef(ObjectRef owner, string name, int id, ParameterDef[] parameters, RaisDataType returnType)
 {
     _methodOwnerObjectRef = owner;
     _methodName           = name;
     _methodId             = id;
     _parameters           = parameters;
     _retType = returnType;
 }
        /// <summary>
        /// create conversion code
        /// </summary>
        /// <param name="sourceType"></param>
        /// <param name="sourceCode"></param>
        /// <param name="targetType"></param>
        /// <returns>conversion code converting sourceCode to the targetType</returns>
        public static CodeExpression GetConversionCode(RaisDataType sourceType, CodeExpression sourceCode, RaisDataType targetType, CodeStatementCollection supprtStatements)
        {
            CodeExpression codeRet;

            if (sourceType.IsSameType(targetType))
            {
                return(sourceCode);
            }
            if (targetType.IsLibType || sourceType.IsLibType)
            {
                if (MathNode.GetTypeConversion != null)
                {
                    return(MathNode.GetTypeConversion(targetType.Type, sourceCode, sourceType.Type, supprtStatements));
                }
                return(CastOrConvert(sourceCode, sourceType.Type, targetType.Type, supprtStatements));
            }
            string srcType = sourceType.DevType.TypeString;
            string tgtType = targetType.DevType.TypeString;

            if (srcType.StartsWith(tgtType))
            {
                return(sourceCode);
            }
            if (tgtType.StartsWith(srcType))
            {
                return(new CodeCastExpression(tgtType, VPLUtil.GetCoreExpressionFromCast(sourceCode)));
            }
            TypeConverter converter = TypeDescriptor.GetConverter(targetType.Type);

            if (converter.CanConvertFrom(sourceType.Type))
            {
                MathNode.AddImportLocation(typeof(TypeConverter).Assembly.Location);
                string converterName = "conv" + targetType.Type.Name;
                if (!MathNodeVariable.VariableDeclared(supprtStatements, converterName))
                {
                    CodeVariableDeclarationStatement cs1 = new CodeVariableDeclarationStatement(
                        typeof(TypeConverter), converterName,
                        new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression(typeof(TypeDescriptor)), "GetConverter",
                            new CodeExpression[] { new CodeSnippetExpression("typeof(" + tgtType + ")") }));
                    supprtStatements.Add(cs1);
                }
                //=================================================
                //
                codeRet = new CodeCastExpression(tgtType,
                                                 new CodeMethodInvokeExpression(
                                                     new CodeVariableReferenceExpression(converterName), "ConvertFrom",
                                                     new CodeExpression[] { sourceCode }));
                MathNode.Trace("\tcode 102: source type:{0}, target type:{1} result=converter.ConvertFrom(code);", sourceType, targetType);
            }
            else
            {
                codeRet = CastOrConvert(sourceCode, sourceType.Type, targetType.Type, supprtStatements);
            }
            return(codeRet);
        }
        public virtual object Clone()
        {
            RaisDataType obj = (RaisDataType)Activator.CreateInstance(this.GetType(), _libType, _name);

            obj.Description = Description;
            if (_devType != null)
            {
                obj.DevType = (ObjectRef)_devType.Clone();
            }
            obj.SetServiceProvider(_serviceProvider);
            return(obj);
        }
 private void getSignature(MethodInfo mi)
 {
     _retType = new RaisDataType(mi.ReturnType);
     ParameterInfo[] ps = mi.GetParameters();
     if (ps != null && ps.Length > 0)
     {
         _parameters = new ParameterDef[ps.Length];
         for (int i = 0; i < ps.Length; i++)
         {
             _parameters[i] = new ParameterDef(ps[i].ParameterType, ps[i].Name);
         }
     }
 }
        public Parameter AddNewParameter(RaisDataType p)
        {
            int    n     = 1;
            string sbase = "parameter";
            string name  = sbase + n.ToString();

            if (_parameters != null)
            {
                bool b = true;
                while (b)
                {
                    b = false;
                    for (int i = 0; i < _parameters.Length; i++)
                    {
                        if (_parameters[i].Name == name)
                        {
                            b = true;
                            break;
                        }
                    }
                    if (b)
                    {
                        n++;
                        name = sbase + n.ToString();
                    }
                }
            }
            p.Name = name;
            if (_parameters != null)
            {
                n = _parameters.Length;
                Parameter[] a = new Parameter[n + 1];
                for (int i = 0; i < n; i++)
                {
                    a[i] = _parameters[i];
                }
                _parameters = a;
            }
            else
            {
                _parameters = new Parameter[1];
                n           = 0;
            }
            _parameters[n] = new Parameter(this, p);
            _parameters[n].Variable.MathExpression.Dummy.ResetID(this.MethodID);
            return(_parameters[n]);
        }
        public void OnReadFromXmlNode(IXmlCodeReader serializer, XmlNode node)
        {
            XmlNode methodXmlNode = null;

            _methodName           = XmlSerialization.GetAttribute(node, XmlSerialization.XMLATT_NAME);
            _methodId             = XmlSerialization.GetAttributeInt(node, XmlSerialization.XMLATT_ID);
            _methodOwnerObjectRef = (ObjectRef)XmlSerialization.ReadFromChildXmlNode(serializer, node, XmlSerialization.XML_METHODOWNER, new object[] { null });
            if (_methodId != 0)
            {
                methodXmlNode = node.OwnerDocument.SelectSingleNode(
                    XmlSerialization.FormatString("//{0}[@{1}='{2}']", XmlSerialization.XML_METHOD, XmlSerialization.XMLATT_ID, _methodId));
            }
            if (methodXmlNode == null)
            {
                if (_methodId != 0)
                {
                    throw new MathException("Method {0} not found", _methodId);
                }
                _retType = (RaisDataType)XmlSerialization.ReadFromChildXmlNode(serializer, node, XmlSerialization.XML_RETURNTYPE);
                XmlNodeList pNodes = node.SelectNodes(XmlSerialization.XML_PARAM);
                if (pNodes != null)
                {
                    _parameters = new ParameterDef[pNodes.Count];
                    for (int i = 0; i < pNodes.Count; i++)
                    {
                        _parameters[i] = (ParameterDef)XmlSerialization.ReadFromXmlNode(serializer, pNodes[i]);
                    }
                }
            }
            else
            {
                MethodType mt = new MethodType();
                mt.OnReadFromXmlNode(serializer, methodXmlNode);
                _retType = mt.ReturnType;
                Parameter[] ps = mt.Parameters;
                if (ps != null && ps.Length > 0)
                {
                    _parameters = new ParameterDef[ps.Length];
                    for (int i = 0; i < ps.Length; i++)
                    {
                        _parameters[i] = new ParameterDef(ps[i], ps[i].Name);
                    }
                }
            }
        }
        public virtual CodeExpression ReturnCodeExpression(IMethodCompile method)
        {
            string s      = method.GetParameterCodeNameById(this.ID);
            bool   bFound = !string.IsNullOrEmpty(s);

            if (bFound)
            {
                if (_code != null)
                {
                    return(_code);
                }
                _actualCompiledType = null;
                return(new CodeArgumentReferenceExpression(Variable.CodeVariableName));
            }
            else
            {
                _actualCompiledType = new RaisDataType(this.DataType.Type);
                return(ValueTypeUtil.GetDefaultCodeByType(this.DataType.Type));
            }
        }
 public ValueForSetProperty(MethodType method, RaisDataType rt)
     : base(method, rt)
 {
 }
 public void SetDataType(RaisDataType type)
 {
     SetType(type);
 }
Ejemplo n.º 14
0
 public ParameterDef(RaisDataType tp, string name)
     : this(tp)
 {
     this.Name = name;
 }
Ejemplo n.º 15
0
 public ParameterDef(RaisDataType tp)
 {
     this.DataType = tp;
 }
 public Parameter(RaisDataType t)
     : base(t)
 {
     Init();
 }
        /// <summary>
        /// retrieve _retyrnType and parameters
        /// </summary>
        private void getSignature()
        {
            if (string.IsNullOrEmpty(_methodName))
            {
                throw new MathException("Accessing getSignature with empty method name");
            }
            if (_methodOwnerObjectRef == null)
            {
                throw new MathException("Accessing getSignature with null owner");
            }

            if (_methodOwnerObjectRef.Type == ObjectRefType.Type)
            {
                Type       t  = _methodOwnerObjectRef.Value.LibType;
                MethodInfo mi = t.GetMethod(_methodName);
                if (mi == null)
                {
                    throw new MathException("Accessing getSignature with invalid method name '{0}'", _methodName);
                }
                getSignature(mi);
            }
            else if (_methodOwnerObjectRef.Type == ObjectRefType.Field || _methodOwnerObjectRef.Type == ObjectRefType.Property)
            {
                if (_methodOwnerObjectRef.Owner.Type == ObjectRefType.XPath)
                {
                }
                else if (_methodOwnerObjectRef.Owner.Type == ObjectRefType.Type)
                {
                    MethodInfo mi = null;
                    Type       t  = _methodOwnerObjectRef.Owner.Value.LibType;
                    if (_methodOwnerObjectRef.Type == ObjectRefType.Field)
                    {
                        FieldInfo fi = t.GetField(_methodOwnerObjectRef.Name);
                        if (fi == null)
                        {
                            throw new MathException(XmlSerialization.FormatString("Accessing getSignature for Field with invalid field name {0} for type {1}", _methodOwnerObjectRef.Name, t));
                        }
                        mi = fi.FieldType.GetMethod(_methodName);
                        if (mi == null)
                        {
                            throw new MathException(XmlSerialization.FormatString("Accessing getSignature for Field with invalid method name {0} for field name {1} and type {2}", _methodName, _methodOwnerObjectRef.Name, t));
                        }
                    }
                    else if (_methodOwnerObjectRef.Type == ObjectRefType.Property)
                    {
                        PropertyInfo pi = t.GetProperty(_methodOwnerObjectRef.Name);
                        if (pi == null)
                        {
                            throw new MathException(XmlSerialization.FormatString("Accessing getSignature for Property with invalid property name {0} for type {1}", _methodOwnerObjectRef.Name, t));
                        }
                        mi = pi.PropertyType.GetMethod(_methodName);
                        if (mi == null)
                        {
                            throw new MathException(XmlSerialization.FormatString("Accessing getSignature for Property with invalid method name {0} for Property name {1} and type {2}", _methodName, _methodOwnerObjectRef.Name, t));
                        }
                    }
                    getSignature(mi);
                }
                throw new MathException(XmlSerialization.FormatString("Accessing getSignature for Field. Owner type {0} not implemented", _methodOwnerObjectRef.Owner.Type));
            }
            else if (_methodOwnerObjectRef.Type == ObjectRefType.XPath)
            {
                XmlDocument doc = _methodOwnerObjectRef.GetXmlDocument();
                if (doc == null)
                {
                    throw new MathException("Accessing getSignature with null document");
                }
                XmlNode prj = XmlSerialization.GetProjectNode(doc);
                if (prj == null)
                {
                    throw new MathException("Accessing getSignature with null project");
                }
                XmlNode ownNode = prj.SelectSingleNode(_methodOwnerObjectRef.XPath);
                if (ownNode == null)
                {
                    throw new MathException(XmlSerialization.FormatString("Accessing getSignature with invalid xpath {0}", _methodOwnerObjectRef.XPath));
                }
                if (_methodName == XmlSerialization.CONSTRUCTOR_METHOD)
                {
                    throw new MathException("Accessing getSignature when the method is a constructor");
                }
                XmlNode methodNode = ownNode.SelectSingleNode(XmlSerialization.FormatString("{0}[@{1}='{2}']",
                                                                                            XmlSerialization.XML_METHOD, XmlSerialization.XMLATT_NAME, _methodName));
                if (methodNode == null)
                {
                    Type t = XmlSerialization.GetObjectLibType(ownNode);
                    if (t == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("Accessing getSignature with valid xpath {0} but method name {1} not found and owner library type not found", _methodOwnerObjectRef.XPath, _methodName));
                    }
                    MethodInfo mif = t.GetMethod(_methodName);
                    if (mif == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("Accessing getSignature with valid xpath {0} but method name {1} not found in owner library type {2}", _methodOwnerObjectRef.XPath, _methodName, t.Name));
                    }
                    getSignature(mif);
                }
                else
                {
                    MethodType mt = new MethodType();
                    mt.OnReadFromXmlNode(null, methodNode);
                    _retType = mt.ReturnType;
                    Parameter[] ps = mt.Parameters;
                    if (ps != null && ps.Length > 0)
                    {
                        _parameters = new ParameterDef[ps.Length];
                        for (int i = 0; i < ps.Length; i++)
                        {
                            _parameters[i] = new ParameterDef(ps[i]);
                        }
                    }
                }
            }
            throw new MathException(XmlSerialization.FormatString("Accessing getSignature: type not implemented: {0}", _methodOwnerObjectRef.Type));
        }