Example #1
0
        /// <summary>
        /// An easily readable name
        /// </summary>
        public string GenerateName(bool prefixWithGlobal = false)
        {
            if (IsGenericParameter)
            {
                return(Name);
            }
            string theReturn = GenerateNameWithoutGenericsAndArrays();

            if (IsGenericTypeDefinition)
            {
                theReturn = $"{theReturn}<{string.Join(",", GenericParameters.Select(t => t.Name))}>";
            }
            else if (IsConstructedGenericType)
            {
                theReturn = $"{theReturn}<{string.Join(",", Wrapped.Select(t => t.GenerateName(prefixWithGlobal)))}>";
            }

            if (ArrayDimensions > 0)
            {
                theReturn = theReturn + $"[{string.Join(",", Enumerable.Repeat(string.Empty, ArrayDimensions))}]";
            }

            if (prefixWithGlobal)
            {
                theReturn = "global::" + theReturn;
            }
            return(theReturn);
        }
Example #2
0
        public void Initialize()
        {
            //properties
            foreach (var tsProperty in Properties)
            {
                tsProperty.Initialize();
            }

            //base types
            DerivedInterfaces = Interfaces.Select(_mapType).Except(new[] { Types.Any, Types.Boolean, Types.Number, Types.String, Types.Void }).ToList();


            //generic
            foreach (var tsGenericParameter in GenericParameters)
            {
                tsGenericParameter.Initialize();
            }
            if (_type.IsGenericType)
            {
                InterFaceName += string.Format("<{0}>", string.Join(", ", GenericParameters.Select(p => p.ToTSString())));
            }

            //methods
            foreach (var tsMethod in Methods)
            {
                tsMethod.Initialize();
            }
        }
Example #3
0
        TypeData MakeGenericTypeData(IReadOnlyList <TypeData> parameters)
        {
            if (!IsSupported)
            {
                return(this);
            }

            if (IsGenericParameter)
            {
                return(parameters[this.GenericParameterIndex]);
            }

            if (!IsGeneric)
            {
                return(this);
            }

            var result = new TypeData()
            {
                IsSupported             = true,
                Kind                    = Kind,
                IsSealed                = IsSealed,
                IsReference             = IsReference,
                IsEnum                  = IsEnum,
                IsInterface             = IsInterface,
                HasConverter            = HasConverter,
                IsISerializable         = IsISerializable,
                IsGeneric               = true,
                IsGenericTypeDefinition = false,
                IsNullable              = IsNullable,
                Element                 = this,
                GenericParameters       = GenericParameters.Select(x => x.MakeGenericTypeData(parameters)).ToList().AsReadOnly(),
            };

            result.BaseType  = BaseType?.MakeGenericTypeData(parameters);
            result.Surrogate = Surrogate?.MakeGenericTypeData(parameters);

            if (Surrogate == null &&
                !IsInterface &&
                !IsArray && !IsEnum &&
                !IsGenericParameter)
            {
                foreach (var m in Members)
                {
                    var rm = new Member(result);
                    rm.Name = m.Name;
                    rm.Type = m.Type.MakeGenericTypeData(parameters);
                    result.Members.Add(rm);
                }
                result.CollectionType = CollectionType;
                result.Collection1    = Collection1?.MakeGenericTypeData(parameters);
                result.Collection2    = Collection2?.MakeGenericTypeData(parameters);
            }
            return(result);
        }
Example #4
0
        public RuntimeType RuntimeType()
        {
            if (target == null && !resolved)
            {
                resolved = true;
                try
                {
                    switch (Kind)
                    {
                    case PrimitiveType.None:
                        break;

                    case PrimitiveType.Object:
                        if (IsArray)
                        {
                            var type = Element.RuntimeType()?.Type;
                            if (ArrayRank == 1)
                            {
                                type = type?.MakeArrayType();
                            }
                            else if (ArrayRank > 1)
                            {
                                type = type?.MakeArrayType(ArrayRank);
                            }
                            target = Serialization.RuntimeType.GetType(type);
                        }
                        else if (IsGenericParameter)
                        {
                            // nothing
                        }
                        else if (IsGeneric && !IsGenericTypeDefinition)
                        {
                            var type       = Element.RuntimeType()?.Type;
                            var parameters = GenericParameters.Select(x => x.RuntimeType()?.Type).ToArray();
                            if (type != null && parameters.All(x => x != null))
                            {
                                target = Serialization.RuntimeType.GetType(type.MakeGenericType(parameters));
                            }
                        }
                        else
                        {
                            target = Serialization.RuntimeType.GetType(FullName, Assembly);
                        }
                        break;

                    default:
                        target = Serialization.RuntimeType.GetType(PrimitiveConverter.GetType(Kind));
                        break;
                    }
                }
                catch (SystemException se) { Log.Error(se); }
            }
            return(target);
        }
