W
Wiktor Zychla
Hello,
I still have some problems with custom attributes and I ask someone for
a helpful hand.
In the appendix is the trivial ILAsm code with single custom attribute. The
code compiles OK but then I cannot use reflection to load the assembly to
read custom attributes because following code surprisingly fails in (*)
line:
public static void PrintAttributes( Assembly a )
{
foreach ( Type t in a.GetTypes() ) // *
foreach ( MethodInfo m in t.GetMethods() )
foreach ( object o in m.GetCustomAttributes(false) )
{
Console.WriteLine( "{0}\r\n\t{1}\r\n\t\t{2}", t, m, o );
}
}
with following exception:
Exception caught:
One or more of the types in the assembly unable to load.
at
at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at ...
The ModuleResolve event is not helpful here. I've determined that the
MyAttribute class (look below) that extends [mscorlib]System.CustomAttribute
causes the problem (if I remove it, the problem is gone but this is not what
I want).
Could anyone explain to me what is happening here?
Thanks a lot.
Wiktor Zychla
Appendix:
1. the ILAsmCode
..assembly extern mscorlib {}
..assembly t1 {}
..class public MyAttribute extends [mscorlib]System.CustomAttribute {
.method public void .ctor(bool b) {
ret
}
}
..class ExClass {
.method public static void ExMethod() cil managed {
.custom instance void MyAttribute::.ctor(bool) = (01 00 01 00 00)
.entrypoint
ldstr "Hello, world!"
call void [mscorlib] System.Console::WriteLine
(class System.String)
ret
}
}
I still have some problems with custom attributes and I ask someone for
a helpful hand.
In the appendix is the trivial ILAsm code with single custom attribute. The
code compiles OK but then I cannot use reflection to load the assembly to
read custom attributes because following code surprisingly fails in (*)
line:
public static void PrintAttributes( Assembly a )
{
foreach ( Type t in a.GetTypes() ) // *
foreach ( MethodInfo m in t.GetMethods() )
foreach ( object o in m.GetCustomAttributes(false) )
{
Console.WriteLine( "{0}\r\n\t{1}\r\n\t\t{2}", t, m, o );
}
}
with following exception:
Exception caught:
One or more of the types in the assembly unable to load.
at
at System.Reflection.Module.GetTypesInternal(StackCrawlMark& stackMark)
at System.Reflection.Assembly.GetTypes()
at ...
The ModuleResolve event is not helpful here. I've determined that the
MyAttribute class (look below) that extends [mscorlib]System.CustomAttribute
causes the problem (if I remove it, the problem is gone but this is not what
I want).
Could anyone explain to me what is happening here?
Thanks a lot.
Wiktor Zychla
Appendix:
1. the ILAsmCode
..assembly extern mscorlib {}
..assembly t1 {}
..class public MyAttribute extends [mscorlib]System.CustomAttribute {
.method public void .ctor(bool b) {
ret
}
}
..class ExClass {
.method public static void ExMethod() cil managed {
.custom instance void MyAttribute::.ctor(bool) = (01 00 01 00 00)
.entrypoint
ldstr "Hello, world!"
call void [mscorlib] System.Console::WriteLine
(class System.String)
ret
}
}