Beispiel #1
0
        void RecursiveAdd(RuntimeType type)
        {
            if (type == null || Contains(type))
            {
                return;
            }

            var id = NewId();

            Register(id, type.TypeData());

            RecursiveAdd(type.Element);
            RecursiveAdd(type.BaseType);
            RecursiveAdd(type.Surrogate?.SurrogateType);
            RecursiveAdd(type.Collection1);
            RecursiveAdd(type.Collection2);
            if (type.GenericParameters != null)
            {
                foreach (var item in type.GenericParameters)
                {
                    RecursiveAdd(item);
                }
            }
            foreach (var item in type.Members)
            {
                RecursiveAdd(item.Type);
            }
        }
Beispiel #2
0
        void GenerateCSharpCode(TextWriter w, TypeData type)
        {
            if (type.HasConverter)
            {
                w.WriteLine($"\tpublic class {ToTypeName(type)}Converter : TypeConverter {{");
                w.WriteLine("\t\t// TODO ...");
                w.WriteLine("\t}");
                w.WriteLine($"\t[TypeConverter(typeof({ToTypeName(type)}Converter))]");
            }
            string TrimAttribute(string s)
            {
                var suffix = "Attribute";

                if (s.EndsWith(suffix))
                {
                    return(s.Substring(0, s.Length - suffix.Length));
                }
                return(s);
            }

            if (type.Assembly == null && Guid.TryParse(type.FullName, out var _))
            {
                w.WriteLine($"\t[{TrimAttribute(nameof(SerializationGuidAttribute))}({ToCSharp(type.FullName)})]");
            }
            else
            {
                w.WriteLine($"\t[{TrimAttribute(nameof(SerializationNameAttribute))}({ToCSharp(type.FullName)}, {ToCSharp(type.Assembly)})]");
            }
            if (type.IsEnum)
            {
                w.WriteLine($"\tpublic enum {ToTypeName(type)} : {type.Element}");
                w.WriteLine("\t{");
                w.WriteLine("\t\t// TODO: Values need be entered manually ...");
                w.WriteLine("\t}");
            }
            else
            {
                w.Write("\tpublic ");
                w.Write(type.IsReference ? "class " : "struct ");
                w.Write(ToTypeName(type));
                if (type.IsGenericTypeDefinition)
                {
                    w.Write("<");
                    int N = type.GenericParameters.Count;
                    for (int i = 0; i < N; i++)
                    {
                        if (i > 0)
                        {
                            w.Write(",");
                        }
                        w.Write("T");
                        w.Write(i + 1);
                    }
                    w.Write(">");
                }
            }
            int    interface_count = 0;
            Action addInterface    = () =>
            {
                if (interface_count == 0)
                {
                    interface_count = 1;
                    w.Write(" : ");
                }
                else
                {
                    interface_count++;
                    w.Write(", ");
                }
            };

            if (type.BaseType != null && type.BaseType != RObject.TypeData())
            {
                addInterface();
                w.Write(ToTypeName(type.BaseType));
            }
            var tSurrogated = Objects.OfType <TypeData>().Where(x => x.Surrogate == type);

            foreach (var item in tSurrogated)
            {
                addInterface();
                w.Write($"ISurrogate<{ToTypeName(item)}");
                if ((item.GenericParameters?.Count ?? 0) > 0)
                {
                    w.Write("<");
                    int N = item.GenericParameters.Count;
                    for (int i = 0; i < N; i++)
                    {
                        if (i > 0)
                        {
                            w.Write(",");
                        }
                        w.Write("T");
                        w.Write(i + 1);
                    }
                    w.Write(">");
                }
                w.Write(">");
            }
            if (type.IsISerializable)
            {
                addInterface();
                w.Write("ISerializable");
            }
            switch (type.CollectionType)
            {
            case RuntimeCollectionType.IList:
                addInterface();
                w.Write("IList");
                break;

            case RuntimeCollectionType.ICollectionT:
                addInterface();
                w.Write($"ICollection<{ToTypeName(type.Collection1)}>");
                break;

            case RuntimeCollectionType.IDictionary:
                addInterface();
                w.Write("IDictionary");
                break;

            case RuntimeCollectionType.IDictionaryKV:
                addInterface();
                w.Write($"IDictionary<{ToTypeName(type.Collection1)}, {ToTypeName(type.Collection2)}>");
                break;
            }
            w.WriteLine();
            w.WriteLine("\t{");
            if (type.IsISerializable)
            {
                w.WriteLine($"\t\tpublic {ToTypeName(type)}() {{ }}");
                w.WriteLine($"\t\t{ToTypeName(type)}(SerializationInfo info, StreamingContext context) {{ throw new NotImplementedException(\"TODO\"); }}");
                w.WriteLine("\t\tvoid ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { throw new NotImplementedException(\"TODO\"); }");
            }
            foreach (var item in tSurrogated)
            {
                w.WriteLine($"\t\tvoid Initialize({ToTypeName(item)} value) {{ throw new NotImplementedException(\"TODO\"); }}");
                w.WriteLine($"\t\t{ToTypeName(item)} Instantiate() {{ throw new NotImplementedException(\"TODO\"); }}");
            }
            foreach (var m in type.Members)
            {
                var mID = ToID(m.Name);
                if (mID != m.Name)
                {
                    w.WriteLine($"\t\t[SerializationMemberName(\"{m.Name}\")]");
                }
                w.Write("\t\tpublic ");
                w.Write(ToTypeName(m.Type));
                w.Write(' ');
                w.Write(mID);
                w.Write(" { get; set; }");
                w.WriteLine();
            }
            w.WriteLine("\t}");
        }