msos.CommandExecutionContext.GetTypeByMetadataToken C# (CSharp) Méthode

GetTypeByMetadataToken() public méthode

public GetTypeByMetadataToken ( string moduleName, int mdTypeDefToken ) : ClrType
moduleName string
mdTypeDefToken int
Résultat ClrType
        public ClrType GetTypeByMetadataToken(string moduleName, int mdTypeDefToken)
        {
            // The metadata token is not unique, and it repeats across modules. Turns out
            // sometimes it even repeats in the same module: all closed generic types share
            // the same metadata token, and it's also the same token as the corresponding
            // open generic type.
            if (_typesByModuleAndMDToken == null)
            {
                _typesByModuleAndMDToken = new Dictionary<Tuple<string, int>, List<ClrType>>();
                foreach (var type in Heap.EnumerateTypes())
                {
                    List<ClrType> list;
                    var key = new Tuple<string, int>(
                        Path.GetFileNameWithoutExtension(type.Module.Name),
                        (int)type.MetadataToken);
                    if (!_typesByModuleAndMDToken.TryGetValue(key, out list))
                    {
                        list = new List<ClrType>();
                        _typesByModuleAndMDToken.Add(key, list);
                    }
                    list.Add(type);
                }
            }

            List<ClrType> candidates;
            _typesByModuleAndMDToken.TryGetValue(
                new Tuple<string, int>(moduleName, mdTypeDefToken), out candidates);

            if (candidates == null || candidates.Count != 1)
                return null; // We don't know which one to pick

            return candidates[0];
        }

Usage Example

            private void TryGetTypeByMetadataToken(ArgumentOrLocal argOrLocal, IXCLRDataTypeInstance typeInstance)
            {
                object tmp;

                if (typeInstance.GetDefinition(out tmp) != HR.S_OK)
                {
                    return;
                }

                IXCLRDataTypeDefinition typeDefinition = (IXCLRDataTypeDefinition)tmp;
                int typeTok;

                if (HR.S_OK == typeDefinition.GetTokenAndScope(out typeTok, out tmp))
                {
                    IXCLRDataModule module = (IXCLRDataModule)tmp;
                    argOrLocal.ClrType = _context.GetTypeByMetadataToken(
                        GetModuleName(module), typeTok);
                    // This might fail if we don't have that type cached (unlikely)
                    // or if the type is generic, which makes the token non-unique.
                }
            }