Exemplo n.º 1
0
        public uint FindTypeRef(string szTypeRef)
        {
            IntPtr hEnum = new IntPtr();
            uint   cDefs, maxcDefs = 10;

            uint[] typeRefs = new uint[maxcDefs];

            importer.EnumTypeRefs(ref hEnum, typeRefs, maxcDefs, out cDefs);
            while (cDefs > 0)
            {
                for (int i = 0; i < cDefs; i++)
                {
                    string name;
                    uint   resScope;
                    GetTypeRefProps(typeRefs[i], out name, out resScope);
                    if (name == szTypeRef)
                    {
                        importer.CloseEnum(hEnum);
                        return(typeRefs[i]);
                    }
                }
                importer.EnumTypeRefs(ref hEnum, typeRefs, maxcDefs, out cDefs);
            }
            importer.CloseEnum(hEnum);
            return(0);
        }
Exemplo n.º 2
0
        private void enumerateMethods()
        {
            if (_import == null)
            {
                return;
            }

            // Handle of enumeration
            uint enumHandle = 0;

            var theMethods = new uint[10];

            // get the methods
            uint methodCount = 0;

            _import.EnumMethods(ref enumHandle, _token, theMethods, Convert.ToUInt32(theMethods.Length), out methodCount);

            _methods = new List <MetadataMethod>();

            for (uint methodIndex = 0; methodIndex < methodCount; ++methodIndex)
            {
                _methods.Add(new MetadataMethod(_import, theMethods[methodIndex]));
            }

            _import.CloseEnum(enumHandle);
        }
Exemplo n.º 3
0
        void enumerateTypeDefinitions()
        {
            //Handle of the enumeration.
            uint enumHandle = 0;
            //We will read maximum 10 TypeDefs at once which will be stored in this array.
            var typeDefs = new uint[10];
            //Number of read TypeDefs.
            uint count;

            _import.EnumTypeDefs(ref enumHandle, typeDefs, Convert.ToUInt32(typeDefs.Length), out count);

            _types = new List <MetadataType>();
            //Continue reading TypeDef's while he typeDefs array contains any new TypeDef.
            while (count > 0)
            {
                for (uint typeDefsIndex = 0; typeDefsIndex < count; typeDefsIndex++)
                {
                    _types.Add(new MetadataType(_import, typeDefs[typeDefsIndex]));
                }

                _import.EnumTypeDefs(ref enumHandle, typeDefs, Convert.ToUInt32(typeDefs.Length), out count);
            }

            _import.CloseEnum(enumHandle);
        }
Exemplo n.º 4
0
        uint[] EnumerateTokens <T>(TokenEnumerator1 <T> tokenEnumerator, T parameter)
        {
            IntPtr enumerator = IntPtr.Zero;

            uint[] buffer = new uint[initialBufferSize];
            uint   fetched;

            tokenEnumerator(ref enumerator, parameter, buffer, (uint)buffer.Length, out fetched);
            if (fetched < buffer.Length)
            {
                // The tokens did fit the buffer
                Array.Resize(ref buffer, (int)fetched);
            }
            else
            {
                // The tokens did not fit the buffer -> Refetch
                uint actualCount;
                metaData.CountEnum(enumerator, out actualCount);
                if (actualCount > buffer.Length)
                {
                    buffer = new uint[actualCount];
                    metaData.ResetEnum(enumerator, 0);
                    tokenEnumerator(ref enumerator, parameter, buffer, (uint)buffer.Length, out fetched);
                }
            }
            metaData.CloseEnum(enumerator);
            return(buffer);
        }
Exemplo n.º 5
0
        /// <returns>MethodBody and MethodDeclaration tokens</returns>
        public IEnumerable <MethodImpl> EnumMethodImpls(uint typeDef)
        {
            IntPtr enumerator = IntPtr.Zero;

            while (true)
            {
                MethodImpl ret  = new MethodImpl();
                uint[]     body = new uint[1];
                uint[]     decl = new uint[1];
                uint       fetched;
                metaData.EnumMethodImpls(ref enumerator, typeDef, body, decl, 1, out fetched);
                if (fetched == 0)
                {
                    break;
                }
                ret.MethodBody = body[0];
                ret.MethodDecl = decl[0];
                yield return(ret);
            }
            metaData.CloseEnum(enumerator);
        }
