Language Question

  • Thread starter Thread starter Tommy Vercetti
  • Start date Start date
T

Tommy Vercetti

I have a fairly firm grasp of the C# language. I often see Visual Studio
add code in square brackets. I've read through "The C# Programming
Language" and I don't see any mention of this construct. Normally I just
try to ignore this code but now I need to understand what it is and what
it is doing.

What is this? Is this a C# construct that just isn't well documented or
is this Visual Studio specific code markup language?

Examples:

[assembly: AssemblyCulture("")]

[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
 
Tommy,

This is know as metadata and is compiled into the code by the compiler.
It
is used by the Common Language Runtime (the runtime environment for .NET
applications) and other classes in .NET to determin runtime and designtime
actions.

For example when building your own UserControls you can add

[
Browsable(true),
Description("This sets the text of the control")
]
public string Text
{
get { return this.text; }
set { this.text = value; }
}

In this case visual designers, such as the form designer in VS.NET will use
this
meta data to (1) determine if the property should be displayed in the
property
grid (by looking for the Browsable attribute and its value) and (2) look for
a
description text to display below the property (by looking for the
Description
attribute and its value) grid when the property is selected. The .NET
framework uses a system called Reflection to work with metadata

You could lookup Metadata, Attributes and Reflection in the MSDN
documentation for the .NET framework to get more information on what is it
is and how to use it.

Hope this helps,

//Andreas
 
Ah, thank you very much! That explains why it wouldn't be covered in a
C# book. I would probably have better luck in a .NET CLR book.

thanks!
Tommy,

This is know as metadata and is compiled into the code by the compiler.
It
is used by the Common Language Runtime (the runtime environment for .NET
applications) and other classes in .NET to determin runtime and designtime
actions.

For example when building your own UserControls you can add

[
Browsable(true),
Description("This sets the text of the control")
]
public string Text
{
get { return this.text; }
set { this.text = value; }
}

In this case visual designers, such as the form designer in VS.NET will use
this
meta data to (1) determine if the property should be displayed in the
property
grid (by looking for the Browsable attribute and its value) and (2) look for
a
description text to display below the property (by looking for the
Description
attribute and its value) grid when the property is selected. The .NET
framework uses a system called Reflection to work with metadata

You could lookup Metadata, Attributes and Reflection in the MSDN
documentation for the .NET framework to get more information on what is it
is and how to use it.

Hope this helps,

//Andreas

I have a fairly firm grasp of the C# language. I often see Visual Studio
add code in square brackets. I've read through "The C# Programming
Language" and I don't see any mention of this construct. Normally I just
try to ignore this code but now I need to understand what it is and what
it is doing.

What is this? Is this a C# construct that just isn't well documented or
is this Visual Studio specific code markup language?

Examples:

[assembly: AssemblyCulture("")]

[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
 
Tommy,

You need to be looking at the Attributes, Attribute class, etc. to discover
the in depth information about these constructs. For example you will find
AssemblyCulture listed under AssemblyCultureAttribute in the help files, not
as AssemblyCulture. AttributeTargets will cover what the "[assembly" part
means. "Specifies the application elements on which it is valid to apply an
attribute." - from the MS Help files.


As an example:
[DefaultEvent("Click")]
DefaultEventAttribute can be applied to a class, since it is defined as:
[AttributeUsage(AttributeTargets.Class)]
public sealed class DefaultEventAttribute : Attribute

In the case where this is applied to a Control, when you double click on the
Control in the Design View, it will create a "Click" event for your Control.

Hope this helps,
Chris A.R.
 
This is what you find in a Web Custom Library code behind. It is called
an attribute and the square bracket specifies that it is an Attribute.
In VB.Net, it is <.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top