首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

How to get a type from an unreferenced assembly?

要从未引用的程序集中获取类型,可以使用反射来实现。以下是一个简单的示例,展示了如何从未引用的程序集中获取类型:

  1. 加载程序集:
代码语言:csharp
复制
Assembly assembly = Assembly.LoadFrom("path_to_assembly.dll");
  1. 获取程序集中的类型:
代码语言:csharp
复制
Type[] types = assembly.GetTypes();
  1. 遍历类型并找到所需的类型:
代码语言:csharp
复制
Type targetType = null;
foreach (Type type in types)
{
    if (type.FullName == "Full.Name.Of.Target.Type")
    {
        targetType = type;
        break;
    }
}
  1. 使用反射创建类型的实例:
代码语言:csharp
复制
object instance = Activator.CreateInstance(targetType);

这样,您就可以使用反射来获取未引用程序集中的类型,并创建它们的实例。请注意,这种方法可能会导致类型安全问题,因此请谨慎使用。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 仅反射加载(ReflectionOnlyLoadFrom)的 .NET 程序集,如何反射获取它的 Attribute 元数据呢?

    var extensionFilePath = @"C:\Users\walterlv\Desktop\Walterlv.Extension.dll"; var assembly = Assembly.ReflectionOnlyLoadFrom...CustomAttributeData 中有 AttributeType 属性,虽然此属性是 Type 类型的,但是实际上它只会是 RuntimeType 类型,而不会是真实的 Attribute 的类型...---- 参考资料 CustomAttributeData Class (System.Reflection) - Microsoft Docs c# - How to get custom attributes...from an assembly that is not (really) loaded - Stack Overflow c# - Get custom attribute data from assembly...file and unlock it afterwise - Stack Overflow 本文会经常更新,请阅读原文: https://blog.walterlv.com/post/get-attributes-for-reflection-only-loaded-assembly.html

    2.3K30
    领券