Ejemplo n.º 1
0
 /// <summary>
 /// Generates the C# class that can be used to deserialize all types registered in this context.
 /// </summary>
 /// <param name="w">The writer to which the generated C# code will be written to.</param>
 /// <param name="namespace">The namespace of the generated class.</param>
 public void GenerateCSharpCode(TextWriter w, string @namespace)
 {
     w.WriteLine("// <auto-generated>");
     w.WriteLine("//     This code was generated by a tool.");
     w.WriteLine("//     But might require manual tweaking.");
     w.WriteLine("// </auto-generated>");
     w.WriteLine();
     w.WriteLine("using System.ComponentModel;");
     w.WriteLine("using System.Collections;");
     w.WriteLine("using System.Collections.Generic;");
     w.WriteLine("using Galador.Reflection.Serialization;");
     w.WriteLine();
     w.Write("namespace "); w.Write(@namespace); w.WriteLine(" {");
     foreach (var item in this.Objects.OfType <TypeData>().Where(x => x.Kind == PrimitiveType.Object && x.IsSupported))
     {
         if (item.IsGeneric && !item.IsGenericTypeDefinition)
         {
             continue;
         }
         if (item.IsGenericParameter)
         {
             continue;
         }
         var rtype = item.RuntimeType();
         if (rtype != null && FastType.IsFromMscorlib(rtype.Type))
         {
             continue;
         }
         w.WriteLine();
         GenerateCSharpCode(w, item);
     }
     w.WriteLine("}");
 }
Ejemplo n.º 2
0
        void ToTypeName(StringBuilder sb, StringBuilder sb2, TypeData type)
        {
            string getFriendlyName()
            {
                if (string.IsNullOrWhiteSpace(type.FullName))
                {
                    return("Type_");
                }
                if (Guid.TryParse(type.FullName, out var _))
                {
                    return("Type_");
                }
                var i = type.FullName.LastIndexOfAny(new char[] { '.', '+' });

                if (i + 1 == type.FullName.Length)
                {
                    return("Type_");
                }
                var s = type.FullName.Substring(i + 1);

                sb2.Clear();
                for (i++; i < type.FullName.Length; i++)
                {
                    var c = type.FullName[i];
                    if (c == '`')
                    {
                        break;
                    }
                    if (char.IsLetterOrDigit(c))
                    {
                        if (sb2.Length == 0 && char.IsDigit(c))
                        {
                            sb2.Append('T');
                        }
                        sb2.Append(c);
                    }
                }
                if (sb2.Length == 0)
                {
                    return("Type_");
                }
                sb2.Append("_");
                return(sb2.ToString());
            }

            if (type.IsEnum)
            {
                sb.Append(getFriendlyName()).Append(objectsToIds[type]);
            }
            else if (type.IsArray)
            {
                ToTypeName(sb, sb2, type.Element);
                for (int i = 1; i < type.ArrayRank; i++)
                {
                    sb.Append(',');
                }
                sb.Append(']');
            }
            else if (type.IsGenericParameter)
            {
                sb.Append('T').Append(type.GenericParameterIndex + 1);
            }
            else if (type.Kind == PrimitiveType.Object)
            {
                var rtype = type.RuntimeType();
                if (type.IsGeneric)
                {
                    if (type.IsGenericTypeDefinition)
                    {
                        if (rtype != null && FastType.IsFromMscorlib(rtype.Type))
                        {
                            var sm = 0;
                            var gm = type.FullName.LastIndexOf('`');
                            if (type.FullName.StartsWith("System.ComponentModel."))
                            {
                                sm = "System.ComponentModel.".Length;
                            }
                            else if (type.FullName.StartsWith("System.Collections.Generic."))
                            {
                                sm = "System.Collections.Generic.".Length;
                            }
                            else if (type.FullName.StartsWith("System.Collections."))
                            {
                                sm = "System.Collections.".Length;
                            }
                            sb.Append(type.FullName.Substring(sm, gm - sm));
                        }
                        else
                        {
                            sb.Append(getFriendlyName()).Append(objectsToIds[type]);
                        }
                    }
                    else
                    {
                        ToTypeName(sb, sb2, type.Element);
                        sb.Append('<');
                        for (int i = 0; i < type.GenericParameters.Count; i++)
                        {
                            if (i > 0)
                            {
                                sb.Append(",");
                            }
                            ToTypeName(sb, sb2, type.GenericParameters[i]);
                        }
                        sb.Append('>');
                    }
                }
                else
                {
                    if (rtype != null && rtype.Type == typeof(object))
                    {
                        sb.Append("object");
                    }
                    else if (rtype != null && FastType.IsFromMscorlib(rtype.Type))
                    {
                        sb.Append(rtype.FullName);
                    }
                    else
                    {
                        sb.Append(getFriendlyName()).Append(objectsToIds[type]);
                    }
                }
            }
            else
            {
                sb.Append(PrimitiveConverter.GetChsarpName(type.Kind));
            }
        }