Defining custom attributes for arrays

  • Thread starter Thread starter Sadeq
  • Start date Start date
S

Sadeq

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?
 
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,

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.
 
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?
 
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 said:
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?
 
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 said:
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 said:
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?
 
Back
Top