Read/Write CustomAttribute with Cecil?

39 views
Skip to first unread message

Wicky

unread,
Jul 19, 2007, 10:02:59 AM7/19/07
to mono-cecil
Hi,

I would like to read/write CustomAttribute, but I found
CustomAttribute class doesn't include all necessary information.

For example, this simple class:

public class Class1
{
[DefaultValue(DayOfWeek.Friday)]
public DayOfWeek TestProperty
{
get { return DayOfWeek.Friday; }
}
}

When I loaded the CustomAttributes collection of TestProperty, I
couldn't find any thing about the type DayOfWeek. And then I traced
into Cecil, I found a internal class CustomAttrib which contain the
related assembly and type reference information (ElemType).

May I ask why Cecil doesn't expose such useful information?
Or I missed something?

Thanks.

Regards
Wicky

Jb Evain

unread,
Jul 19, 2007, 10:25:25 AM7/19/07
to mono-...@googlegroups.com
Hey,

On 7/19/07, Wicky <wicky...@gmail.com> wrote:
> For example, this simple class:
>
> public class Class1
> {
> [DefaultValue(DayOfWeek.Friday)]
> public DayOfWeek TestProperty
> {
> get { return DayOfWeek.Friday; }
> }
> }
>
> When I loaded the CustomAttributes collection of TestProperty, I
> couldn't find any thing about the type DayOfWeek. And then I traced
> into Cecil, I found a internal class CustomAttrib which contain the
> related assembly and type reference information (ElemType).
>
> May I ask why Cecil doesn't expose such useful information?
> Or I missed something?

Actually, yeah. When you use an enumeration defined in another
assembly in a CustomAttribute, Cecil will not decode the
CustomAttribute, so its property IsResolved will return false. That's
because we cannot know the size of the enumeration (byte, int32,
int64, ..) without loading the assembly the enumeration is declared
in.

By calling Resolve on the CustomAttribute, it will try to resolve the
assembly in which the enumeration is defined, so the custom attribute
is actually decoded.

--
Jb

Wicky

unread,
Jul 19, 2007, 10:35:46 AM7/19/07
to mono-cecil
Hi

On 7月19日, 下午10时25分, "Jb Evain" <j...@nurv.fr> wrote:
>
> By calling Resolve on the CustomAttribute, it will try to resolve the
> assembly in which the enumeration is defined, so the custom attribute
> is actually decoded.

I tried the CustomAttribute.Resolve method, it resolves
DayOfWeek.Friday to 5 (int).

So I still cannot know the type (System.DayOfWeek) from
CustomAttribute class after calling Resolve method.

It there any way to get the type definition/reference of
System.DayofWeek?
(Indeed it's in internal CustomAttrib class, but I cannot find it in
CustomAttribute class).

Thanks.

Regards
Wicky

Jb Evain

unread,
Jul 19, 2007, 10:45:26 AM7/19/07
to mono-...@googlegroups.com
On 7/19/07, Wicky <wicky...@gmail.com> wrote:
> I tried the CustomAttribute.Resolve method, it resolves
> DayOfWeek.Friday to 5 (int).
>
> So I still cannot know the type (System.DayOfWeek) from
> CustomAttribute class after calling Resolve method.
>
> It there any way to get the type definition/reference of
> System.DayofWeek?
> (Indeed it's in internal CustomAttrib class, but I cannot find it in
> CustomAttribute class).

Right now, you can't. Maybe in the future, on Resolve, Cecil will
actually Load the assembly declaring the enumeration, and put a boxed
enumeration in the Parameters.

I need to think about that.

--
Jb

rong...@gmail.com

unread,
Jul 19, 2007, 3:37:40 PM7/19/07
to mono-cecil
But for example when I only want to get the string in Attribute. Can I
do that.

*******
public class MyBaseForm : Form
{
[AccessedThroughProperty("MyList")]
private ArrayList myList;

public ArrayList MyList
{
return this.myList;
}
}
*******

In this example, I would like to get/set the string "MyList". Can I do
that?
Regards.

Jb Evain

unread,
Jul 19, 2007, 3:40:00 PM7/19/07
to mono-...@googlegroups.com
Hey,

On 7/19/07, rong...@gmail.com <rong...@gmail.com> wrote:
> *******
> public class MyBaseForm : Form
> {
> [AccessedThroughProperty("MyList")]
> private ArrayList myList;
>
> public ArrayList MyList
> {
> return this.myList;
> }
> }
> *******
>
> In this example, I would like to get/set the string "MyList". Can I do
> that?
> Regards.

Yeah that works well. The only issue is with enums.

--
Jb

rong...@gmail.com

unread,
Jul 19, 2007, 3:41:10 PM7/19/07
to mono-cecil
I have found it. This is the way

*****
fMember.CustomAttributes[0].ConstructorParameters[0].ToString()
*****

Regards.
rca

Jb Evain

unread,
Jul 19, 2007, 3:46:01 PM7/19/07
to mono-...@googlegroups.com
> *****
> fMember.CustomAttributes[0].ConstructorParameters[0].ToString()
> *****

You can simply cast it to string instead of calling ToString.

--
Jb

Reply all
Reply to author
Forward
0 new messages