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?