Exemplo n.º 6
0
        public void GetMethodImpls(uint typeDefTok, out List <uint> bodyToks, out List <uint> declToks)
        {
            bodyToks = new List <uint>();
            declToks = new List <uint>();

            uint handle = 0;

            uint[] bodys = new uint[10];
            uint[] decls = new uint[10];
            uint   count = 0;

            while ((m_import.EnumMethodImpls(ref handle, typeDefTok, bodys, decls, 10, out count) != 0) && (count > 0))
            {
                for (int i = 0; i < count; i++)
                {
                    bodyToks.Add(bodys[i]);
                    declToks.Add(decls[i]);
                }
                m_import.EnumMethodImpls(ref handle, typeDefTok, bodys, decls, 10, out count);
            }
            m_import.CloseEnum(handle);
        }
Exemplo n.º 7
0
        public unsafe static uint[] GetPropertyTokens(IMetaDataImport mdi, uint token)
        {
            if (mdi == null)
            {
                return(new uint[0]);
            }
            IntPtr iter = IntPtr.Zero;

            try {
                uint cTokens;
                int  hr = mdi.EnumProperties(ref iter, token, IntPtr.Zero, 0, out cTokens);
                if (hr < 0)
                {
                    return(new uint[0]);
                }

                uint ulCount = 0;
                hr = mdi.CountEnum(iter, ref ulCount);
                if (hr < 0 || ulCount == 0)
                {
                    return(new uint[0]);
                }

                hr = mdi.ResetEnum(iter, 0);
                if (hr < 0)
                {
                    return(new uint[0]);
                }

                uint[] tokens = new uint[ulCount];

                fixed(uint *p = &tokens[0])
                hr = mdi.EnumProperties(ref iter, token, new IntPtr(p), (uint)tokens.Length, out cTokens);

                if (hr < 0)
                {
                    return(new uint[0]);
                }
                return(tokens);
            }
            finally {
                if (iter != IntPtr.Zero)
                {
                    mdi.CloseEnum(iter);
                }
            }
        }
Exemplo n.º 8
0
        private void enumerateCustomAttributes()
        {
            // Handle of enumeration
            uint enumHandle = 0;

            uint tkType = 0;

            uint[] customAttributes = new uint[10];
            uint   attributeCount   = 0;

            _import.EnumCustomAttributes(ref enumHandle, _token, tkType, customAttributes, Convert.ToUInt32(customAttributes.Length), out attributeCount);

            _attributes = new List <MetadataCustomAttribute>();

            for (uint attributeIndex = 0; attributeIndex < attributeCount; ++attributeIndex)
            {
                _attributes.Add(new MetadataCustomAttribute(_import, customAttributes[attributeIndex], _token));
            }

            _import.CloseEnum(enumHandle);
        }
Exemplo n.º 9
0
		public unsafe static uint[] GetPermissionSetTokens(IMetaDataImport mdi, uint token) {
			if (mdi == null)
				return new uint[0];
			IntPtr iter = IntPtr.Zero;
			try {
				uint cTokens;
				int hr = mdi.EnumPermissionSets(ref iter, token, 0, IntPtr.Zero, 0, out cTokens);
				if (hr < 0)
					return new uint[0];

				uint ulCount = 0;
				hr = mdi.CountEnum(iter, ref ulCount);
				if (hr < 0 || ulCount == 0)
					return new uint[0];

				hr = mdi.ResetEnum(iter, 0);
				if (hr < 0)
					return new uint[0];

				uint[] tokens = new uint[ulCount];
				fixed (uint* p = &tokens[0])
					hr = mdi.EnumPermissionSets(ref iter, token, 0, new IntPtr(p), (uint)tokens.Length, out cTokens);
				if (hr < 0)
					return new uint[0];
				return tokens;
			}
			finally {
				if (iter != IntPtr.Zero)
					mdi.CloseEnum(iter);
			}
		}
