使用反射动态查询程序集

发布于 2024-11-30 14:24:21 字数 234 浏览 0 评论 0原文

我在动态使用反射时遇到困难,例如。查询 .exe 文件,而不需要为我希望查询的每个程序集添加引用。

例如,下面的代码是获取一个类然后进行检查的常规方法。

AssemblyName assembly_name = new AssemblyName( "Name" ); 

问题不在于将参数添加到代码中,而是代码需要直接引用新程序集进行检查。

欢迎任何建议。

I am having difficulty with using reflections dynamically eg. query a .exe file without requiring a reference to be added for every assembly which I wish to query against.

So for instance, the code below is the regular way to get a hold of a class to then be checked.

AssemblyName assembly_name = new AssemblyName( "Name" ); 

The issue is not adding the argument in to the code but the code requirng direct reference to the new assembly to check against.

Any suggestions are welcome.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2024-12-07 14:24:21

听起来您实际上只是想在执行时加载程序集。查看 Assembly.Load< /a> 和 Assembly.ReflectionOnlyLoad

It sounds like you're really just trying to load an assembly at execution time. Look at Assembly.Load and Assembly.ReflectionOnlyLoad.

望喜 2024-12-07 14:24:21

也许您正在寻找类似 Cecil 的内容。它是一个库(可在 Windows 和其他平台上使用),允许查询元数据而无需解析所有引用。

Maybe you're looking for something like Cecil. It's a library (available on Windows and other platforms) that allows to query metadata without the need to resolve all references.

习ぎ惯性依靠 2024-12-07 14:24:21

我不太确定“查询”是什么意思。如果您想知道如何使用反射从程序集创建实例,这里有一个示例:

// From within the current assembly
public CartesianType CreateInstance(string fullyQualifiedClassName)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    Type target = assembly.GetType(fullyQualifiedClassName, true, true);

    return (CartesianType)Activator.CreateInstance(target);
}

// From an external assembly already referenced in your project
public SomeClass CreateInstance(string fullyQualifiedClassName)
{
    Assembly assembly = Assembly.GetAssembly(typeof(SomeClass));
    Type target = assembly.GetType(fullyQualifiedClassName, true, true);

    return (SomeClass)Activator.CreateInstance(target);
}

所有其他方法必须使用 LoadLoadFileLoadFrom等

I'm not really sure what you mean by "query". If you want to know how to create an instance from an assembly using reflection, here is an example:

// From within the current assembly
public CartesianType CreateInstance(string fullyQualifiedClassName)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    Type target = assembly.GetType(fullyQualifiedClassName, true, true);

    return (CartesianType)Activator.CreateInstance(target);
}

// From an external assembly already referenced in your project
public SomeClass CreateInstance(string fullyQualifiedClassName)
{
    Assembly assembly = Assembly.GetAssembly(typeof(SomeClass));
    Type target = assembly.GetType(fullyQualifiedClassName, true, true);

    return (SomeClass)Activator.CreateInstance(target);
}

All other methods must use Load or LoadFile, LoadFrom etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文