コード例 #1
0
        public static void AddVariableDeclaration(Type type, string name, CodeStatementCollection statements)
        {
            for (int i = 0; i < statements.Count; i++)
            {
                CodeVariableDeclarationStatement vd = statements[i] as CodeVariableDeclarationStatement;
                if (vd != null)
                {
                    if (vd.Name == name)
                    {
                        return;
                    }
                }
            }
            CodeVariableDeclarationStatement p;

            if (type.IsValueType)
            {
                p = new CodeVariableDeclarationStatement(type, name);
            }
            else
            {
                p = new CodeVariableDeclarationStatement(type, name, ObjectCreationCodeGen.GetDefaultValueExpression(type));
            }
            statements.Insert(0, p);
        }
コード例 #2
0
        public static void CheckDeclareField(bool isStatic, string fieldName, DataTypePointer fieldType, CodeTypeDeclaration typeDeclaration, string defaultValue)
        {
            CodeMemberField cmf;

            foreach (CodeTypeMember ctm in typeDeclaration.Members)
            {
                cmf = ctm as CodeMemberField;
                if (cmf != null)
                {
                    if (string.CompareOrdinal(cmf.Name, fieldName) == 0)
                    {
                        return;
                    }
                }
            }
            cmf = new CodeMemberField(fieldType.TypeString, fieldName);
            if (!string.IsNullOrEmpty(defaultValue))
            {
                bool   b;
                object val = VPLUtil.ConvertObject(defaultValue, fieldType.BaseClassType, out b);
                if (b)
                {
                    cmf.InitExpression = ObjectCreationCodeGen.ObjectCreationCode(val);
                }
            }
            if (isStatic)
            {
                cmf.Attributes |= MemberAttributes.Static;
            }
            typeDeclaration.Members.Add(cmf);
        }
コード例 #3
0
        public IList <CodeAttributeArgument> CreatePropertyExpressions()
        {
            List <CodeAttributeArgument> lst = new List <CodeAttributeArgument>();

            if (_values != null && _values.Count > 0)
            {
                if (_defValues == null)
                {
                    _defValues = new Dictionary <string, object>();
                }
                if (_defValues.Count == 0)
                {
                    GetProperties();
                }
                foreach (KeyValuePair <string, object> kv in _values)
                {
                    if (_defValues.ContainsKey(kv.Key))
                    {
                        if (VPLUtil.IsValueEqual(kv.Value, _defValues[kv.Key]))
                        {
                            continue;
                        }
                    }
                    lst.Add(new CodeAttributeArgument(kv.Key, ObjectCreationCodeGen.ObjectCreationCode(kv.Value)));
                }
            }
            return(lst);
        }
コード例 #4
0
        public static CodeExpression ConvertToBool(Type t, CodeExpression ce)
        {
            if (t.Equals(typeof(bool)))
            {
                return(ce);
            }
            if (t.Equals(typeof(string)))
            {
                CodeBinaryOperatorExpression ret = new CodeBinaryOperatorExpression();
                ret.Operator = CodeBinaryOperatorType.BooleanOr;
                CodeBinaryOperatorExpression b = new CodeBinaryOperatorExpression();
                b.Operator = CodeBinaryOperatorType.IdentityEquality;
                b.Right    = new CodePrimitiveExpression(0);
                CodeMethodInvokeExpression mi = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(string)), "Compare"),
                                                                               ce, new CodePrimitiveExpression("true"), new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(StringComparison)), "OrdinalIgnoreCase"));
                b.Left   = mi;
                ret.Left = b;
                //
                b          = new CodeBinaryOperatorExpression();
                b.Operator = CodeBinaryOperatorType.IdentityEquality;
                b.Right    = new CodePrimitiveExpression(0);
                mi         = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(string)), "Compare"),
                                                            ce, new CodePrimitiveExpression("on"), new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(StringComparison)), "OrdinalIgnoreCase"));
                b.Left    = mi;
                ret.Right = b;
                return(ret);
            }
            if (typeof(object).Equals(t) || typeof(byte).Equals(t) ||
                typeof(char).Equals(t) || typeof(DateTime).Equals(t) ||
                typeof(decimal).Equals(t) || typeof(double).Equals(t) ||
                typeof(float).Equals(t) || typeof(int).Equals(t) ||
                typeof(long).Equals(t) || typeof(sbyte).Equals(t) ||
                typeof(short).Equals(t) || typeof(uint).Equals(t) ||
                typeof(ulong).Equals(t) || typeof(ushort).Equals(t)
                )
            {
                CodeMethodInvokeExpression mie = new CodeMethodInvokeExpression();
                mie.Method = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean");
                mie.Parameters.Add(ce);
                if (IsPrimaryExp(ce))
                {
                    return(mie);
                }
                CodeBinaryOperatorExpression bo = new CodeBinaryOperatorExpression();
                bo.Operator = CodeBinaryOperatorType.BooleanAnd;
                bo.Left     = new CodeBinaryOperatorExpression(ce, CodeBinaryOperatorType.IdentityInequality, new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(DBNull)), "Value"));
                bo.Right    = mie;
                return(bo);
            }
            CodeExpression def = ObjectCreationCodeGen.GetDefaultValueExpression(t);

            return(new CodeBinaryOperatorExpression(ce, CodeBinaryOperatorType.IdentityInequality, def));
        }