Exemplo n.º 10
0
		public unsafe static uint[] GetTypeDefTokens(IMetaDataImport mdi) {
			if (mdi == null)
				return new uint[0];

			IntPtr iter = IntPtr.Zero;
			try {
				uint count;
				int hr = mdi.EnumTypeDefs(ref iter, IntPtr.Zero, 0, out count);
				if (hr < 0)
					return new uint[0];

				uint ulCount = 0;
				hr = mdi.CountEnum(iter, ref ulCount);
				if (hr < 0)
					return new uint[0];

				hr = mdi.ResetEnum(iter, 0);
				if (hr < 0)
					return new uint[0];

				// The global type isn't included
				uint[] tokens = new uint[ulCount + 1];
				if (tokens.Length > 1) {
					fixed (uint* p = &tokens[1])
						hr = mdi.EnumTypeDefs(ref iter, new IntPtr(p), ulCount, out count);
				}
				if (hr < 0)
					return new uint[0];
				tokens[0] = 0x02000001;
				return tokens;
			}
			finally {
				if (iter != IntPtr.Zero)
					mdi.CloseEnum(iter);
			}
		}
Exemplo n.º 11
0
		public unsafe static MethodOverrideInfo[] GetMethodOverrides(IMetaDataImport mdi, uint token) {
			if (mdi == null)
				return new MethodOverrideInfo[0];
			IntPtr iter = IntPtr.Zero;
			try {
				uint cTokens;
				int hr = mdi.EnumMethodImpls(ref iter, token, IntPtr.Zero, IntPtr.Zero, 0, out cTokens);
				if (hr < 0)
					return new MethodOverrideInfo[0];

				uint ulCount = 0;
				hr = mdi.CountEnum(iter, ref ulCount);
				if (hr < 0 || ulCount == 0)
					return new MethodOverrideInfo[0];

				hr = mdi.ResetEnum(iter, 0);
				if (hr < 0)
					return new MethodOverrideInfo[0];

				uint[] bodyTokens = new uint[ulCount];
				uint[] declTokens = new uint[ulCount];
				fixed (uint* b = &bodyTokens[0]) {
					fixed (uint* d = &declTokens[0])
						hr = mdi.EnumMethodImpls(ref iter, token, new IntPtr(b), new IntPtr(d), (uint)bodyTokens.Length, out cTokens);
				}
				if (hr < 0)
					return new MethodOverrideInfo[0];
				var infos = new MethodOverrideInfo[ulCount];
				for (int i = 0; i < infos.Length; i++)
					infos[i] = new MethodOverrideInfo(bodyTokens[i], declTokens[i]);
				return infos;
			}
			finally {
				if (iter != IntPtr.Zero)
					mdi.CloseEnum(iter);
			}
		}
Exemplo n.º 12
0
 internal uint[] _GetFieldTokens(IMetaDataImport importer, ICorDebugClass iclass)
 {
     uint ctoken;
     iclass.GetToken(out ctoken);
     uint henum = 0;
     List<uint> fieldtoks = new List<uint>();
     try
     {
         uint[] aonefieldtok = new uint[1];
         for (; ; )
         {
             uint count;
             importer.EnumFields(ref henum, ctoken, aonefieldtok, 1, out count);
             if (1 != count)
             {
                 break;
             }
             fieldtoks.Add(aonefieldtok[0]);
         }
     }
     finally
     {
         importer.CloseEnum(henum);
     }
     return fieldtoks.ToArray();
 }
