/// <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) { if (t.Equals(typeof(IntPtr)) || t.Equals(typeof(UIntPtr))) { CodeObjectCreateExpression coce; if (t.Equals(typeof(IntPtr))) { IntPtr val = (IntPtr)v; if (IntPtr.Size == 4) { coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToInt32())); } else { coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToInt64())); } } else { UIntPtr val = (UIntPtr)v; if (IntPtr.Size == 4) { coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToUInt32())); } else { coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToUInt64())); } } return(coce); } else { CodePrimitiveExpression cpi = new CodePrimitiveExpression(v); if (VPLUtil.IsNumber(t)) { if (!typeof(int).Equals(t)) { return(new CodeCastExpression(t, cpi)); } } return(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] = GetDefaultValueExpression(te); } else { inits[i] = ObjectCreationCodeGen.ObjectCreationCode(val); } } CodeArrayCreateExpression ac = new CodeArrayCreateExpression(t, inits); return(ac); } else { StringBuilder sb = new StringBuilder(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) )); } } if (t.Equals(typeof(Font))) { Font f = (Font)v; return(new CodeObjectCreateExpression(typeof(Font), new CodePrimitiveExpression(f.FontFamily.ToString()), new CodePrimitiveExpression(f.Size))); } if (t.Equals(typeof(Icon))) { Icon ic = (Icon)v; using (MemoryStream ms = new MemoryStream()) { ic.Save(ms); byte[] bs = ms.ToArray(); CodeObjectCreateExpression coce = new CodeObjectCreateExpression(); coce.CreateType = new CodeTypeReference(t); CodeObjectCreateExpression p1 = new CodeObjectCreateExpression(); coce.Parameters.Add(p1); p1.CreateType = new CodeTypeReference(typeof(MemoryStream)); CodeExpression p2 = ObjectCreationCode(bs); p1.Parameters.Add(p2); return(coce); } } //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); 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)); }
public static CodeExpression GetDefaultValueExpression(Type t) { if (t == null) { return(null); } if (t.IsGenericType) { return(null); } if (t.IsGenericParameter) { return(null); } if (t.IsGenericTypeDefinition) { return(null); } bool isByref = t.IsByRef; if (isByref) { t = t.GetElementType(); if (typeof(object).Equals(t)) { //System.Reflection.Missing.Value return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(System.Reflection.Missing)), "Value")); } } if (t.IsArray) { return(new CodePrimitiveExpression(null)); } if (t.IsEnum) { Array a = Enum.GetValues(t); if (a.Length > 0) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(t), a.GetValue(0).ToString())); } return(new CodeCastExpression(t, new CodePrimitiveExpression(0))); } if (t.Equals(typeof(DateTime))) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(DateTime)), "MinValue")); } if (t.Equals(typeof(IntPtr))) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(IntPtr)), "Zero")); } if (t.Equals(typeof(UIntPtr))) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(UIntPtr)), "Zero")); } if (t.IsPrimitive) { return(new CodePrimitiveExpression(VPLUtil.GetDefaultValue(t))); } if (t.Equals(typeof(string))) { return(new CodePrimitiveExpression(null)); } if (t.IsValueType) { // use Activator.CreateInstance<T>() CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(); cmie.Method = new CodeMethodReferenceExpression(); cmie.Method.TargetObject = new CodeTypeReferenceExpression(typeof(Activator)); cmie.Method.MethodName = "CreateInstance"; cmie.Method.TypeArguments.Add(new CodeTypeReference(t)); return(cmie); } object v = VPLUtil.GetDefaultValue(t); return(ObjectCreationCodeGen.ObjectCreationCode(v)); }