コード例 #5
0
        public static CodeVariableDeclarationStatement CreateVariableDeclaration(CodeTypeReference type, string name, Type t, object init)
        {
            CodeVariableDeclarationStatement p;

            if (init == null)
            {
                p = new CodeVariableDeclarationStatement(type, name, ObjectCreationCodeGen.GetDefaultValueExpression(t));
            }
            else
            {
                p = new CodeVariableDeclarationStatement(type, name, ObjectCreationCodeGen.ObjectCreationCode(init));
            }
            return(p);
        }
コード例 #6
0
        public CodeExpression GetReferenceCode(IMethodCompile method, CodeStatementCollection statements, bool forValue)
        {
            CodeExpression ret = null;

            if (_itemType != null)
            {
                //method.MethodCode.Statements
                if (_array == null)
                {
                    //without initializers
                    ret = _itemType.CreateArrayCreationCode();
                }
                else
                {
                    //with initializers
                    if (_itemType.Rank == 1)
                    {
                        CodeExpression[] inits = new CodeExpression[_itemType.Dimnesions[0]];
                        for (int i = 0; i < inits.Length; i++)
                        {
                            object v = _array.GetValue(i);
                            if (v == null)
                            {
                                inits[i] = CompilerUtil.GetDefaultValueExpression(_itemType.ItemBaseType);
                            }
                            else
                            {
                                IObjectPointer op = v as IObjectPointer;
                                if (op != null)
                                {
                                    inits[i] = op.GetReferenceCode(method, statements, forValue);
                                }
                                else
                                {
                                    inits[i] = ObjectCreationCodeGen.ObjectCreationCode(v);
                                }
                            }
                        }
                        CodeArrayCreateExpression ac = new CodeArrayCreateExpression(_itemType.ItemBaseTypeString, inits);
                        ret = ac;
                    }
                    else
                    {
                        //only support constant element values
                        StringBuilder sb = new StringBuilder(CompilerUtil.CreateArrayCreationCodeString(_itemType.Rank, _itemType.ItemBaseTypeName));
                        sb.Append("{");
                        for (int i = 0; i < _itemType.Rank; i++)
                        {
                            if (i > 0)
                            {
                                sb.Append(",");
                            }
                            sb.Append("{");
                            for (int j = 0; j < _itemType.Dimnesions[i]; j++)
                            {
                                if (j > 0)
                                {
                                    sb.Append(",");
                                }
                                sb.Append(ObjectCreationCodeGen.GetObjectCreationCodeSnippet(_array.GetValue(i, j)));
                            }
                            sb.Append("}");
                        }
                        sb.Append("}");
                        ret = new CodeSnippetExpression(sb.ToString());
                        //throw new NotImplementedException("Multi-dimensional array code not implemented");
                    }
                }
            }
            return(ret);
        }