Exemplo n.º 13
0
            static void _PrintArgs(ICorDebugILFrame ilframe, IMetaDataImport importer, System.IO.TextWriter writer)
            {
                uint argcount;
                {
                    ICorDebugValueEnum ven;
                    ilframe.EnumerateArguments(out ven);
                    ven.GetCount(out argcount);
                }
                if (argcount < 1)
                {
                    return;
                }

                uint ftoken;
                ilframe.GetFunctionToken(out ftoken);

                System.Reflection.MethodAttributes fattribs;
                {
                    uint chMethod;
                    uint dwAttr;
                    IntPtr pvSigBlob;
                    uint cbSigBlob;
                    uint ulCodeRVA;
                    uint dwImplFlags;
                    uint ftypedeftoken;
                    int hrmethodprops = (int)importer.GetMethodProps(ftoken, out ftypedeftoken,
                        null, 0, out chMethod,
                        out dwAttr,
                        out pvSigBlob, out cbSigBlob,
                        out ulCodeRVA, out dwImplFlags);
                    Marshal.ThrowExceptionForHR(hrmethodprops);
                    char[] methodnamechars = new char[chMethod];
                    hrmethodprops = (int)importer.GetMethodProps(ftoken, out ftypedeftoken,
                        methodnamechars, (uint)methodnamechars.Length, out chMethod,
                        out dwAttr,
                        out pvSigBlob, out cbSigBlob,
                        out ulCodeRVA, out dwImplFlags);
                    Marshal.ThrowExceptionForHR(hrmethodprops);
                    chMethod--; // Remove nul.
                    //MethodName = new string(methodnamechars, 0, (int)chMethod);
                    fattribs = (System.Reflection.MethodAttributes)dwAttr;
                }

                List<uint> paramtoks = new List<uint>();
                {
                    uint henum = 0;
                    try
                    {
                        uint[] aoneparamtok = new uint[1];
                        for (; ; )
                        {
                            uint count;
                            importer.EnumParams(ref henum, ftoken, aoneparamtok, 1, out count);
                            if (1 != count)
                            {
                                break;
                            }
                            paramtoks.Add(aoneparamtok[0]);
                        }
                    }
                    finally
                    {
                        importer.CloseEnum(henum);
                    }
                }

                uint ia = 0;
                int ip = 0; //paramtoks.Count - (int)argcount;
                for (; ia < argcount; ia++)
                {
                    string argname;
                    if (0 == ia
                            && System.Reflection.MethodAttributes.Static != (fattribs & System.Reflection.MethodAttributes.Static))
                    {
                        argname = "this";
                    }
                    else
                    {
                        if (ip >= paramtoks.Count)
                        {
                            argname = "unnamed_param_" + (1 + ia).ToString();
                        }
                        else
                        {
                            {
                                uint ptok = paramtoks[ip++];
                                uint parenttok;
                                uint pulSequence;
                                uint argnamelen = 0;
                                uint dwAttr, dwCPlusTypeFlag, cchValue;
                                IntPtr pValue;
                                importer.GetParamProps(ptok, out parenttok, out pulSequence, null, argnamelen, out argnamelen,
                                    out dwAttr, out dwCPlusTypeFlag, out pValue, out cchValue);
                                char[] argnamebuf = new char[argnamelen];
                                importer.GetParamProps(ptok, out parenttok, out pulSequence, argnamebuf, argnamelen, out argnamelen,
                                    out dwAttr, out dwCPlusTypeFlag, out pValue, out cchValue);
                                argnamelen--; // Remove nul.
                                argname = new string(argnamebuf, 0, (int)argnamelen);
                            }
                        }
                    }
                    string argstr;
                    {
                        ICorDebugValue pvalue;
                        ilframe.GetArgument(ia, out pvalue);
                        argstr = ToString(pvalue);
                    }
                    writer.WriteLine("{0}={1}", argname, argstr);
                }
            }
