Hi, Jb,
You are right.
I just made some tests.
For example:
EnumTest1.dll:
namespace EnumTest1
{
public enum Enum1
{
Item1,
Item2,
Item3
}
public enum Enum2 : byte
{
Item1,
Item2,
Item3
}
public enum Enum3 : short
{
Item1,
Item2,
Item3
}
public enum Enum4 : ushort
{
Item1,
Item2,
Item3
}
}
EnumTest2.dll:
namespace EnumTest2
{
public class Class1
{
[DefaultValue(Enum1.Item1)]
public Enum1 Property1;
[DefaultValue(Enum2.Item2)]
public Enum2 Property2;
[DefaultValue(Enum3.Item3)]
public Enum3 Property3;
[DefaultValue(Enum4.Item1)]
public Enum4 Property4;
}
}
The il of EnumTest2.dll:
.field /*04000001*/ public valuetype [EnumTest1/
*23000002*/]EnumTest1.Enum1/*01000002*/ Property1
.custom /*0C000001:0A000010*/ instance void [System/
*23000003*/]System.ComponentModel.DefaultValueAttribute/
*01000016*/::.ctor(object) /* 0A000010 */
= {object(int32(0))}
// = ( 01 00 55 51 45 6E 75 6D 54 65 73 74 31 2E 45
6E // ..UQEnumTest1.En
// 75 6D 31 2C 20 45 6E 75 6D 54 65 73 74 31 2C 20 //
um1, EnumTest1,
// 56 65 72 73 69 6F 6E 3D 31 2E 30 2E 30 2E 30 2C //
Version=1.0.0.0,
// 20 43 75 6C 74 75 72 65 3D 6E 65 75 74 72 61 6C //
Culture=neutral
// 2C 20 50 75 62 6C 69 63 4B 65 79 54 6F 6B 65 6E // ,
PublicKeyToken
// 3D 6E 75 6C 6C 00 00 00 00 00 00 ) //
=null......
.field /*04000002*/ public valuetype [EnumTest1/
*23000002*/]EnumTest1.Enum2/*01000003*/ Property2
.custom /*0C00000F:0A000010*/ instance void [System/
*23000003*/]System.ComponentModel.DefaultValueAttribute/
*01000016*/::.ctor(object) /* 0A000010 */
= {object(bool(true))}
// = ( 01 00 55 51 45 6E 75 6D 54 65 73 74 31 2E 45
6E // ..UQEnumTest1.En
// 75 6D 32 2C 20 45 6E 75 6D 54 65 73 74 31 2C 20 //
um2, EnumTest1,
// 56 65 72 73 69 6F 6E 3D 31 2E 30 2E 30 2E 30 2C //
Version=1.0.0.0,
// 20 43 75 6C 74 75 72 65 3D 6E 65 75 74 72 61 6C //
Culture=neutral
// 2C 20 50 75 62 6C 69 63 4B 65 79 54 6F 6B 65 6E // ,
PublicKeyToken
// 3D 6E 75 6C 6C 01 00 00 ) //
=null...
.field /*04000003*/ public valuetype [EnumTest1/
*23000002*/]EnumTest1.Enum3/*01000004*/ Property3
.custom /*0C000010:0A000010*/ instance void [System/
*23000003*/]System.ComponentModel.DefaultValueAttribute/
*01000016*/::.ctor(object) /* 0A000010 */
= {object(int16(2))}
// = ( 01 00 55 51 45 6E 75 6D 54 65 73 74 31 2E 45
6E // ..UQEnumTest1.En
// 75 6D 33 2C 20 45 6E 75 6D 54 65 73 74 31 2C 20 //
um3, EnumTest1,
// 56 65 72 73 69 6F 6E 3D 31 2E 30 2E 30 2E 30 2C //
Version=1.0.0.0,
// 20 43 75 6C 74 75 72 65 3D 6E 65 75 74 72 61 6C //
Culture=neutral
// 2C 20 50 75 62 6C 69 63 4B 65 79 54 6F 6B 65 6E // ,
PublicKeyToken
// 3D 6E 75 6C 6C 02 00 00 00 ) //
=null....
.field /*04000004*/ public valuetype [EnumTest1/
*23000002*/]EnumTest1.Enum4/*01000005*/ Property4
.custom /*0C000011:0A000010*/ instance void [System/
*23000003*/]System.ComponentModel.DefaultValueAttribute/
*01000016*/::.ctor(object) /* 0A000010 */
= {object(int16(0))}
// = ( 01 00 55 51 45 6E 75 6D 54 65 73 74 31 2E 45
6E // ..UQEnumTest1.En
// 75 6D 34 2C 20 45 6E 75 6D 54 65 73 74 31 2C 20 //
um4, EnumTest1,
// 56 65 72 73 69 6F 6E 3D 31 2E 30 2E 30 2E 30 2C //
Version=1.0.0.0,
// 20 43 75 6C 74 75 72 65 3D 6E 65 75 74 72 61 6C //
Culture=neutral
// 2C 20 50 75 62 6C 69 63 4B 65 79 54 6F 6B 65 6E // ,
PublicKeyToken
// 3D 6E 75 6C 6C 00 00 00 00 ) //
=null....
It seems ildasm guesses the enum size by bytes count of value, and
take the value as primitive types like int32,int16.
Is it possible for Cecil to take similar approach?
I thought Cecil was low level library which could read/write any part
of an assembly.
At least the "guess" should work and won't stop cecil from reading
custom attribute.
Regards
Wicky
On Aug 6, 11:03 pm, Jb Evain <[email protected]> wrote:
> On Fri, Aug 6, 2010 at 4:56 PM, Wicky <[email protected]> wrote:
> > Do you think ildasm knows some tricks to decode the custom attribute
> > binary?
> > How can it be sure to take the 0 as value without the type definition?
>
> It takes a guess. There's no way to know for sure without loading the
> definition of the enum.
>
> --
> Jb Evain <[email protected]>
--
--
mono-cecil