473,789 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GetCustomAttrib utes 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 1790
>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.goo glegroups.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 GetCustomAttrib utes 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.co m

"Sadeq" <MS******@gmail .com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 GetCustomAttrib utes 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.Attribut e
{
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()Ge tCustomAttribut es(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()Ge tCustomAttribut es(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].GetCustomAttri butes(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.goo glegroups.com.. . My custom attribute looks like this:

public sealed class StringAttribute : System.Attribut e
{
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()Ge tCustomAttribut es(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].GetCustomAttri butes(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******** ******@TK2MSFTN GP10.phx.gbl...
StringAttribute[] sa = StringAttribute[]
str.GetType()Ge tCustomAttribut es(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].GetCustomAttri butes(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.goo glegroups.com.. .
My custom attribute looks like this:

public sealed class StringAttribute : System.Attribut e
{
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()Ge tCustomAttribut es(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
454
by: Doug Holland | last post by:
Often you see code where an empty interface is used to indicate something about the class that realizes it. In the .NET world this can be done with custom attributes too, so which is better: public class SomeClass : IEmptyInterface or
3
2147
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
1
4211
by: Tamas Demjen | last post by:
I started to experiment with VC++ 2005 Beta1. So far everything went fine, and already have a working project, but soon I realized that the compiler was ancient (not supporting half of the C++/CLI standard). So I downloaded the VC++ 2005 Tools Refresh (http://tinyurl.com/5mdq2). After that I tried to do a full rebuild, but my project wouldn't link anymore. I get the following errors: MSVCMRTD.lib(mstartup.obj) : error LNK2022: metadata...
2
3536
by: Harry F. Harrison | last post by:
I get 2 compile errors on assembly attributes after creating a custom attribute. If I comment out the attribute, the errors go away. I don't get it because my attribute specifies class usage, not assembly usage. Assembly attribute 'System.Runtime.InteropServices.GuidAttribute' is not valid: Assembly custom attribute 'System.Runtime.InteropServices.GuidAttribute' was specified multiple times with different values Attribute...
6
2337
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without having to do a httpwebreqeust call.
16
4347
by: Howard Jess | last post by:
All -- I'm trying to solve a problem for which I think the solution will be to *cheat*; but I don't mind doing so for this case. The background is: Given an object constructor, and an instance SampleObj = function() { this.prop = 1; }
2
2530
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article is the usage of "Custom Attribute" in real life project. Can somebody please help me understand where are these informations really helpful in Development Environment; may be a few example(s) will help me understand.
10
1969
by: Andy Fish | last post by:
Hi, I would like to include extra "hidden" information in a generated HTML page that can be used by javascript functions. I realise that most browsers seem to ignore any tags and attributes they don't understand, but from what I can tell the standards do not allow me to make up my own tags or attributes as they will fail validation. is there any standard element name that can be used for such a purpose i.e.
3
9461
by: fussellja | last post by:
Is there a way of defining the maximum length of a string parameter to a web service in .Net? I assumed that there would be an Xml... attribute that I could apply to a string definition in the class that would control the generation of the necessary maxlength attribute in the WSDL but I can't seem to find one. Thanks for any help you can offer, John.
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.