« Home « Kết quả tìm kiếm

Module 17 (Optional): Attributes


Tóm tắt Xem thử

- Complying with all applicable copyright laws is the responsibility of the user.
- Read all of the materials for this module..
- This code is in the runtime for the predefined attributes and is written by the developer for custom attributes..
- Then explain the details of the.
- You will learn about attribute syntax and how to use some of the predefined attributes in the.
- Topic Objective To provide an overview of the module topics and objectives..
- With the introduction of attributes, the C# language provides a convenient technique that will help handle tasks such as changing the behavior of the runtime, obtaining transaction information about an object, conveying organizational information to a designer, and handling unmanaged code..
- Identify some of the predefined attributes that are available in the .NET Framework..
- Stored with the metadata of the element.
- The .NET Framework provides attributes so that you can extend the capabilities of the C# language.
- You can apply attributes to many elements of the source code.
- Information about the attributes is stored with the metadata of the elements they are associated with..
- The code to examine them and act upon the values they contain is also incorporated as a part of the runtime and .NET Framework software development kit (SDK)..
- For example, in the case of assembly attributes, place them after any using clauses but before any code, and explicitly specify them as attributes of the assembly..
- The capabilities of predefined attributes in the .NET Framework encompass a wide range of areas, from interoperability with COM to compatibility with visual design tools..
- This topic describes some of the common predefined attributes that are provided by the .NET Framework.
- The following list summarizes some of the general attributes that are provided by the .NET Framework..
- DllImport Method Indicates that the method is implemented in unmanaged code, in the specified DLL.
- Some of these are listed in the following table..
- in the .NET Framework SDK..
- The attribute you use for this purpose is shown in the following table..
- Developers who build components for a visual designer use the attributes listed in the following table..
- Defines a brief piece of text to be displayed at the bottom of the property window in the visual designer when this property or event is selected..
- Does not cause conditional compilation of the method itself.
- The Conditional attribute is common and is used in the labs.
- You can either add a #define directive to the code as shown in the preceding example, or define the symbol from the command line when you compile your program..
- The Conditional attribute does not cause conditional compilation of the method itself.
- When your code calls this method, the common language runtime locates the DLL, loads it into the memory of your process, marshals parameters as necessary, and transfers control to the address at the beginning of the unmanaged code.
- However, if you use COM+, it takes care of managing the transactional integrity of the system and coordinating events on the network..
- The Transaction attribute is one of the predefined .NET Framework attributes that the .NET Framework runtime interprets automatically..
- When you encounter a situation in which none of the predefined .NET Framework attributes satisfy your requirements, you can create your own attribute.
- To do so, you annotate your custom attribute with an AttributeUsage tag as shown in the following example:.
- The members of this enumeration are summarized in the following table..
- Such a class will define the name of the attribute, how it can be created, and the information that it will store..
- For example, the DeveloperInfo attribute requires the name of the developer as a string parameter.
- parameter or parameters of the attribute pass this information to the constructor..
- Checks the Scope of the Attribute 3.
- Checks for a Constructor in the Attribute 4.
- Creates an Instance of the Object 5.
- Checks the scope of the attribute 3.
- Checks for a constructor in the attribute 4.
- Creates an instance of the object 5.
- Saves the current state of the attribute class.
- In the preceding example, when MyClass is compiled, the compiler will search for an attribute class called DevloperInfoAttribute.
- Then it will check for a constructor that matches the parameters specified in the attribute use.
- If it finds one, it creates an instance of the object by calling the constructor with the specified values..
- If there is a named parameter, the compiler matches the name of the parameter with a field or property in the attribute class, and then sets the field or property to the specified value.
- Then the current state of the attribute class is saved to the metadata for the program element on which it is applied..
- As is mentioned in the previous topic, it is a good practice to add the suffix “Attribute” to the name of an attribute class.
- Even if you omit the Attribute suffix as shown in the example, your code will still compile correctly.
- An Element Can Have More Than One Instance of The Same Attribute.
- You can apply more than one attribute to a programming element, and you can use multiple instances of the same attribute in an application..
- The default behavior of a custom attribute does not permit multiple instances of the attribute.
- This attribute allows you to record the name of the developer that wrote a class.
- If more than one developer was involved in the development, you need to use the DeveloperInfo attribute more than once.
- For an attribute to permit this, you must mark it as AllowMultiple in the AttributeUsage attribute, as follows:.
- After you have applied attributes to programming elements in your code, it is useful to be able to determine the values of the attributes.
- One particular class in this namespace—the MemberInfo class—is very useful if you need to find out about the attributes of a class..
- To populate a MemberInfo array, you can use the GetMembers method of the System.Type object.
- To create a System.Type object, you use the typeof operator with a class or any other element, as shown in the following code:.
- Once created, the memberInfoArray elements can be queried for metadata information about the members of the class MyClass..
- For more information, search for “System.Reflection namespace” in the .NET Framework SDK..
- MemberInfo is actually the abstract base class of the other “Info” types..
- Iterate through the array and examine the values of each element in the array.
- This method retrieves the information about all attributes of a class and stores it as an array, as shown in the following code:.
- You can then iterate through the array to find the values of the attributes that you are interested in..
- In the following code, the only attribute of interest is.
- For each DeveloperInfoAttribute found, the values of the Developer and Date properties are displayed as follows:.
- If you only want values for that one attribute type, you can invoke this method by passing the type of the custom attribute you are looking for through it, as shown in the following code:.
- This method will display the details of the account.
- Open the Bank.sln project in the install folder\Labs\Lab17\Starter\Bank folder..
- The method must display the contents of the account: account number, account holder, account type, and account balance.
- The following code shows a possible example of the method:.
- Make use of the method’s dependence on the DEBUG_ACCOUNT symbol..
- Open the TestHarness.sln project in the install folder\Labs\Lab17\.
- Review the Main method of the CreateAccount class.
- You will see that the DumpToScreen method is present in the BankAccount class..
- You will see the ConditionalAttribute at the beginning of the method.
- The problem is in the test harness.
- Because of the ConditionalAttribute on DumpToScreen, the runtime will effectively ignore calls made to that method if the DEBUG_ACCOUNT symbol is not defined when the calling program is compiled.
- At the top of the CreateAccount.cs file, before the first using directive, add the following code:.
- This attribute will allow the name of the developer and, optionally, the creation date of a class to be stored in the metadata of that class.
- You will then write a method that retrieves and displays all of the DevloperInfoAttribute values for a class..
- Using Visual Studio .NET, create a new Microsoft Visual C# project, using the information shown in the following table..
- Make sure that you also change the name of the constructor..
- The DeveloperInfoAttribute attribute requires the name of the developer of the class as a mandatory parameter and takes the date that the class was written as an optional string parameter.
- the name of the developer of a class to be stored.
- You will now use the DeveloperInfo attribute to record the name of the developer of the Rational number class.
- Open the Rational.sln project in the install folder\Labs\Lab17\Starter\Rational folder..
- In the Add Reference dialog box, click Browse..
- Expand the Rational namespace in the tree view..
- Near the top of the class, notice your custom attribute and the values that you supplied..
- In the Main method, create a variable called attrInfo of type System.Reflection.MemberInfo, as shown in the following code:.
- The attributes of a class are held as part of the class information.
- Create an object array called attrs, and use the GetCustomAttributes method of attrInfo to find all of the custom attributes used by the Rational class, as shown in the following code:.
- Now you need to extract the attribute information that is stored in the attrs array and print it.
- CustomAttribute.DeveloperInfoAttribute, and assign it the first element in the attrs array, casting as appropriate, as shown in the following code:.
- In production code, you would use reflection rather than a cast to determine the type of the attribute..
- Use the get accessor of the DeveloperInfoAttribute attribute to retrieve the Developer and Date attributes and print them out as follows:.
- Repeat steps 4 and 5 for element 1 of the attrs array..
- The completed code for the Main method is shown in the following code:.
- An attribute value is stored with the metadata of the programming element that it was used with..
- The review questions cover some of the key concepts taught in the module.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt