Exemplo n.º 1
0
        /// <summary>
        /// To be called after WalkTypes(TreeNode). This method walks all classes and defines its methods and fields
        /// </summary>
        public void WalkTypeMembers()
        {
            // now define all methods and fields of the classes
            int interfaces = 0;
            Dictionary <TypeBuilder, UserType> hasConstructors = new Dictionary <TypeBuilder, UserType>();

            for (int i = 0; i < types.Count; i++)
            {
                TreeNode    n          = types[i];
                UserType    customType = usertypes[i];
                TypeBuilder tb         = (TypeBuilder)n.Entry.ReflectionObject;
                Type        b;
                if (customType.BaseType is UserType)
                {
                    b = (Type)((UserType)customType.BaseType).Entry.ReflectionObject;
                }
                else
                {
                    b = customType.BaseType;
                }

                if (!tb.IsInterface)
                {
                    tb.SetParent(b);
                }

                foreach (Type t in customType.GetDirectInterfaces())
                {
                    tb.AddInterfaceImplementation(t);
                }

                int index = allTypes.IndexOf(tb.BaseType as TypeBuilder);
                if (index != -1 && index > allTypes.IndexOf(tb))
                {
                    TypeBuilder tb2 = (TypeBuilder)tb.BaseType;
                    allTypes.Remove(tb2);
                    allTypes.Insert(allTypes.IndexOf(tb), tb2);
                }
                else if (tb.IsInterface && tb.GetInterfaces().Length > 0 && allTypes.IndexOf(tb.GetInterfaces()[0] as TypeBuilder) > allTypes.IndexOf(tb))
                {
                    TypeBuilder tb2 = (TypeBuilder)tb.GetInterfaces()[0];
                    allTypes.Remove(tb2);
                    allTypes.Insert(allTypes.IndexOf(tb), tb2);
                    interfaces++;
                }
                else if (tb.IsInterface)
                {
                    allTypes.Remove(tb);
                    allTypes.Insert(interfaces, tb);
                    interfaces++;
                }

                Boolean hasConstructor = false;
                int     enumCount      = 0;
                foreach (TreeNode n2 in n.Children)
                {
                    if (n2.Type == VnvdTreeWalker.FIELD)
                    {
                        UserField uf = (UserField)n2.Entry.ReflectionObject;
                        n2.Entry.ReflectionObject = DefineField(tb, uf.Name, uf.Attributes, uf.FieldType);
                    }
                    else if (n2.Type == VnvdTreeWalker.CONSTRUCTOR)
                    {
                        hasConstructor = true;
                        UserConstructor uf = (UserConstructor)n2.Entry.ReflectionObject;
                        n2.Entry.ReflectionObject = DefineConstructor(tb, uf.Attributes, uf.GetParameters());
                    }
                    else if (n2.Type == VnvdTreeWalker.METHOD)
                    {
                        UserMethod uf = (UserMethod)n2.Entry.ReflectionObject;
                        n2.Entry.ReflectionObject = DefineMethod(tb, uf.Name, uf.Attributes, uf.ReturnType, uf.GetParameters());
                    }
                    else if (n2.Type == VnvdTreeWalker.IMETHOD)
                    {
                        UserMethod uf = (UserMethod)n2.Entry.ReflectionObject;
                        n2.Entry.ReflectionObject = DefineMethod(tb, uf.Name, uf.Attributes, uf.ReturnType, uf.GetParameters());
                    }
                    else if (n2.Type == VnvdTreeWalker.SCONSTRUCTOR)
                    {
                        //UserConstructor uf = (UserConstructor)n2.Entry.ReflectionObject;
                        n2.Entry.ReflectionObject = tb.DefineTypeInitializer();
                    }
                    else if (n2.Type == VnvdTreeWalker.ENUMOPTION)
                    {
                        UserField    uf = (UserField)n2.Entry.ReflectionObject;
                        FieldBuilder fb = tb.DefineField(uf.Name, tb, FieldAttributes.Literal | FieldAttributes.Static | FieldAttributes.Public);
                        n2.Entry.ReflectionObject = fb;
                        fb.SetConstant(enumCount);
                        enumCount++;
                    }
                }
                if (tb.IsClass && !tb.IsEnum && !hasConstructor)
                {
                    hasConstructors[tb] = customType;
                }
            }

            foreach (TypeBuilder t in allTypes)
            {
                if (hasConstructors.ContainsKey(t))
                {
                    UserConstructor uc    = (UserConstructor)hasConstructors[t].GetConstructor(new Type[] { });
                    UserType        baset = FindUserType(hasConstructors[t].BaseType);
                    ConstructorInfo baseConstructor;
                    if (baset == null)
                    {
                        baseConstructor = hasConstructors[t].BaseType.GetConstructor(new Type[] { });
                    }
                    else
                    {
                        baseConstructor = baset.GetConstructor(new Type[] { });
                        baseConstructor = (ConstructorInfo)((UserConstructor)baseConstructor).Entry.ReflectionObject;
                    }
                    ConstructorBuilder cb = DefineConstructor(t,
                                                              MethodAttributes.Public | MethodAttributes.HideBySig |
                                                              MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
                                                              new ParameterInfo[] {});
                    ILGenerator ig = cb.GetILGenerator();
                    ig.Emit(OpCodes.Call, baseConstructor);
                    ig.Emit(OpCodes.Ret);
                    uc.Entry.ReflectionObject = cb;
                }
            }
        }