コード例 #1
0
        private EntityHandle ResolveMethodReference(MethodInfo method)
        {
            if (!IsReferencedType(method.DeclaringType))
            {
                throw new ArgumentException(
                          $"Method of a reference type is expected: {MetadataHelper.GetFriendlyName(method)}",
                          nameof(method));
            }

            // Constructed generic method?
            if (method.IsConstructedGenericMethod())
            {
                // Already created?
                if (_methodSpecHandles.TryGetValue(method, out var handle))
                {
                    return(handle);
                }

                // Get the definition handle
                var definition       = method.GetGenericMethodDefinition();
                var definitionHandle = ResolveMethodReference(definition);

                // Create method spec encoder
                var enc = new BlobEncoder(new BlobBuilder()).MethodSpecificationSignature(definition.GetGenericArguments().Length);
                foreach (var a in method.GetGenericArguments())
                {
                    enc.AddArgument().FromSystemType(a, this);
                }

                // Add method spec
                var spec = Builder.AddMethodSpecification(definitionHandle, GetOrAddBlob(enc.Builder));
                _methodSpecHandles.Add(method, spec);
                return(spec);
            }
            else
            {
                // Already created?
                if (_methodRefHandles.TryGetValue(method, out var handle))
                {
                    return(handle);
                }

                var typeRef   = ResolveTypeReference(method.DeclaringType);
                var methodRef = Builder.AddMemberReference(typeRef, GetOrAddString(method.Name),
                                                           GetMethodOrConstructorSignature(method));
                _methodRefHandles.Add(method, methodRef);
                return(methodRef);
            }
        }