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.