Re: [Mono-dev] Parch for Enum support in Custom attributes
Hi Eyal, Your patch is in SVN, I've just adapted coding style. Thanks a lot, Jb Eyal Alaluf wrote: Hi, JB. I worked a bit further on the patch and implemented the ForceRead logic for custom attributes. I aded a ForceResolving property to SignatureReader (it was the easiest way with the previous patch I made). If the patch if acceptable I'll prepare a separate and smaller patch to simplify SignatureReader abit and remove this property (pass it as a parameter). Attached is the patch and slightly modified test case. Eyal. On Thu, 5 Oct 2006, Jb Evain wrote: Date: Thu, 05 Oct 2006 19:16:47 +0200 From: Jb Evain [EMAIL PROTECTED] To: Eyal Alaluf [EMAIL PROTECTED] Cc: mono-devel-list@lists.ximian.com Subject: Re: [Mono-dev] Parch for Enum support in Custom attributes Hi Eyal, Thanks for working on this. I don't want to commit it as it is, but I'll surely use part of it. I don't want to load the assembly referenced only for reading a custom attribute body. Instead, I'll create an interface that CustomAttribute and SecurityDeclaration will share, and will allow one to load the content of something that needs to load a reference. Something like: CustomAttribute ca = ...; if (!ca.Read) { ca.ForceRead (); } Otherwise, for a lot of assemblies, Cecil will have to load the corelib while the user don't necessary need to read the content of the custom attribute. Jb Eyal Alaluf wrote: Hi, JB. Attached is patch for supporting enums in cutom attributes. Support is added for enums as ctor parameters as fields and as properties. The main problem with Enums is to identify their underlying integral type. Without this integral type the custom attribute cannot be read. The patch uses the assembly resolver for this purpose. I have attached a simple test scenraio with 3 C# files. * Test1.cs is a DLL defining enums and an attribute that has enums as field properties and ctor params. * Test2.cs is another DLL that uses the attribute and enums from Test1. This exercise the new code that resolves enum types from another DLL. * ReadTest2.cs is an EXE written using Cecil that parses test2.dll and prints the custom attributes of its types. It gets as argument the path to the dll it parses. Note that Test1 uses ClassUsageAttaribute from mscorlib. For some reason the assembly resolver didn't find mscorlib.dll from the GAC when I ran ReadTest2 on Test2 until I put mscorlib.dll in the same dir as Test2 ReadTest2. Eyal. Index: Mono.Cecil/ReflectionReader.cs === --- Mono.Cecil/ReflectionReader.cs(revision 66216) +++ Mono.Cecil/ReflectionReader.cs(working copy) @@ -65,7 +65,24 @@ protected CodeReader m_codeReader; protected ISymbolReader m_symbolReader; -public ModuleDefinition Module { +internal AssemblyNameReference Corlib +{ +get +{ +if (m_corlib == null) { +foreach (AssemblyNameReference ar in m_module.AssemblyReferences) { +if (ar.Name == Constants.Corlib) { +m_corlib = ar; +break; +} +} +} +return m_corlib; +}+} + +public ModuleDefinition Module +{ get { return m_module; } } @@ -295,19 +312,11 @@ TypeReference coreType = m_module.TypeReferences [fullName]; if (coreType == null) { -if (m_corlib == null) { -foreach (AssemblyNameReference ar in m_module.AssemblyReferences) { -if (ar.Name == Constants.Corlib) { -m_corlib = ar; -break; -} -} -} string [] parts = fullName.Split ('.'); if (parts.Length != 2) throw new ReflectionException (Unvalid core type name); -coreType = new TypeReference (parts [1], parts [0], m_corlib); +coreType = new TypeReference (parts [1], parts [0], Corlib); m_module.TypeReferences.Add (coreType); } if (!coreType.IsValueType) { Index: ChangeLog === --- ChangeLog(revision 66226) +++ ChangeLog(working copy) @@ -1,3 +1,11 @@ +2006-10-05 Eyal Alaluf [EMAIL PROTECTED] + +Mono.Cecil/ReflectionReader.cs: +Expose Corlib assembly refereice so SignatureReader can ise it. +Mono.Cecil.Signatures/SignatureReader.cs: +Added support
Re: [Mono-dev] Parch for Enum support in Custom attributes
Hi Eyal, The patch looks mostly good. Please use the new interface IRequireResolving I've just commited for the changes in CustomAttribute. Please also follow the coding guideline used in Cecil. I would also prefer a patch using a parameter instead of the ForceResolving property. Also please send me the patch once it's done instead of commiting, I have to check that everything works fine on the writing part. Thanks a lot, Jb Eyal Alaluf wrote: Hi, JB. I worked a bit further on the patch and implemented the ForceRead logic for custom attributes. I aded a ForceResolving property to SignatureReader (it was the easiest way with the previous patch I made). If the patch if acceptable I'll prepare a separate and smaller patch to simplify SignatureReader abit and remove this property (pass it as a parameter). Attached is the patch and slightly modified test case. Eyal. On Thu, 5 Oct 2006, Jb Evain wrote: Date: Thu, 05 Oct 2006 19:16:47 +0200 From: Jb Evain [EMAIL PROTECTED] To: Eyal Alaluf [EMAIL PROTECTED] Cc: mono-devel-list@lists.ximian.com Subject: Re: [Mono-dev] Parch for Enum support in Custom attributes Hi Eyal, Thanks for working on this. I don't want to commit it as it is, but I'll surely use part of it. I don't want to load the assembly referenced only for reading a custom attribute body. Instead, I'll create an interface that CustomAttribute and SecurityDeclaration will share, and will allow one to load the content of something that needs to load a reference. Something like: CustomAttribute ca = ...; if (!ca.Read) { ca.ForceRead (); } Otherwise, for a lot of assemblies, Cecil will have to load the corelib while the user don't necessary need to read the content of the custom attribute. Jb Eyal Alaluf wrote: Hi, JB. Attached is patch for supporting enums in cutom attributes. Support is added for enums as ctor parameters as fields and as properties. The main problem with Enums is to identify their underlying integral type. Without this integral type the custom attribute cannot be read. The patch uses the assembly resolver for this purpose. I have attached a simple test scenraio with 3 C# files. * Test1.cs is a DLL defining enums and an attribute that has enums as field properties and ctor params. * Test2.cs is another DLL that uses the attribute and enums from Test1. This exercise the new code that resolves enum types from another DLL. * ReadTest2.cs is an EXE written using Cecil that parses test2.dll and prints the custom attributes of its types. It gets as argument the path to the dll it parses. Note that Test1 uses ClassUsageAttaribute from mscorlib. For some reason the assembly resolver didn't find mscorlib.dll from the GAC when I ran ReadTest2 on Test2 until I put mscorlib.dll in the same dir as Test2 ReadTest2. Eyal. Index: Mono.Cecil/ReflectionReader.cs === --- Mono.Cecil/ReflectionReader.cs(revision 66216) +++ Mono.Cecil/ReflectionReader.cs(working copy) @@ -65,7 +65,24 @@ protected CodeReader m_codeReader; protected ISymbolReader m_symbolReader; -public ModuleDefinition Module { +internal AssemblyNameReference Corlib +{ +get +{ +if (m_corlib == null) { +foreach (AssemblyNameReference ar in m_module.AssemblyReferences) { +if (ar.Name == Constants.Corlib) { +m_corlib = ar; +break; +} +} +} +return m_corlib; +}+} + +public ModuleDefinition Module +{ get { return m_module; } } @@ -295,19 +312,11 @@ TypeReference coreType = m_module.TypeReferences [fullName]; if (coreType == null) { -if (m_corlib == null) { -foreach (AssemblyNameReference ar in m_module.AssemblyReferences) { -if (ar.Name == Constants.Corlib) { -m_corlib = ar; -break; -} -} -} string [] parts = fullName.Split ('.'); if (parts.Length != 2) throw new ReflectionException (Unvalid core type name); -coreType = new TypeReference (parts [1], parts [0], m_corlib); +coreType = new TypeReference (parts [1], parts [0], Corlib); m_module.TypeReferences.Add (coreType); } if (!coreType.IsValueType) { Index: ChangeLog
Re: [Mono-dev] Parch for Enum support in Custom attributes
Hi Eyal, Thanks for working on this. I don't want to commit it as it is, but I'll surely use part of it. I don't want to load the assembly referenced only for reading a custom attribute body. Instead, I'll create an interface that CustomAttribute and SecurityDeclaration will share, and will allow one to load the content of something that needs to load a reference. Something like: CustomAttribute ca = ...; if (!ca.Read) { ca.ForceRead (); } Otherwise, for a lot of assemblies, Cecil will have to load the corelib while the user don't necessary need to read the content of the custom attribute. Jb Eyal Alaluf wrote: Hi, JB. Attached is patch for supporting enums in cutom attributes. Support is added for enums as ctor parameters as fields and as properties. The main problem with Enums is to identify their underlying integral type. Without this integral type the custom attribute cannot be read. The patch uses the assembly resolver for this purpose. I have attached a simple test scenraio with 3 C# files. * Test1.cs is a DLL defining enums and an attribute that has enums as field properties and ctor params. * Test2.cs is another DLL that uses the attribute and enums from Test1. This exercise the new code that resolves enum types from another DLL. * ReadTest2.cs is an EXE written using Cecil that parses test2.dll and prints the custom attributes of its types. It gets as argument the path to the dll it parses. Note that Test1 uses ClassUsageAttaribute from mscorlib. For some reason the assembly resolver didn't find mscorlib.dll from the GAC when I ran ReadTest2 on Test2 until I put mscorlib.dll in the same dir as Test2 ReadTest2. Eyal. Index: Mono.Cecil/ReflectionReader.cs === --- Mono.Cecil/ReflectionReader.cs(revision 66216) +++ Mono.Cecil/ReflectionReader.cs(working copy) @@ -65,7 +65,24 @@ protected CodeReader m_codeReader; protected ISymbolReader m_symbolReader; - public ModuleDefinition Module { + internal AssemblyNameReference Corlib + { + get + { + if (m_corlib == null) { + foreach (AssemblyNameReference ar in m_module.AssemblyReferences) { + if (ar.Name == Constants.Corlib) { + m_corlib = ar; + break; + } + } + } + return m_corlib; + } + } + + public ModuleDefinition Module + { get { return m_module; } } @@ -295,19 +312,11 @@ TypeReference coreType = m_module.TypeReferences [fullName]; if (coreType == null) { - if (m_corlib == null) { - foreach (AssemblyNameReference ar in m_module.AssemblyReferences) { - if (ar.Name == Constants.Corlib) { - m_corlib = ar; - break; - } - } - } string [] parts = fullName.Split ('.'); if (parts.Length != 2) throw new ReflectionException (Unvalid core type name); - coreType = new TypeReference (parts [1], parts [0], m_corlib); + coreType = new TypeReference (parts [1], parts [0], Corlib); m_module.TypeReferences.Add (coreType); } if (!coreType.IsValueType) { Index: ChangeLog === --- ChangeLog (revision 66226) +++ ChangeLog (working copy) @@ -1,3 +1,11 @@ +2006-10-05 Eyal Alaluf [EMAIL PROTECTED] + + Mono.Cecil/ReflectionReader.cs: + Expose Corlib assembly refereice so SignatureReader can ise it. + Mono.Cecil.Signatures/SignatureReader.cs: + Added support for enums in custom attributes ctors, properties and + fields. + 2006-10-04 Eyal Alaluf [EMAIL PROTECTED] * Mono.Cecil/StructureReader.cs: @@ -2,3 +10,2 @@ Visit the module we load when a DLL has more then 1