Question

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

Was it helpful?

Solution

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.

OTHER TIPS

You can also try to modify T4VSHostProcess.exe to run as 64 bit process by running corflags T4VSHostProcess.exe /32BITREQ- /Force. But this modification will repair situation on PC where you made this modification. Also be sure what you are doing (backup, try outside of Program Files, etc.).

Backup your T4VSHostProcess.exe file. To avoid access rights problems, copy your T4VSHostProcess.exe process to some folder outside Program Files. Run the following command and then copy T4VSHostProcess.exe for original folder.

C:\Tools\T4>corflags T4VSHostProcess.exe /32BITREQ- /Force
Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.6.1590.0
Copyright (c) Microsoft Corporation.  All rights reserved.

corflags : warning CF011 : The specified file is strong name signed.  Using /Force will invalidate the signature of this image and will require the assembly to be resigned.

You can ignore warning from CorFlags tool.

You can make sure what flags has been set for T4VSHostProcess.exe by running corflags T4VSHostProcess.exe without options.

Before:

C:\Tools\T4>corflags T4VSHostProcess.exe
Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.6.1590.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 0xb
ILONLY    : 1
32BITREQ  : 1
32BITPREF : 0
Signed    : 1

After:

C:\Tools\T4>corflags T4VSHostProcess.exe
Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.6.1590.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 0x9
ILONLY    : 1
32BITREQ  : 0
32BITPREF : 0
Signed    : 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top