Exemplo n.º 14
0
        internal static IEnumerable GetTypeRefNames(string assemblyLocation)
        {
            IMetaDataDispenser metaDataDispenser = new MetaDataDispenser() as IMetaDataDispenser;

            if (metaDataDispenser == null)
            {
                throw new InvalidOperationException(String.Format(SR.GetString(SR.Error_MetaDataInterfaceMissing), assemblyLocation, "IMetaDataDispenser"));
            }

            Guid   guidMetaDataImport = new Guid(Guids.IID_IMetaDataImport);
            object metaDataImportObj  = null;

            NativeMethods.ThrowOnFailure(metaDataDispenser.OpenScope(assemblyLocation, 0, ref guidMetaDataImport, out metaDataImportObj));
            IMetaDataImport metaDataImport = metaDataImportObj as IMetaDataImport;

            if (metaDataImport == null)
            {
                throw new InvalidOperationException(String.Format(SR.GetString(SR.Error_MetaDataInterfaceMissing), assemblyLocation, "IMetaDataImport"));
            }

            IntPtr enumHandle = new IntPtr();

            uint[] typeRefs     = new uint[20];
            uint   typeRefCount = 0;

            try
            {
                do
                {
                    NativeMethods.ThrowOnFailure((metaDataImport.EnumTypeRefs(ref enumHandle, typeRefs, (uint)typeRefs.Length, ref typeRefCount)));
                    for (int typeRefIndex = 0; typeRefIndex < typeRefCount; typeRefIndex++)
                    {
                        IntPtr typeRefNamePtr = IntPtr.Zero;
                        uint   typeRefNameLength;
                        uint   resolutionScopeToken;

                        NativeMethods.ThrowOnFailure(metaDataImport.GetTypeRefProps(typeRefs[typeRefIndex], out resolutionScopeToken, typeRefNamePtr, 0, out typeRefNameLength));
                        if (typeRefNameLength > 0)
                        {
                            string typeRefName = String.Empty;
                            typeRefNamePtr = Marshal.AllocCoTaskMem((int)(2 * (typeRefNameLength + 1)));
                            try
                            {
                                NativeMethods.ThrowOnFailure(metaDataImport.GetTypeRefProps(typeRefs[typeRefIndex], out resolutionScopeToken, typeRefNamePtr, typeRefNameLength, out typeRefNameLength));
                            }
                            finally
                            {
                                typeRefName = Marshal.PtrToStringUni(typeRefNamePtr);
                                Marshal.FreeCoTaskMem(typeRefNamePtr);
                            }

                            IMetaDataAssemblyImport metaDataAssemblyImport = metaDataImportObj as IMetaDataAssemblyImport;
                            if (metaDataAssemblyImport == null)
                            {
                                throw new InvalidOperationException(String.Format(SR.GetString(SR.Error_MetaDataInterfaceMissing), assemblyLocation, "IMetaDataAssemblyImport"));
                            }

                            if (TokenTypeFromToken(resolutionScopeToken) == MetadataTokenType.AssemblyRef)
                            {
                                AssemblyMetadata assemblyMetadata;
                                IntPtr           publicKeyOrToken = IntPtr.Zero;
                                uint             publicKeyOrTokenSize;
                                IntPtr           assemblyName = IntPtr.Zero;
                                uint             assemblyNameSize;
                                IntPtr           hashValueBlob = IntPtr.Zero;
                                uint             hashValueSize;
                                uint             assemblyRefFlags;

                                NativeMethods.ThrowOnFailure(metaDataAssemblyImport.GetAssemblyRefProps(resolutionScopeToken, out publicKeyOrToken, out publicKeyOrTokenSize, assemblyName, 0, out assemblyNameSize, out assemblyMetadata, out hashValueBlob, out hashValueSize, out assemblyRefFlags));

                                if (assemblyNameSize > 0)
                                {
                                    assemblyName = Marshal.AllocCoTaskMem((int)(2 * (assemblyNameSize + 1)));
                                }

                                if (assemblyMetadata.localeSize > 0)
                                {
                                    assemblyMetadata.locale = Marshal.AllocCoTaskMem((int)(2 * (assemblyMetadata.localeSize + 1)));
                                }

                                try
                                {
                                    if (assemblyNameSize > 0 || assemblyMetadata.localeSize > 0)
                                    {
                                        NativeMethods.ThrowOnFailure(metaDataAssemblyImport.GetAssemblyRefProps(resolutionScopeToken, out publicKeyOrToken, out publicKeyOrTokenSize, assemblyName, assemblyNameSize, out assemblyNameSize, out assemblyMetadata, out hashValueBlob, out hashValueSize, out assemblyRefFlags));
                                    }

                                    String publicKeyString = String.Empty;
                                    for (int pos = 0; pos < publicKeyOrTokenSize; pos++)
                                    {
                                        publicKeyString += String.Format("{0}", Marshal.ReadByte(publicKeyOrToken, pos).ToString("x2"));
                                    }

                                    yield return(String.Format("{0}, {1}, Version={2}.{3}.{4}.{5}, Culture={6}, PublicKeyToken={7}", typeRefName, Marshal.PtrToStringUni(assemblyName), assemblyMetadata.majorVersion, assemblyMetadata.minorVersion, assemblyMetadata.buildNumber, assemblyMetadata.revisionNumber, String.IsNullOrEmpty(Marshal.PtrToStringUni(assemblyMetadata.locale)) ? "neutral" : Marshal.PtrToStringUni(assemblyMetadata.locale), String.IsNullOrEmpty(publicKeyString) ? "null" : publicKeyString));
                                }
                                finally
                                {
                                    if (assemblyName != IntPtr.Zero && assemblyNameSize > 0)
                                    {
                                        Marshal.FreeCoTaskMem(assemblyName);
                                        assemblyName = IntPtr.Zero;
                                    }

                                    if (assemblyMetadata.locale != IntPtr.Zero && assemblyMetadata.localeSize > 0)
                                    {
                                        Marshal.FreeCoTaskMem(assemblyMetadata.locale);
                                        assemblyMetadata.locale = IntPtr.Zero;
                                    }
                                }
                            }
                        }
                    }
                }while (typeRefCount > 0);
            }
            finally
            {
                metaDataImport.CloseEnum(enumHandle);
            }
            yield break;
        }