使用反射从 Visual Studio(32 位)中的 T4 模板中分析 64 位 DLL [英] Analyze 64-bit DLL from within T4 template in Visual Studio (32-bit) using Reflection

查看:22
本文介绍了使用反射从 Visual Studio(32 位)中的 T4 模板中分析 64 位 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用反射从 T4 模板中分析 DLL,以便我可以根据反射分析的结果生成代码.我知道 EnvDTE 会是更好的选择,但由于多种原因,这在我的情况下是不可能的.

I would like to analyse a DLL from within a T4 template using Reflection, so that I can generate code based on the results of the reflection analysis. I know EnvDTE would be a better option, but this is not possible in my case for several reasons.

反射的问题是 DLL 是 64 位 DLL,如果我在 T4 模板中加载它,我会得到一个 BadImageFormatException 因为我试图将 64 位 DLL 加载到32 位进程 (Visual Studio 2012).

The problem with reflection is that the DLL is a 64-bit DLL and if I load that within the T4 template I get a BadImageFormatException because I am trying to load a 64-bit DLL into a 32-bit process (Visual Studio 2012).

有没有办法在 T4 中分析那个 DLL 的内容,最好使用反射?

我已经考虑过编写一个控制台应用程序来分析 DLL,将结果写入一个 XML 文件,然后由 T4 模板使用,但这并不是我最喜欢的解决方案......

I have already thought about writing a console application which analyses the DLL, writes the results to an XML file which is then consumed by the T4 template, but that is not really my favorite solution...

顺便说一句:DLL 是托管的 C++ DLL.所以 Roslyn 也没有选择,因为它只支持 C# 和 VB).

BTW: The DLL is a managed C++ DLL. So Roslyn is no option either because it only supports C# and VB).

推荐答案

一个值得测试的事情是,如果加载程序集进行反射只适合你.我做了一些实验,然后似乎成功地将 64 位程序集加载到 32 位进程中.它显然无法执行,但如果我理解正确的话,那应该没问题:

A thing worth testing is that if loading an assembly for reflection only works for you. I did experiment a bit and it seems it succeeds loading a 64bit assembly into a 32bit process then. It can't execute obviously but that should be ok for you if I understood you correctly:

查看完整示例:https://github.com/mrange/CodeStack/tree/master/q18985529/Reflect

var assembly = Assembly.ReflectionOnlyLoad ("X64");

var types = assembly.GetTypes ();

foreach (var type in types)
{
    Console.WriteLine (type.FullName);

    foreach (var field in type.GetFields ())
    {
        Console.WriteLine ("  {0} {1}", field.FieldType, field.Name);
    }

    foreach (var property in type.GetProperties ())
    {
        Console.WriteLine ("  {0} {1}", property.PropertyType, property.Name);
    }

}

为 ReflectionOnly 加载有一些缺点 IIRC 但有时它是值得的.

Loading for ReflectionOnly has some drawbacks IIRC but sometimes it's worth it.

这篇关于使用反射从 Visual Studio(32 位)中的 T4 模板中分析 64 位 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