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

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

问题描述

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



反射问题是DLL是64位DLL并且如果我在T4模板中加载它,则会得到 BadImageFormatException ,因为我试图将64位DLL加载到32位进程中(Visual Studio 2012)。 / p>

有什么方法可以分析T4中该DLL的内容,最好使用反射吗?



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



BTW:该DLL是托管的C ++ DLL。因此Roslyn也不可行,因为它仅支持C#和VB。)

解决方案

值得测试的是,如果加载程序集反思只适合您。我做了一些实验,看来它成功地将64位程序集加载到32位进程中。它显然不能执行,但是如果我对您的理解正确,那应该没问题:



有关完整示例,请参见: https://github.com/mrange/CodeStack/tree/master/q18985529/Reflect

  var assembly = Assembly.ReflectionOnlyLoad( X64); 

var类型= assembly.GetTypes();

foreach(类型中的变量类型)
{
Console.WriteLine(type.FullName);

foreach(type.GetFields()中的var字段)
{
Console.WriteLine( {0} {1},field.FieldType,field.Name);
}

foreach(类型为.GetProperties()的var属性)
{
Console.WriteLine( {0} {1},property.PropertyType, property.Name);
}

}

加载ReflectionOnly有一些缺点但有时候值得。


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.

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).

Is there any way to analyse the contents of that DLL within T4, preferrably using reflection?

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...

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

解决方案

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:

For the full sample look at: 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);
    }

}

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

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

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