471,353 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Defining custom attributes for arrays

Is it possible to define custom attributes for arrays? And if so, how
can I retrieve them?

I mean I want to define sth like:

[SomeAttribute("SomeValue")]
int[] MyArray;

and then retrieve the value of the custom attribute on demand. I used
the GetCustomAttributes method on MyArray, but the results "length" was
0, meaning that there's no custom attribute associated with MyArray. I
also used ILDASM and it confirmed this.

Any idea?

Feb 2 '06 #1
5 1655
>Is it possible to define custom attributes for arrays?

Yes.

You would need to show us what your attribute class looks like and also in
full how you are trying to retrieve it.
Whereabouts are you looking in the ILDASM output?

"Sadeq" <MS******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is it possible to define custom attributes for arrays? And if so, how
can I retrieve them?

I mean I want to define sth like:

[SomeAttribute("SomeValue")]
int[] MyArray;

and then retrieve the value of the custom attribute on demand. I used
the GetCustomAttributes method on MyArray, but the results "length" was
0, meaning that there's no custom attribute associated with MyArray. I
also used ILDASM and it confirmed this.

Any idea?

Feb 2 '06 #2
Sadeq,

You can not place an attribute on an array type, as far as I know. This
would require you to have access to the type definition, which you do not.

Rather, you can place the attribute on the field, parameter, or return
type, and then reflect on that, and perform your operation.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sadeq" <MS******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is it possible to define custom attributes for arrays? And if so, how
can I retrieve them?

I mean I want to define sth like:

[SomeAttribute("SomeValue")]
int[] MyArray;

and then retrieve the value of the custom attribute on demand. I used
the GetCustomAttributes method on MyArray, but the results "length" was
0, meaning that there's no custom attribute associated with MyArray. I
also used ILDASM and it confirmed this.

Any idea?

Feb 2 '06 #3
My custom attribute looks like this:

public sealed class StringAttribute : System.Attribute
{
public tag_str StringAttr;
public StringAttribute(tag_str StringType)
{
this.StringAttr = StringType;
}
}

tag_str is an enum.

I want to put my own attribute on strings like this:

[StringAttribute(tag_str.UTF8)]
String str= "Hello, World!";

OR

[StringAttribute(tag_str.ASCII)]
String str= "Hello, World!";

This attribute can also be assigned to arrays of String like:

[StringAttribute(tag_str.UTF8)]
String str[]= {"Hello", "World"};

I want to retrieve the attribute in my code, and decide on the encoding
of the string based on the value of the attribute. I wanted to code it
like this (as far as I remember):

StringAttribute[] sa = StringAttribute[]
str.GetType()GetCustomAttributes(false);

but this doesn't work (sa will be empty).

As ILDASM lists all attributes for every defined type in an assembly, I
checked for attributes for str, but there were non.

********************************************

Nicholas, are u sure this can't be done? If so, what should I do?

Feb 2 '06 #4
> StringAttribute[] sa = StringAttribute[]
str.GetType()GetCustomAttributes(false);
Syntax issues aside, all this would do is determine whether the .NET
type System.String has your custom attribute. Which it doesn't.

I am assuming that your string is a field in a class, e.g.

class MyClass
{
[StringAttribute(tag_str.UTF8)]
String str= "Hello, World!";
}
Then you would need to do something along the lines of:

MemberInfo[] mi = typeof(MyClass).GetMember("str");
// error checking omitted here
mi[0].GetCustomAttributes(false);

possibly having to mess around with binding flags in GetMember (for example
in the above scenario the field "str" is private).

"Sadeq" <MS******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... My custom attribute looks like this:

public sealed class StringAttribute : System.Attribute
{
public tag_str StringAttr;
public StringAttribute(tag_str StringType)
{
this.StringAttr = StringType;
}
}

tag_str is an enum.

I want to put my own attribute on strings like this:

[StringAttribute(tag_str.UTF8)]
String str= "Hello, World!";

OR

[StringAttribute(tag_str.ASCII)]
String str= "Hello, World!";

This attribute can also be assigned to arrays of String like:

[StringAttribute(tag_str.UTF8)]
String str[]= {"Hello", "World"};

I want to retrieve the attribute in my code, and decide on the encoding
of the string based on the value of the attribute. I wanted to code it
like this (as far as I remember):

StringAttribute[] sa = StringAttribute[]
str.GetType()GetCustomAttributes(false);

but this doesn't work (sa will be empty).

As ILDASM lists all attributes for every defined type in an assembly, I
checked for attributes for str, but there were non.

********************************************

Nicholas, are u sure this can't be done? If so, what should I do?

Feb 3 '06 #5
missed a bit out. Casting the returned array to an array of your attribute
type either. Do something like:

MemberInfo[] mi = typeof(MyClass).GetMember("str");
// error checking omitted here
object[] attributes = mi[0].GetCustomAttributes(false);
foreach (object attr in attributes)
{
if (attr is StringAttribute)
{
// do whatever you need here
}
}
"Clive Dixon" <cl*******************@digita.noluncheonmeat.com > wrote in
message news:Od**************@TK2MSFTNGP10.phx.gbl...
StringAttribute[] sa = StringAttribute[]
str.GetType()GetCustomAttributes(false);


Syntax issues aside, all this would do is determine whether the .NET
type System.String has your custom attribute. Which it doesn't.

I am assuming that your string is a field in a class, e.g.

class MyClass
{
[StringAttribute(tag_str.UTF8)]
String str= "Hello, World!";
}
Then you would need to do something along the lines of:

MemberInfo[] mi = typeof(MyClass).GetMember("str");
// error checking omitted here
mi[0].GetCustomAttributes(false);

possibly having to mess around with binding flags in GetMember (for
example in the above scenario the field "str" is private).

"Sadeq" <MS******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
My custom attribute looks like this:

public sealed class StringAttribute : System.Attribute
{
public tag_str StringAttr;
public StringAttribute(tag_str StringType)
{
this.StringAttr = StringType;
}
}

tag_str is an enum.

I want to put my own attribute on strings like this:

[StringAttribute(tag_str.UTF8)]
String str= "Hello, World!";

OR

[StringAttribute(tag_str.ASCII)]
String str= "Hello, World!";

This attribute can also be assigned to arrays of String like:

[StringAttribute(tag_str.UTF8)]
String str[]= {"Hello", "World"};

I want to retrieve the attribute in my code, and decide on the encoding
of the string based on the value of the attribute. I wanted to code it
like this (as far as I remember):

StringAttribute[] sa = StringAttribute[]
str.GetType()GetCustomAttributes(false);

but this doesn't work (sa will be empty).

As ILDASM lists all attributes for every defined type in an assembly, I
checked for attributes for str, but there were non.

********************************************

Nicholas, are u sure this can't be done? If so, what should I do?


Feb 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Doug Holland | last post: by
3 posts views Thread by Edward Diener | last post: by
1 post views Thread by Tamas Demjen | last post: by
2 posts views Thread by Harry F. Harrison | last post: by
16 posts views Thread by Howard Jess | last post: by
2 posts views Thread by prabhupr | last post: by
10 posts views Thread by Andy Fish | last post: by
3 posts views Thread by fussellja | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.