public SharpAssemblyField(SharpAssembly_ assembly, Field[] fieldTable, SharpAssemblyClass declaringtype, uint index)
        {
            if (assembly == null) {
                throw new System.ArgumentNullException("assembly");
            }
            if (fieldTable == null) {
                throw new System.ArgumentNullException("fieldTable");
            }
            if (declaringtype == null) {
                throw new System.ArgumentNullException("declaringtype");
            }
            if (index > fieldTable.GetUpperBound(0) || index < 0) {
                throw new System.ArgumentOutOfRangeException("index", index, String.Format("must be between 1 and {0}!", fieldTable.GetUpperBound(0)));
            }

            declaringType = declaringtype;

            Field field = fieldTable[index];
            string name = assembly.Reader.GetStringFromHeap(field.Name);
            FullyQualifiedName = String.Concat(declaringType.FullyQualifiedName, ".", name);

            // Attributes
            ArrayList attrib = assembly.Attributes.Field[index] as ArrayList;
            if (attrib == null) goto noatt;

            AbstractAttributeSection sect = new AbstractAttributeSection();

            foreach(SharpCustomAttribute customattribute in attrib) {
                sect.Attributes.Add(new SharpAssemblyAttribute(assembly, customattribute));
            }

            attributes.Add(sect);

            noatt:

            if (field.IsFlagSet(Field.FLAG_INITONLY)) {
                modifiers |= ModifierEnum.Readonly;
            }

            if (field.IsFlagSet(Field.FLAG_STATIC)) {
                modifiers |= ModifierEnum.Static;
            }

            if (field.IsMaskedFlagSet(Field.FLAG_PRIVATE, Field.FLAG_FIELDACCESSMASK)) { // I assume that private is used most and public last (at least should be)
                modifiers |= ModifierEnum.Private;
            } else if (field.IsMaskedFlagSet(Field.FLAG_FAMILY, Field.FLAG_FIELDACCESSMASK)) {
                modifiers |= ModifierEnum.Protected;
            } else if (field.IsMaskedFlagSet(Field.FLAG_PUBLIC, Field.FLAG_FIELDACCESSMASK)) {
                modifiers |= ModifierEnum.Public;
            } else if (field.IsMaskedFlagSet(Field.FLAG_ASSEMBLY, Field.FLAG_FIELDACCESSMASK)) {
                modifiers |= ModifierEnum.Internal;
            } else if (field.IsMaskedFlagSet(Field.FLAG_FAMORASSEM, Field.FLAG_FIELDACCESSMASK)) {
                modifiers |= ModifierEnum.ProtectedOrInternal;
            } else if (field.IsMaskedFlagSet(Field.FLAG_FAMANDASSEM, Field.FLAG_FIELDACCESSMASK)) {
                modifiers |= ModifierEnum.Protected;
                modifiers |= ModifierEnum.Internal;
            }

            if (field.IsFlagSet(Field.FLAG_LITERAL)) {
                modifiers |= ModifierEnum.Const;
            }

            if (field.IsFlagSet(Field.FLAG_SPECIALNAME)) {
                modifiers |= ModifierEnum.SpecialName;
            }

            // field return type
            uint sigOffset = field.Signature;
            assembly.Reader.LoadBlob(ref sigOffset);
            sigOffset++;  // skip field id
            returnType = new SharpAssemblyReturnType(assembly, ref sigOffset);

            // field constant value -- for enums
            Constant cst = (Constant)assembly.FieldConstantTable[index];
            if (declaringtype.ClassType == ClassType.Enum && cst != null) {
                try {
                    DataType dt = (DataType)cst.Type;

                    byte[] blob = assembly.Reader.GetBlobFromHeap(cst.Val);
                    BinaryReader binReader = new BinaryReader(new MemoryStream(blob));

                    switch (dt) {
                        case DataType.Byte:
                            initialValue = binReader.ReadByte();
                            break;
                        case DataType.Int16:
                            initialValue = binReader.ReadInt16();
                            break;
                        case DataType.Int32:
                            initialValue = binReader.ReadInt32();
                            break;
                        case DataType.Int64:
                            initialValue = binReader.ReadInt64();
                            break;
                        case DataType.SByte:
                            initialValue = binReader.ReadSByte();
                            break;
                        case DataType.UInt16:
                            initialValue = binReader.ReadUInt16();
                            break;
                        case DataType.UInt32:
                            initialValue = binReader.ReadUInt32();
                            break;
                        case DataType.UInt64:
                            initialValue = binReader.ReadUInt64();
                            break;
                        default: // not supported
                            break;
                    }
                    binReader.Close();
                } catch {
                    Runtime.LoggingService.Info("SharpAssemblyField: Error reading constant value");
                }
            }
        }
        /// <summary>
        /// The constructor is private because the only way to construct SharpAssemblyClass objects
        /// is to call FromTypeRef/Def to make us of the cache
        /// </summary>
        private SharpAssemblyClass(SharpAssembly_ assembly, TypeDef[] typeDefTable, uint index)
        {
            if (assembly == null) {
                throw new System.ArgumentNullException("assembly");
            }
            if (typeDefTable == null) {
                throw new System.ArgumentNullException("typeDefTable");
            }
            if (index > typeDefTable.GetUpperBound(0) || index < 1) {
                throw new System.ArgumentOutOfRangeException("index", index, String.Format("must be between 1 and {0}!", typeDefTable.GetUpperBound(0)));
            }

            TypeDef typeDef = typeDefTable[index];
            typeDefIndex = index;  // store index for use in LoadMembers()

            declaredIn = assembly;

            FullyQualifiedName = GetNestedName(assembly, typeDefTable, index);

            // store in assembly's cache
            assembly.TypeDefObjects[index] = this;

            if (typeDef.IsFlagSet(TypeDef.FLAG_INTERFACE)) {
                classType = ClassType.Interface;
            } else if (typeDef.IsFlagSet(TypeDef.FLAG_CLASS)) {
                classType = ClassType.Class;
            }

            if (typeDef.Extends == 0) goto noext;

            SharpAssemblyClass extend = GetTypeRefOrDefClass(assembly, typeDef.Extends);

            if (extend == null) goto noext;

            if (extend.FullyQualifiedName == "System.Enum") {
                classType = ClassType.Enum;
            } else if (extend.FullyQualifiedName == "System.ValueType") {
                classType = ClassType.Struct;
            }

            baseTypeCollection.Add(extend);

            if (IsSubclassOf("System.Delegate")) classType = ClassType.Delegate;

            noext:

            InterfaceImpl[] ifaces = assembly.Tables.InterfaceImpl;
            if (ifaces == null) goto nointerfaces;

            for (int i = 1; i <= ifaces.GetUpperBound(0); ++i) {
                if (ifaces[i].Class == index) {
                    SharpAssemblyClass impl = GetTypeRefOrDefClass(assembly, ifaces[i].Interface);
                    if (impl != null) {
                        baseTypeCollection.Add(impl);
                    }
                }
            }

            nointerfaces:

            NestedClass[] nestedClasses = assembly.Tables.NestedClass;
            if (nestedClasses == null) goto nonested;

            for (int i = 1; i <= nestedClasses.GetUpperBound(0); ++i) {
                if (nestedClasses[i].EnclosingClass == index) {
                    IClass newclass = FromTypeDef(assembly, nestedClasses[i].NestedClassIndex);
                    innerClasses.Add(newclass);
                }
            }

            nonested:

            // Attributes
            ArrayList attrib = assembly.Attributes.TypeDef[index] as ArrayList;
            if (attrib == null) goto modifiers;

            AbstractAttributeSection sect = new AbstractAttributeSection();

            foreach(SharpCustomAttribute customattribute in attrib) {
                sect.Attributes.Add(new SharpAssemblyAttribute(assembly, customattribute));
            }

            attributes.Add(sect);

            modifiers:

            modifiers = ModifierEnum.None;

            if (typeDef.IsFlagSet(TypeDef.FLAG_SEALED)) {
                modifiers |= ModifierEnum.Sealed;
            }

            if (typeDef.IsFlagSet(TypeDef.FLAG_ABSTRACT)) {
                modifiers |= ModifierEnum.Abstract;
            }

            if (typeDef.IsMaskedFlagSet(TypeDef.FLAG_NESTEDPRIVATE, TypeDef.FLAG_VISIBILITYMASK)) {
                modifiers |= ModifierEnum.Private;
            } else if (typeDef.IsMaskedFlagSet(TypeDef.FLAG_NESTEDPUBLIC, TypeDef.FLAG_VISIBILITYMASK) || typeDef.IsMaskedFlagSet(TypeDef.FLAG_PUBLIC, TypeDef.FLAG_VISIBILITYMASK)) {
                modifiers |= ModifierEnum.Public;
            } else if (typeDef.IsMaskedFlagSet(TypeDef.FLAG_NESTEDASSEMBLY, TypeDef.FLAG_VISIBILITYMASK) ||
                typeDef.IsMaskedFlagSet(TypeDef.FLAG_NOTPUBLIC, TypeDef.FLAG_VISIBILITYMASK)) {
                modifiers |= ModifierEnum.Internal;
            } else if (typeDef.IsMaskedFlagSet(TypeDef.FLAG_NESTEDFAMILY, TypeDef.FLAG_VISIBILITYMASK)) {
                modifiers |= ModifierEnum.Protected;
            } else if (typeDef.IsMaskedFlagSet(TypeDef.FLAG_NESTEDFAMORASSEM, TypeDef.FLAG_VISIBILITYMASK)) {
                modifiers |= ModifierEnum.ProtectedOrInternal;
            } else if (typeDef.IsMaskedFlagSet(TypeDef.FLAG_NESTEDFAMANDASSEM, TypeDef.FLAG_VISIBILITYMASK)) {
                modifiers |= ModifierEnum.Protected;
                modifiers |= ModifierEnum.Internal;
            }

            if (typeDef.IsFlagSet(TypeDef.FLAG_SPECIALNAME)) {
                modifiers |= ModifierEnum.SpecialName;
            }

            /* members are loaded on demand now
            if (classType != ClassType.Delegate && loadMembers) {
                AddMethods(assembly, typeDefTable, index);
                AddFields(assembly, typeDefTable, index);
                AddProperties(assembly, typeDefTable, index);
                AddEvents(assembly, typeDefTable, index);

                membersLoaded = true;
            }
            */
        }
        public SharpAssemblyProperty(SharpAssembly_ asm, Property[] propertyTable, SharpAssemblyClass declaringtype, uint index)
        {
            if (asm == null) {
                throw new System.ArgumentNullException("asm");
            }
            if (propertyTable == null) {
                throw new System.ArgumentNullException("propertyTable");
            }
            if (declaringtype == null) {
                throw new System.ArgumentNullException("declaringType");
            }
            if (index > propertyTable.GetUpperBound(0) || index < 1) {
                throw new System.ArgumentOutOfRangeException("index", index, String.Format("must be between 1 and {0}!", propertyTable.GetUpperBound(0)));
            }

            AssemblyReader assembly = asm.Reader;
            declaringType = declaringtype;

            Property property = asm.Tables.Property[index];
            string name = assembly.GetStringFromHeap(property.Name);
            FullyQualifiedName = String.Concat(declaringType.FullyQualifiedName, ".", name);

            MethodSemantics[] sem = (MethodSemantics[])assembly.MetadataTable.Tables[MethodSemantics.TABLE_ID];
            Method[] method       = (Method[])assembly.MetadataTable.Tables[Method.TABLE_ID];

            uint getterMethodIndex = 0; // used later for parameters

            if (sem == null) goto nosem;

            for (int i = 1; i <= sem.GetUpperBound(0); ++i) {
                uint table = sem[i].Association & 1;
                uint ident = sem[i].Association >> 1;

                if (table == 1 && ident == index) {  // table: Property
                    modifiers = ModifierEnum.None;
                    Method methodDef = method[sem[i].Method];

                    if (methodDef.IsFlagSet(Method.FLAG_STATIC)) {
                        modifiers |= ModifierEnum.Static;
                    }

                    if (methodDef.IsFlagSet(Method.FLAG_ABSTRACT)) {
                        modifiers |= ModifierEnum.Abstract;
                    }

                    if (methodDef.IsFlagSet(Method.FLAG_VIRTUAL)) {
                        modifiers |= ModifierEnum.Virtual;
                    }

                    if (methodDef.IsFlagSet(Method.FLAG_FINAL)) {
                        modifiers |= ModifierEnum.Final;
                    }

                    if (methodDef.IsMaskedFlagSet(Method.FLAG_PRIVATE, Method.FLAG_MEMBERACCESSMASK)) { // I assume that private is used most and public last (at least should be)
                        modifiers |= ModifierEnum.Private;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMILY, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Protected;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_PUBLIC, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Public;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_ASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Internal;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMORASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.ProtectedOrInternal;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMANDASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Protected;
                        modifiers |= ModifierEnum.Internal;
                    }

                    if ((sem[i].Semantics & MethodSemantics.SEM_GETTER) == MethodSemantics.SEM_GETTER) {
                        getterRegion = new DefaultRegion(0, 0, 0, 0);
                        getterMethod = new SharpAssemblyMethod(asm, method, declaringtype, sem[i].Method);
                        getterMethodIndex = sem[i].Method;
                    }

                    if ((sem[i].Semantics & MethodSemantics.SEM_SETTER) == MethodSemantics.SEM_SETTER) {
                        setterRegion = new DefaultRegion(0, 0, 0, 0);
                        setterMethod = new SharpAssemblyMethod(asm, method, declaringtype, sem[i].Method);
                    }
                }

            }

            nosem:

            // Attributes
            ArrayList attrib = asm.Attributes.Property[index] as ArrayList;
            if (attrib == null) goto noatt;

            AbstractAttributeSection sect = new AbstractAttributeSection();

            foreach(SharpCustomAttribute customattribute in attrib) {
                sect.Attributes.Add(new SharpAssemblyAttribute(asm, customattribute));
            }

            attributes.Add(sect);

            noatt:

            if ((property.Flags & Property.FLAG_SPECIALNAME) == Property.FLAG_SPECIALNAME) modifiers |= ModifierEnum.SpecialName;

            uint offset = property.Type;
            assembly.LoadBlob(ref offset);
            offset += 1; // skip calling convention
            int paramCount = assembly.LoadBlob(ref offset);

            returnType = new SharpAssemblyReturnType(asm, ref offset);

            IReturnType[] returnTypes = new IReturnType[paramCount];
            for (int i = 0; i < returnTypes.Length; ++i) {
                returnTypes[i] = new SharpAssemblyReturnType(asm, ref offset);
            }

            if (getterMethodIndex != 0) {
                AddParameters(asm, asm.Tables.Method, getterMethodIndex, returnTypes);
            } else {
                AddParameters(asm, returnTypes);
            }
        }
        public SharpAssemblyMethod(SharpAssembly_ asm, Method[] methodTable, SharpAssemblyClass declaringtype, uint index)
        {
            if (asm == null) {
                throw new System.ArgumentNullException("asm");
            }
            if (methodTable == null) {
                throw new System.ArgumentNullException("methodTable");
            }
            if (declaringtype == null) {
                throw new System.ArgumentNullException("declaringType");
            }
            if (index > methodTable.GetUpperBound(0) || index < 1) {
                throw new System.ArgumentOutOfRangeException("index", index, String.Format("must be between 1 and {0}!", methodTable.GetUpperBound(0)));
            }
            AssemblyReader assembly = asm.Reader;

            declaringType = declaringtype;

            Method methodDef = methodTable[index];
            string name = assembly.GetStringFromHeap(methodDef.Name);

            FullyQualifiedName = String.Concat(declaringType.FullyQualifiedName, ".", name);

            // Attributes
            ArrayList attrib = asm.Attributes.Method[index] as ArrayList;
            if (attrib == null) goto noatt;

            AbstractAttributeSection sect = new AbstractAttributeSection();

            foreach(SharpCustomAttribute customattribute in attrib) {
                sect.Attributes.Add(new SharpAssemblyAttribute(asm, customattribute));
            }

            attributes.Add(sect);

            noatt:

            modifiers = ModifierEnum.None;

            if (methodDef.IsFlagSet(Method.FLAG_STATIC)) {
                modifiers |= ModifierEnum.Static;
            }

            if (methodDef.IsMaskedFlagSet(Method.FLAG_PRIVATE, Method.FLAG_MEMBERACCESSMASK)) {
                modifiers |= ModifierEnum.Private;
            } else if (methodDef.IsMaskedFlagSet(Method.FLAG_PUBLIC, Method.FLAG_MEMBERACCESSMASK)) {
                modifiers |= ModifierEnum.Public;
            } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMILY, Method.FLAG_MEMBERACCESSMASK)) {
                modifiers |= ModifierEnum.Protected;
            } else if (methodDef.IsMaskedFlagSet(Method.FLAG_ASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                modifiers |= ModifierEnum.Internal;
            } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMORASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                modifiers |= ModifierEnum.ProtectedOrInternal;
            } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMANDASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                modifiers |= ModifierEnum.Protected;
                modifiers |= ModifierEnum.Internal;
            }

            if (methodDef.IsFlagSet(Method.FLAG_VIRTUAL)) {
                modifiers |= ModifierEnum.Virtual;
            }

            if (methodDef.IsFlagSet(Method.FLAG_FINAL)) {
                modifiers |= ModifierEnum.Final;
            }

            if (methodDef.IsFlagSet(Method.FLAG_ABSTRACT)) {
                modifiers |= ModifierEnum.Abstract;
            }

            if (methodDef.IsFlagSet(Method.FLAG_SPECIALNAME)) {
                modifiers |= ModifierEnum.SpecialName;
            }

            uint offset = methodDef.Signature;
            assembly.LoadBlob(ref offset);
            offset += 1;  // skip calling convention
            int numReturnTypes = assembly.LoadBlob(ref offset);

            returnType = new SharpAssemblyReturnType(asm, ref offset);

            IReturnType[] returnTypes = new IReturnType[numReturnTypes];
            for (int i = 0; i < returnTypes.Length; ++i) {
                returnTypes[i] = new SharpAssemblyReturnType(asm, ref offset);
            }

            AddParameters(asm, methodTable, index, returnTypes);
        }
        public SharpAssemblyEvent(SharpAssembly_ asm, Event[] eventTable, SharpAssemblyClass declaringtype, uint index)
        {
            if (asm == null) {
                throw new System.ArgumentNullException("asm");
            }
            if (eventTable == null) {
                throw new System.ArgumentNullException("eventTable");
            }
            if (declaringtype == null) {
                throw new System.ArgumentNullException("declaringtype");
            }
            if (index > eventTable.GetUpperBound(0) || index < 1) {
                throw new System.ArgumentOutOfRangeException("index", index, String.Format("must be between 1 and {0}!", eventTable.GetUpperBound(0)));
            }

            AssemblyReader assembly = asm.Reader;

            declaringType = declaringtype;

            Event evt = eventTable[index];
            string name = assembly.GetStringFromHeap(evt.Name);
            FullyQualifiedName = String.Concat(declaringType.FullyQualifiedName, ".", name);

            MethodSemantics[] sem = asm.Tables.MethodSemantics;
            Method[] method       = asm.Tables.Method;
            if (sem == null) goto nosem;

            for (int i = 1; i <= sem.GetUpperBound(0); ++i) {
                uint table = sem[i].Association & 1;
                uint ident = sem[i].Association >> 1;

                if (table == 0 && ident == index) {  // table: Event
                    modifiers = ModifierEnum.None;
                    Method methodDef = method[sem[i].Method];

                    if (methodDef.IsFlagSet(Method.FLAG_STATIC)) {
                        modifiers |= ModifierEnum.Static;
                    }

                    if (methodDef.IsMaskedFlagSet(Method.FLAG_PRIVATE, Method.FLAG_MEMBERACCESSMASK)) { // I assume that private is used most and public last (at least should be)
                        modifiers |= ModifierEnum.Private;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMILY, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Protected;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_PUBLIC, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Public;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_ASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Internal;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMORASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.ProtectedOrInternal;
                    } else if (methodDef.IsMaskedFlagSet(Method.FLAG_FAMANDASSEM, Method.FLAG_MEMBERACCESSMASK)) {
                        modifiers |= ModifierEnum.Protected;
                        modifiers |= ModifierEnum.Internal;
                    }

                    if ((sem[i].Semantics & MethodSemantics.SEM_ADDON) == MethodSemantics.SEM_ADDON) {
                        addMethod = new SharpAssemblyMethod(asm, method, declaringtype, sem[i].Method);
                    }

                    if ((sem[i].Semantics & MethodSemantics.SEM_REMOVEON) == MethodSemantics.SEM_REMOVEON) {
                        removeMethod = new SharpAssemblyMethod(asm, method, declaringtype, sem[i].Method);
                    }

                    if ((sem[i].Semantics & MethodSemantics.SEM_FIRE) == MethodSemantics.SEM_FIRE) {
                        raiseMethod = new SharpAssemblyMethod(asm, method, declaringtype, sem[i].Method);
                    }
                }

            }

            nosem:

            // Attributes
            ArrayList attrib = asm.Attributes.Event[index] as ArrayList;
            if (attrib == null) goto noatt;

            AbstractAttributeSection sect = new AbstractAttributeSection();

            foreach(SharpCustomAttribute customattribute in attrib) {
                sect.Attributes.Add(new SharpAssemblyAttribute(asm, customattribute));
            }

            attributes.Add(sect);

            noatt:

            if ((evt.EventFlags & Event.FLAG_SPECIALNAME) == Event.FLAG_SPECIALNAME) modifiers |= ModifierEnum.SpecialName;

            uint typtab = evt.EventType & 0x03;
            uint typid  = evt.EventType >> 2;

            if (typtab == 0) {        // TypeDef
                TypeDef[] typedef = (TypeDef[])assembly.MetadataTable.Tables[TypeDef.TABLE_ID];
                returnType = new SharpAssemblyReturnType(asm, typedef, typid);

            } else if (typtab == 1) { // TypeRef
                TypeRef[] typeref = (TypeRef[])assembly.MetadataTable.Tables[TypeRef.TABLE_ID];
                returnType = new SharpAssemblyReturnType(asm, typeref, typid);

            } else {                  // TypeSpec
                returnType = new SharpAssemblyReturnType("NOT_SUPPORTED");
                Runtime.LoggingService.Info("SharpAssemblyEvent: TypeSpec -- not supported");
            }
        }