C#-ILAsm custom attributes

W

Wiktor Zychla

Hello,

for some reason I need to use attributes in ILAsm code and read these
attributes in C# code. My problem is with the attribute declaration inside
ILAsm code. I know that I define any attribute by using ILasm's ".custom"
keyword but I do not understand how to initialize the attribute Value blob
when the attribute contains some fields. I just do not understand that
binary format:

..custom instance void MyAttribute::.ctor(bool) = (01 00 01 00 00)

(example taken from "Inside IL" by Serge Lidin, the attribute constructor
has only 1 parameter of type 'bool')

Lidin does not say anything on how the attrubute's constructor arguments
should be encoded (except of arguments of type 'string', he says that such
attributes should be represented by unicode hex data).

My questions:
- how do I encode any particular datatype (strings, ints, etc)?
- how do I encode attributes of custom types?
- how do I separate consecutive arguments in the argument list?

Thanks for any help,
Wiktor Zychla
 
E

Ed Courtenay

Wiktor said:
Hello,

for some reason I need to use attributes in ILAsm code and read these
attributes in C# code. My problem is with the attribute declaration inside
ILAsm code. I know that I define any attribute by using ILasm's ".custom"
keyword but I do not understand how to initialize the attribute Value blob
when the attribute contains some fields. I just do not understand that
binary format:

.custom instance void MyAttribute::.ctor(bool) = (01 00 01 00 00)

(example taken from "Inside IL" by Serge Lidin, the attribute constructor
has only 1 parameter of type 'bool')

Lidin does not say anything on how the attrubute's constructor arguments
should be encoded (except of arguments of type 'string', he says that such
attributes should be represented by unicode hex data).

My questions:
- how do I encode any particular datatype (strings, ints, etc)?
- how do I encode attributes of custom types?
- how do I separate consecutive arguments in the argument list?

Thanks for any help,
Wiktor Zychla

Why not create a dummy C# application with the attributes you require
applied, compile it and then use ILDASM to see what was generated?
 
W

Wiktor Zychla

I'm curious, why are you declaring these in IL?

practical part of my PhD research.

thanks for your help.

Wiktor Zychla
 
M

Mattias Sjögren

Wiktor,

FYI, the Whidbey version of IL(D)ASM supports new, typed syntax for
the attribute data, so you no longer have to work with the raw blob
format.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top