Example #5
0
 public TypeRef Clone() => new TypeRef()
 {
     ArrayDimensions          = ArrayDimensions,
     GenericParameters        = GenericParameters?.Select(gp => gp.Clone()).ToArray(),
     IsPrimitive              = IsPrimitive,
     IsConstructedGenericType = IsConstructedGenericType,
     IsEnum                  = IsEnum,
     IsGenericParameter      = IsGenericParameter,
     IsGenericTypeDefinition = IsGenericTypeDefinition,
     IsWellKnown             = IsWellKnown,
     Name        = Name,
     Namespace   = Namespace,
     NestedIn    = NestedIn?.Clone(),
     Wrapped     = Wrapped?.Select(w => w.Clone()).ToArray(),
     IsInterface = IsInterface
 };
Example #6
0
        public void Initialize()
        {
            var ti = _type.GetTypeInfo();

            //properties
            foreach (var tsProperty in Properties)
            {
                tsProperty.Initialize();
            }

            //base types
            var exc = new [] { Types.Any, Types.Boolean, Types.Number, Types.String, Types.Void };

            foreach (var intr in Interfaces)
            {
                var typ = _mapType(intr);
                if (!exc.Contains(typ))
                {
                    DerivedInterfaces.Add(typ);
                }
            }
            //DerivedInterfaces = Interfaces.Select(_mapType).Except(new[] { Types.Any, Types.Boolean, Types.Number, Types.String, Types.Void }).ToList();
            //foreach(var der in DerivedInterfaces) { }

            //generic
            foreach (var tsGenericParameter in GenericParameters)
            {
                tsGenericParameter.Initialize();
            }
            if (ti.IsGenericType)
            {
                InterFaceName += string.Format("<{0}>", string.Join(", ", GenericParameters.Select(p => p.ToTSString())));
            }

            //methods
            foreach (var tsMethod in Methods)
            {
                tsMethod.Initialize();
            }
        }
Example #7
0
        public string MakeGenericName(string openBracket, string closeBracket, string commaSeparator)
        {
            if (!IsGenericType)
            {
                return(this.Name.ToString());
            }

            if (IsGenericDefinition)
            {
                return(
                    this.Name +
                    openBracket +
                    string.Join(commaSeparator, GenericParameters.Select(t => t.Name)) +
                    closeBracket);
            }
            else
            {
                return(
                    this.Name +
                    openBracket +
                    string.Join(commaSeparator, GenericArguments.Select(t => t.MakeGenericName(openBracket, closeBracket, commaSeparator))) +
                    closeBracket);
            }
        }
Example #8
0
 public override string ToString()
 {
     if (ExtensionOf == null)
     {
         return("{0}{2} ( {1} )".With(Name, String.Join(", ", Parameters.Select(p => p.ToString())), GenericParameters != null && GenericParameters.Count > 0 ? "<{0}>".With(String.Join(", ", GenericParameters.Select(g => g.ToString()))) : ""));
     }
     else
     {
         return("{0}{2} ( {1} )".With(
                    Name,
                    String.Join(", ", Parameters.Skip(1).Select(p => p.ToString())),
                    GenericParameters != null && GenericParameters.Count > 0 ? "<{0}>".With(String.Join(", ", GenericParameters.Select(g => g.ToString()))) : ""
                    ));
     }
 }
