コード例 #1
0
        internal static bool HasReferenceToNuGetAssembly(MetadataReader metadata)
        {
            foreach (var assemblyReferenceHandle in metadata.AssemblyReferences)
            {
                var assemblyReference = metadata.GetAssemblyReference(assemblyReferenceHandle);

                if (NuGetAssembly.IsNuGetAssembly(assemblyReference))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
        internal static UsageInformation FindUsedNuGetApis(MetadataReader metadata)
        {
            var result = new UsageInformation();

            void AddMember(string assembly, string member)
            {
                if (!result.MemberReferences.TryGetValue(assembly, out HashSet <string>?apis))
                {
                    apis = new HashSet <string>();
                    result.MemberReferences[assembly] = apis;
                }

                apis.Add(member);
            }

            foreach (var memberReferenceHandle in metadata.MemberReferences)
            {
                var memberReference        = metadata.GetMemberReference(memberReferenceHandle);
                var foundAssemblyReference = TryFindAssemblyReference(memberReferenceHandle, metadata, out var assemblyReference);

                if (foundAssemblyReference && NuGetAssembly.IsNuGetAssembly(assemblyReference))
                {
                    var assemblyName = assemblyReference.GetAssemblyName();
                    var version      = assemblyName.Version?.ToString();
                    if (version != null)
                    {
                        result.Versions.Add(version);
                    }

                    var kind = memberReference.GetKind();
                    switch (kind)
                    {
                    case MemberReferenceKind.Method:
                    {
                        var(assembly, member) = TypeNameGenerator.GetFullName(memberReferenceHandle, metadata);
                        var sig             = memberReference.DecodeMethodSignature(MethodSignatureDecoder.Default, null);
                        var methodSignature = GetMethodSignature(member, sig);
                        AddMember(assembly, methodSignature);
                    }
                    break;

                    case MemberReferenceKind.Field:
                    {
                        var(assembly, member) = TypeNameGenerator.GetFullName(memberReferenceHandle, metadata);
                        AddMember(assembly, member);
                    }
                    break;

                    default:
                        throw new NotImplementedException(kind.ToString());
                    }
                }
            }

            if (result.MemberReferences.SelectMany(r => r.Value).Any())
            {
                if (TryGetTargetFramework(metadata, out string?targetFramework))
                {
                    result.TargetFrameworks.Add(targetFramework);
                }
            }

            return(result);
        }