如何从iassemblysymbol中获取所有接口实现?
我有一种方法可以从汇编中获取所有接口实现:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论