Example #9
0
        public string GetCode()
        {
            var rst = new StringBuilder();

            rst.Append(CommonUsing());

            rst.AppendLine($@"namespace {Category.FullName}
{{");
            rst.AppendLine("\t//BO:" + Oid);
            if (!IsPersistent)
            {
                rst.AppendLine("\t[NonPersistent]");
            }

            if (IsCloneable.HasValue && IsCloneable.Value)
            {
                rst.AppendLine("\t[ModelDefault(\"Cloneable\",\"True\")]");
            }

            if (IsCreatableItem.HasValue && IsCreatableItem.Value)
            {
                rst.AppendLine("\t[ModelDefault(\"Createable\",\"True\")]");
            }

            if (IsVisibileInReports.HasValue && IsVisibileInReports.Value)
            {
                rst.AppendLine("\t[VisibleInReport]");
            }

            rst.Append($"\tpublic { (CanInherits ? "" : "sealed") + (IsAbstract ? "abstract" : "") } partial class { 名称 } ");

            if (IsGenericTypeDefine)
            {
                //如果设置了泛型参数的值,则付入,否则认为本类也是泛型类
                rst.AppendFormat("<{0}>",
                                 string.Join(",",
                                             GenericParameters.Where(x => x.ParameterValue == null)
                                             .OrderBy(x => x.ParameterIndex)
                                             .Select(x => x.Name)
                                             .ToArray()));
            }

            rst.Append(":");

            if (Base.IsGenericTypeDefine)
            {
                var n = Base.FullName;
                if (Base.IsRuntimeDefine)
                {
                    rst.Append("global::" + n);
                }
                else
                {
                    rst.Append("global::" + n.Substring(0, n.Length - 2));
                }

                //如果设置了泛型参数的值,则付入,否则认为本类也是泛型类
                rst.AppendFormat("<{0}>",
                                 string.Join(",",
                                             GenericParameters.Select(
                                                 x => x.ParameterValue == null ? x.Name : "global::" + x.ParameterValue.FullName).ToArray()));
            }
            else
            {
                rst.Append("global::" + Base.FullName);
            }
            rst.AppendLine();

            var constraints = string.Join("\n", GenericParameters.Where(x => !string.IsNullOrEmpty(x.Constraint)).Select(x => " where " + x.Name + " : " + x.Constraint));

            rst.AppendLine(constraints);

            //begin class
            rst.AppendLine("\t{");

            rst.AppendLine($"\t\tpublic {名称}(Session s):base(s){{  }}");

            var propertyTemplate =
                "\t\tpublic {0} {1}\n" +
                "\t\t{\n" +
                "\t\t\tget { return _{1}; }\n" +
                "\t\t\tset { SetPropertyValue(\"{1}\",ref _{1},value); }\n" +
                "\t\t}\n";

            foreach (var item in Properties)
            {
                var pt = "global::" + item.PropertyType.FullName;
                rst.AppendLine($"\t\t{ pt } _{ item.名称 };");

                if (item.Size != 100 && item.Size != 0)
                {
                    rst.AppendLine($"\t\t[Size({item.Size})]");
                }
                ProcessPropertyBase(rst, item);
                if (item.RelationProperty != null)
                {
                    var assName = string.Format("{0}_{1}", item.RelationProperty.称, item.称);
                    rst.AppendFormat("\t\t[{0}(\"{1}\")]", typeof(AssociationAttribute).FullName, assName);
                }


                rst.Append(propertyTemplate.Replace("{0}", pt).Replace("{1}", item.称));
            }

            var att = "\t\t[" + typeof(DevExpress.Xpo.AggregatedAttribute).FullName + "]";

            foreach (var item in CollectionProperties)
            {
                if (item.Aggregated)
                {
                    rst.AppendLine(att);
                }
                ProcessPropertyBase(rst, item);

                var assName = string.Format("{0}_{1}", item.称, item.RelationProperty.称);
                rst.AppendFormat("\t\t[{0}(\"{1}\")]\n", typeof(AssociationAttribute).FullName, assName);
                var pt = "global::" + item.PropertyType.FullName;
                rst.AppendLine($"\t\tpublic XPCollection<{pt}> {item.名称}{{ get{{ return GetCollection<{pt}>(\"{item.名称}\"); }} }}");
            }

            foreach (var method in Methods)
            {
                rst.AppendLine(method.MethodDefineCode);
            }

            //end class
            rst.AppendLine("\t}");
            rst.AppendLine("}");
            return(rst.ToString());
        }