Example #1
0
        public override void FromXml(XElement xe)
        {
            base.FromXml(xe);
            Namespace = xe.Attribute("Namespace")?.Value ?? "";
            Kind      = (TypeIndexKind)Enum.Parse(typeof(TypeIndexKind), xe.Attribute("Kind").Value);
            switch (Kind)
            {
            case TypeIndexKind.GenericDefinition:
                foreach (var name in xe.Attribute("Params").Value.Split(','))
                {
                    GenericParams.Add(new TypeIndex(name, TypeIndexKind.GenericParam));
                }
                break;

            case TypeIndexKind.GenericType:
                foreach (var e in xe.Elements("TypeIndex"))
                {
                    GenericParams.Add(Parse <TypeIndex>(e));
                }
                break;
            }
        }
Example #2
0
        public Type(System.Type type)
        {
            if (type.FullName == "System.Array")
            {
                Namespace = "System";
                Name      = "Array";
                Kind      = TypeIndexKind.GenericDefinition;
                GenericParams.Add(new TypeIndex("T", TypeIndexKind.GenericParam));
            }
            else
            {
                var ti = FromSystemType(type);
                if (string.IsNullOrEmpty(ti.Namespace))
                {
                    return;
                }
                Namespace     = ti.Namespace;
                Name          = ti.Name;
                Kind          = ti.Kind;
                GenericParams = ti.GenericParams;
            }

            var ns = GetNamespace();

            if (type.IsNested)
            {
                var names = Name.Split('.');
                var ptype = ns.Types[names[0]];
                int i     = 1;
                for (; i < names.Length - 1; i++)
                {
                    ptype = ptype.Types[names[i]];
                }
                Owner = ptype;
                Name  = names[i];
                if (ptype.Types.ContainsKey(Name))
                {
                    if (ptype.Types[Name].Kind == TypeIndexKind.General)
                    {
                        ptype.Types.Remove(Name);
                    }
                    else
                    {
                        return;
                    }
                }
                ptype.Types.Add(this);
            }
            else
            {
                if (ns.Types.ContainsKey(Name))
                {
                    if (ns.Types[Name].Kind == TypeIndexKind.General)
                    {
                        ns.Types.Remove(Name);
                    }
                    else
                    {
                        return;
                    }
                }
                ns.Types.Add(this);
            }

            if (type.BaseType != null)
            {
                BaseTypes.Add(FromSystemType(type.BaseType));
            }

            var itypes = type.GetInterfaces().ToList();

            for (int i = itypes.Count - 1; i >= 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (itypes[i].IsAssignableFrom(itypes[j]))
                    {
                        itypes.RemoveAt(i);
                        break;
                    }
                }
            }

            foreach (var it in itypes)
            {
                BaseTypes.Add(FromSystemType(it));
            }

            foreach (var field in type.GetFields())
            {
                Fields.Add(new Field(field, _type_int));
            }

            if (!type.IsEnum)
            {
                foreach (var field in type.GetFields())
                {
                    Fields.Add(new Field(field));
                }

                foreach (var property in type.GetProperties())
                {
                    Properties.Add(new Property(property));
                }

                foreach (var @event in type.GetEvents())
                {
                    Events.Add(new Event(@event));
                }

                var inits = type.GetConstructors().Where(m => !m.IsStatic);
                Methods.Add(new Method(inits));

                //去除基类已定义的函数
                var imethodnames =
                    type.GetInterfaces()
                    .SelectMany(t => t.GetMethods())
                    .Select(m => m.Name)
                    .Distinct();
                var methodnames =
                    Properties.SelectMany(p => p.Value.Methodnames)
                    .Union(Events.SelectMany(e => e.Value.Methodnames))
                    .Union(imethodnames);
                if (type.BaseType != null)
                {
                    var bmethodnames =
                        type.BaseType
                        .GetMethods()
                        .Select(m => m.Name)
                        .Distinct();
                    methodnames = methodnames.Union(bmethodnames);
                }
                var methods =
                    type.GetMethods()
                    .Where(m => !(m.IsStatic && m.Name.StartsWith("op_")) && !m.Name.Contains("."))
                    .GroupBy(m => m.Name)
                    .Where(g => !methodnames.Contains(g.Key));

                foreach (var g in methods)
                {
                    Methods.Add(new Method(g));
                }

                if (Kind == TypeIndexKind.GenericDefinition)
                {
                    var bti = new TypeIndex("typing", "Generic", TypeIndexKind.GenericDefinition);
                    foreach (var p in GenericParams)
                    {
                        bti.GenericParams.Add(p);
                    }
                    BaseTypes.Add(bti);
                }

                var prop = Properties.Select(p => p.Value).FirstOrDefault(p => p.IsIndex);
                if (prop != null)
                {
                    var bti = new TypeIndex("typing", "Iterable", TypeIndexKind.GenericDefinition);
                    bti.GenericParams.Add(prop.TypeIndex);
                    BaseTypes.Add(bti);
                }
                else if (type.FullName == "System.Array")
                {
                    var bti = new TypeIndex("typing", "Iterable", TypeIndexKind.GenericDefinition);
                    bti.GenericParams.Add(new TypeIndex("T", TypeIndexKind.GenericParam));
                    BaseTypes.Add(bti);
                }

                if (BaseTypes.Count > 1 && BaseTypes[0].Equals(Object))
                {
                    BaseTypes.RemoveAt(0);
                }

                //去除基类已定义的事件
                var eventnames =
                    type.GetInterfaces()
                    .SelectMany(i => i.GetEvents())
                    .Select(e => e.Name);
                if (type.BaseType != null)
                {
                    eventnames =
                        type.BaseType
                        .GetEvents()
                        .Select(e => e.Name)
                        .Union(eventnames);
                }
                foreach (var name in eventnames.Distinct())
                {
                    Events.Remove(name);
                }

                //去除基类已定义的属性
                var propnames =
                    type.GetInterfaces()
                    .SelectMany(i => i.GetProperties())
                    .Select(p => p.Name);
                if (type.BaseType != null)
                {
                    propnames =
                        type.BaseType
                        .GetProperties()
                        .Select(p => p.Name)
                        .Union(propnames);
                }
                foreach (var name in propnames.Distinct())
                {
                    Properties.Remove(name);
                }
            }
        }