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
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
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
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
*******
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.
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
*****
fMember.CustomAttributes[0].ConstructorParameters[0].ToString()
*****
Regards.
rca
You can simply cast it to string instead of calling ToString.
--
Jb