コード例 #1
0
        /// <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);
        }