如何从iassemblysymbol中获取所有接口实现?

发布于 2025-02-04 05:17:55 字数 2904 浏览 3 评论 0原文

我有一种方法可以从汇编中获取所有接口实现

public static class AssemblyExtensions
{
    /// <summary>
    /// Gets all implementations of interface or class from assembly
    /// </summary>
    /// <param name="assembly"></param>
    /// <param name="ofType">Interface or class type</param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static IEnumerable<Implementation> GetAllImplementations(this Assembly assembly, Type ofType)
    {
        var types = assembly.GetTypes()
            .Where(type => !type.IsAbstract && !type.IsGenericTypeDefinition);

        var implementations = new List<Implementation>();

        foreach (var type in types)
        {
            if (ofType.IsInterface)
            {
                var typeInterfaces = type.GetInterfaces();
                Type? matchingInterface = null;

                if (ofType.IsGenericType)
                {
                    matchingInterface = typeInterfaces
                        .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == ofType);

                }
                else
                {
                    matchingInterface = typeInterfaces
                        .FirstOrDefault(i => !i.IsGenericType && i == ofType);
                }

                if (matchingInterface != null)
                {
                    implementations.Add(
                        new Implementation(matchingInterface, type)
                    );
                }
            }
            else if (ofType.IsClass)
            {
                if (
                    ofType.IsGenericType &&
                    type.BaseType?.IsGenericType == true &&
                    type.BaseType?.GetGenericTypeDefinition() == ofType
                )
                {
                    implementations.Add(
                        new Implementation(type.BaseType, type)
                    );
                }
                else if (type.BaseType == ofType)
                {
                    implementations.Add(
                        new Implementation(type.BaseType, type)
                    );
                }
            }
            else
            {
                throw new Exception("ofType can only be interface or class");
            }
        }

        return implementations;
    }

     public record Implementation(Type BaseType, Type ImplementationType);
}

我可以这样使用:

assembly.GetAllImplementations(typeof(MyInterface<,>));

我需要得到类似的东西,但从iassemblysymbol - 例如,对于myinterface&lt的每个实现; t,k&gt;我想知道“ t”,“ k”及其名称空间,以及实现myinterface&lt; t,k&gt;的类型的名称和名称空间。我该怎么做?

I have a method to get all interface implementations from Assembly:

public static class AssemblyExtensions
{
    /// <summary>
    /// Gets all implementations of interface or class from assembly
    /// </summary>
    /// <param name="assembly"></param>
    /// <param name="ofType">Interface or class type</param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public static IEnumerable<Implementation> GetAllImplementations(this Assembly assembly, Type ofType)
    {
        var types = assembly.GetTypes()
            .Where(type => !type.IsAbstract && !type.IsGenericTypeDefinition);

        var implementations = new List<Implementation>();

        foreach (var type in types)
        {
            if (ofType.IsInterface)
            {
                var typeInterfaces = type.GetInterfaces();
                Type? matchingInterface = null;

                if (ofType.IsGenericType)
                {
                    matchingInterface = typeInterfaces
                        .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == ofType);

                }
                else
                {
                    matchingInterface = typeInterfaces
                        .FirstOrDefault(i => !i.IsGenericType && i == ofType);
                }

                if (matchingInterface != null)
                {
                    implementations.Add(
                        new Implementation(matchingInterface, type)
                    );
                }
            }
            else if (ofType.IsClass)
            {
                if (
                    ofType.IsGenericType &&
                    type.BaseType?.IsGenericType == true &&
                    type.BaseType?.GetGenericTypeDefinition() == ofType
                )
                {
                    implementations.Add(
                        new Implementation(type.BaseType, type)
                    );
                }
                else if (type.BaseType == ofType)
                {
                    implementations.Add(
                        new Implementation(type.BaseType, type)
                    );
                }
            }
            else
            {
                throw new Exception("ofType can only be interface or class");
            }
        }

        return implementations;
    }

     public record Implementation(Type BaseType, Type ImplementationType);
}

And I can use it like this:

assembly.GetAllImplementations(typeof(MyInterface<,>));

I need get something similar but from IAssemblySymbol - for example for each implementation of MyInterface<T,K> I want to know "T", "K" and their namespaces, and name and namespace of the type that implements MyInterface<T,K>. How I can do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文