Hello guys,

   We are running into an interesting problem with the Reflection.Emit
API in Mono, precisely, this code crashes Reflection.Emit when the Mono
C# compiler compiles it:

[StructLayout(LayoutKind.Explicit,Size=32,CharSet=CharSet.Ansi)]
struct FloatHack
{
         [FieldOffset(0)] public float fh_float;
         [FieldOffset(0)] public int fh_int;
}

    I could not understand what was wrong, but it turns out that this
also happens with the Microsoft Java Script compiler:

C:\>type test.js
import System;
import System.Runtime.InteropServices;

public StructLayoutAttribute (LayoutKind.Auto, CharSet=CharSet.Ansi)
class Hello {
      var x : System.String;
}

System.Console.WriteLine ("Hello!");

C:\>jsc test.js
Microsoft (R) JScript .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.00.3705
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

***INTERNAL COMPILER ERROR***
Microsoft.Vsa.VsaException: InternalCompilerError (0x80133021): 
System.ArgumentException: Invalid custom attribute provided: Unrecognized custom 
attribute argument:    ????.
   at System.Reflection.Emit.TypeBuilder.InternalCreateCustomAttribute(Int32 
tkAssociate, Int32 tkConstructor, Byte[] attr, Module module, Boolean toDisk)
   at 
System.Reflection.Emit.CustomAttributeBuilder.CreateCustomAttribute(ModuleBuilder mod, 
Int32 tkOwner)
   at Microsoft.JScript.Class.GetTypeBuilderOrEnumBuilder()
   at Microsoft.JScript.Class.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.Block.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.ScriptBlock.TranslateToIL(ILGenerator il, Type rtype)
   at Microsoft.JScript.ScriptBlock.TranslateToILClass(CompilerGlobals compilerGlobals)
   at Microsoft.JScript.VsaStaticCode.TranslateToIL()
   at Microsoft.JScript.Vsa.VsaEngine.DoCompile()
   at Microsoft.Vsa.BaseVsaEngine.Compile()

The problem is that Reflection.Emit has no API for me to set the layout
bits once a type has been created (otherwise, I could create the type,
then filter out the attribute, and apply the updates).  So I am
convinced that .NET is applying some magic on its own to the type that
was created for the CharSet property of the StructLayoutAttribute.

My request for help is to get confirmation for someone who has the Rotor
source code to verify if the class
System.Runtime.InteropServices.StructLayout and in particular
System.Runtime.InteropServices.CharSet is handled specially by Rotor
during a call to TypeBuilder.SetCustomAttributes.

>From the stack trace, it looks like it happens in the C-side of things,
and not in the IL code.

We plan on handling this at the Reflection.Emit/SetCustomAttribute
level, since there is no other API that would let us customize the type
without breaking the compiler in fundamental ways.

This is preferable than adding incompatible APIs to
System.Reflection.Emit.

I just want confirmation on the fact that our System.Reflection will
have to manually handle some attributes (and of course, if you can list
them, that would be even better ;-).

Best wishes,
Miguel.

Reply via email to