This site is 100% ad supported. Please add an exception to adblock for this site.

C# Attributes

Terms

undefined, object
copy deck
What general kinds of attributes are there?
Predefined or custom.
What are the reserved attributes?
There are only two reserved attributes, Conditional and Obsolete.
Give an example of attribute usage, i.e., show a code snippet.
[Obsolete] public myClass { ... };

//This class should not be used.

or

[Conditional("Debug")] void DebugTrace() { ... }

Compile this method only if the Debug flag is defined.
What types of declarations can an attribute target?
Most declarations. A complete list can be found at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfattributetargets.asp
Can all attributes be used on all targets?
No. Most attributes must be attached to a specific language element. However, there are some global attributes.
To what elements do global attributes apply?
Assemblies or modules.
Name two global assembly attributes.
The following are found in the System.Reflection namespace.

AssemblyConfigurationAttribute
AssemblyCultureAttribute
AssemblyDescriptionAttribute
AssemblyKeyFileAttribute
AssemblyKeyNameAttribute
AssemblyDelaySignAttribute
AssemblyTitleAttribute
AssemblyVersionAttribute
What System.Reflection attributes are used with the strong name tool?
AssemblyKeyFileAttribute
AssemblyKeyNameAttribute
AssemblyDelaySignAttribute

Use the file name from the SN.exe (strong name utility) as arguments to these attributes.
How can you create your own custom attributes.
Create a class that inherits form the System.Attribute class.
Give a quick code example that builds a custom attribute
using System;
//restrict attribute usage to classes only
[AttributeUsage(AttributeTargets.Class)]
public class myClass(string astring)
{
//constructor
public Collector(string astring
{
this.astring = astring;
date = 1993;
}
//private versions of parameters
public short date;
string astring;
}
How is name space collision avoided when building attributes?
All attributes implicitly end with "Attribute". Thus "Collector" from the example will be treated as "CollectorAttribute" by the system.
What System.Reflection method is used to retrieve attribute information from Attributes?
Either
public virtual object[] GetCustomAttributes(bool);
or
public static Attribute GetCustomAttribute(Assembly, Type);
What are the two types of parameters that are supported by Attribute Classes?
Positional Parameters:
a. Must be specified when the attribute is used.
b. Must be set by constructor.

Named Parameters:
a. optional
b. when specified name of parameter must be specified. i.e., foo = foovalue
c. named parameters are similar to properties. But defined at the class level.
What types may an attribute argument be?
Simple numeric types including char.
string
System.Type
enums
object
one-dimensional arrays of any of the above types.
Where can you find the acceptable values for the AttributeTargets attribute?
AttributeTargets enumeration
If you wanted to create a attribute that could be used more than once how would you do it?
[AttributeUsage(AttributeTargets.All,AllowMultiple = true)]
Note: AllowMultiple must be named.

Deck Info

16

permalink