コード例 #7
0
        /// <summary>
        /// create a CodeExpression to generate an object
        /// </summary>
        /// <param name="v">the object to be generated</param>
        /// <returns>the CodeExpression generating the object</returns>
        public static CodeExpression ObjectCreationCode(object v)
        {
            //===simple values===
            if (v == null)
            {
                return(new CodePrimitiveExpression(null));
            }
            if (v == System.DBNull.Value)
            {
                return(new CodePropertyReferenceExpression(new CodeTypeReferenceExpression(typeof(System.DBNull)), "Value"));
            }

            Type t = v.GetType();

            if (t.Equals(typeof(EventArgs)))
            {
                return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EventArgs)), "Empty"));
            }
            if (t.IsPrimitive)
            {
                CodePrimitiveExpression cpi = new CodePrimitiveExpression(v);
                TypeCode tc = Type.GetTypeCode(t);
                return(new CodeCastExpression(t, cpi));
            }
            if (t.Equals(typeof(string)))
            {
                return(new CodePrimitiveExpression(v));
            }
            if (t.IsEnum)
            {
                return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(t), v.ToString()));
            }
            if (t.IsArray)
            {
                Type  te = t.GetElementType();
                Array a  = (Array)v;
                if (a.Rank == 1)
                {
                    CodeExpression[] inits = new CodeExpression[a.Length];
                    for (int i = 0; i < inits.Length; i++)
                    {
                        object val = a.GetValue(i);
                        if (val == null)
                        {
                            inits[i] = CompilerUtil.GetDefaultValueExpression(te);
                        }
                        else
                        {
                            inits[i] = ObjectCreationCodeGen.ObjectCreationCode(val);
                        }
                    }
                    CodeArrayCreateExpression ac = new CodeArrayCreateExpression(t, inits);
                    return(ac);
                }
                else
                {
                    StringBuilder sb = new StringBuilder(CompilerUtil.CreateArrayCreationCodeString(a.Rank, te.Name));
                    sb.Append("{");
                    for (int i = 0; i < a.Rank; i++)
                    {
                        if (i > 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append("{");
                        for (int j = a.GetLowerBound(i); j <= a.GetUpperBound(i); j++)
                        {
                            if (j > 0)
                            {
                                sb.Append(",");
                            }
                            sb.Append(ObjectCreationCodeGen.GetObjectCreationCodeSnippet(a.GetValue(i, j)));
                        }
                        sb.Append("}");
                    }
                    sb.Append("}");
                    return(new CodeSnippetExpression(sb.ToString()));
                }
            }
            //===use plug-ins to handle it===
            if (creators != null)
            {
                foreach (IGetObjectCreationCode occ in creators.Values)
                {
                    CodeExpression ce = occ.ObjectCreationCode(v);
                    if (ce != null)
                    {
                        return(ce);
                    }
                }
            }
            //===use default handlers===
            if (t.Equals(typeof(Size)))
            {
                Size v1 = (Size)v;
                return(new CodeObjectCreateExpression(t,
                                                      new CodePrimitiveExpression(v1.Width),
                                                      new CodePrimitiveExpression(v1.Height)));
            }
            if (t.Equals(typeof(Point)))
            {
                Point v1 = (Point)v;
                return(new CodeObjectCreateExpression(t,
                                                      new CodePrimitiveExpression(v1.X),
                                                      new CodePrimitiveExpression(v1.Y)));
            }
            if (t.Equals(typeof(Color)))
            {
                Color v1 = (Color)v;
                return(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Color)), "FromArgb"),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.A)),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.R)),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.G)),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.B))
                                                      ));
            }
            if (t.Equals(typeof(Cursor)))
            {
                //System.Windows.Forms.Cursors.
                Cursor v1 = (Cursor)v;
                if (v1 == Cursors.AppStarting)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "AppStarting"));
                }
                else if (v1 == Cursors.Arrow)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Arrow"));
                }
                else if (v1 == Cursors.Cross)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Cross"));
                }
                else if (v1 == Cursors.Default)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Default"));
                }
                else if (v1 == Cursors.Hand)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Hand"));
                }
                else if (v1 == Cursors.Help)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Help"));
                }
                else if (v1 == Cursors.HSplit)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "HSplit"));
                }
                else if (v1 == Cursors.IBeam)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "IBeam"));
                }
                else if (v1 == Cursors.No)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "No"));
                }
                else if (v1 == Cursors.NoMove2D)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "NoMove2D"));
                }
                else if (v1 == Cursors.NoMoveHoriz)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "NoMoveHoriz"));
                }
                else if (v1 == Cursors.NoMoveVert)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "NoMoveVert"));
                }
                else if (v1 == Cursors.PanEast)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanEast"));
                }
                else if (v1 == Cursors.PanNE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanNE"));
                }
                else if (v1 == Cursors.PanNorth)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanNorth"));
                }
                else if (v1 == Cursors.PanNW)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanNW"));
                }
                else if (v1 == Cursors.PanSE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanSE"));
                }
                else if (v1 == Cursors.PanSouth)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanSouth"));
                }
                else if (v1 == Cursors.PanSW)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanSW"));
                }
                else if (v1 == Cursors.PanWest)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanWest"));
                }
                else if (v1 == Cursors.SizeAll)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeAll"));
                }
                else if (v1 == Cursors.SizeNESW)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeNESW"));
                }
                else if (v1 == Cursors.SizeNS)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeNS"));
                }
                else if (v1 == Cursors.SizeNWSE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeNWSE"));
                }
                else if (v1 == Cursors.SizeWE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeWE"));
                }
                else if (v1 == Cursors.UpArrow)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "UpArrow"));
                }
                else if (v1 == Cursors.VSplit)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "VSplit"));
                }
                else if (v1 == Cursors.WaitCursor)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "WaitCursor"));
                }
                else
                {
                    CursorConverter cc = new CursorConverter();
                    string          s  = cc.ConvertToString(v);
                    return(new CodeMethodInvokeExpression(
                               new CodeObjectCreateExpression(typeof(CursorConverter), null),
                               "ConvertFromString",
                               new CodePrimitiveExpression(s)
                               ));
                    //new CursorConverter().ConvertFromString(s)
                }
            }
            if (t.Equals(typeof(Font)))
            {
                Font f = (Font)v;
                return(new CodeObjectCreateExpression(typeof(Font), new CodePrimitiveExpression(f.FontFamily.ToString()), new CodePrimitiveExpression(f.Size)));
            }
            //use string converter ===================================
            TypeConverter sc = TypeDescriptor.GetConverter(t);

            if (sc != null)
            {
                if (sc.CanConvertTo(typeof(string)) && sc.CanConvertFrom(typeof(string)))
                {
                    string svalue = sc.ConvertToInvariantString(v);
                    //TypeDescriptor.GetConverter(t).ConvertFromInvariantString(svalue)
                    CodeMethodInvokeExpression mgc = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(TypeDescriptor)), "GetConverter", new CodeTypeOfExpression(t));
                    CodeMethodInvokeExpression fs  = new CodeMethodInvokeExpression(mgc, "ConvertFromInvariantString", new CodePrimitiveExpression(svalue));
                    //
                    return(new CodeCastExpression(t, fs));
                }
            }
            //===use TypeConverter as the last resort===
            PropertyDescriptorCollection pifs = TypeDescriptor.GetProperties(v);

            if (pifs == null || pifs.Count == 0)
            {
                return(new CodeObjectCreateExpression(t));
            }
            int nCount = 0;

            for (int i = 0; i < pifs.Count; i++)
            {
                if (pifs[i].IsBrowsable && !pifs[i].IsReadOnly)
                {
                    nCount++;
                }
            }
            if (nCount == 0)
            {
                return(new CodeObjectCreateExpression(t));
            }
            string sCodeSnippet = GetObjectCreationCodeSnippet(v);

            return(new CodeSnippetExpression(sCodeSnippet));
        }