3

I want to output the types in a .winmd file given its path. I copied a winmd file from my Windows 8 Developer Preview machine to my dev machine. I wrote a small test app (in C#, .NET 4.0, not 4.5) which tries to load an assembly at run time, given its path, and outputs the types in it. Though the assembly was loaded, an exception occurred when I tried to get the types.

Here is the code:

static void Main(string[] args)
{
     if (args.Length != 1) return;

    var path = args[0];
    if (!System.IO.File.Exists(path))
    {
        Console.WriteLine("file not found : " + path);
        return;
    }

    var asm = System.Reflection.Assembly.LoadFrom(path);    // load successful.

    Console.WriteLine("loaded ");
    string name = asm.GetName().Name;
    Console.WriteLine(name);

    System.Type[] types = asm.GetTypes();   // exception occurs here

    foreach(var type in types)
    {
        // output type name
    }
}

The exception is of type ReflectionTypeLoadException. Its Message property is: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

The LoaderExceptions property that has the underlying exception, a TypeLoadException. Its Message property reads: Bad use of Runtime Impl attribute.

Anybody knows why I cannot read the types?

Thanks.

Note: I know I am using .NET 4.0. But, in .NET 4.5 (the one in the Windows 8 preview), I could not load an assembly from file at run time. There is no method in the Assembly class that does it.

pnvn
  • 488
  • 1
  • 5
  • 11

3 Answers3

4

While .winmd files use the ECMA 355 file format, they are NOT .Net assemblies, and it is highly unlikely that you'll be able to read the files using the .Net framework directly.

If you use the version of ILDASM shipped with the developer preview, you can view the types in the files visually. You can also use ildasm to dump a text file containing the types in the metadata file. I believe that .Net reflector can also do this.

If you DO need to enumerate types in a winmd file programmaticly, I suggest that you use the unmanaged metadata reading APIs. That's how we read metadata files for our development tools internally.

If you're running on the developer preview build, I'll suggest you look at the RoGetMetaDataFile API - this is the API used by the Chakra javascript engine to open the metadata file for a particular type.

sarvesh
  • 2,743
  • 1
  • 20
  • 30
Larry Osterman
  • 16,086
  • 32
  • 60
2

WinMDs are metadata only assemblies. Using ReflectionOnlyLoadFrom should do the trick.

The following code works.

        var assembly = System.Reflection.Assembly.ReflectionOnlyLoadFrom(winmdPath);


        foreach (var type in assembly.GetTypes())
        {
            Console.WriteLine("type found name = " + type.Name);
        }
0

To work with *.winmd files you must set in your project file inside PropertyGroup

<TargetPlatformVersion>8.0</TargetPlatformVersion>
Arman Hayots
  • 2,459
  • 6
  • 29
  • 53