Presentation is loading. Please wait.

Presentation is loading. Please wait.

.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.

Similar presentations


Presentation on theme: ".NET and .NET Core 10. Enabling Contracts Pan Wuming 2017."— Presentation transcript:

1 .NET and .NET Core 10. Enabling Contracts Pan Wuming 2017

2 Attribute Overview of Attributes Common Predefined Attributes
Introduction to Attributes Applying Attributes Common Predefined Attributes Defining Custom Attributes Retrieving Attribute Values

3

4 Introduction to Attributes
Attributes Are: Stored with the metadata of the element Declarative tags that convey information to the runtime .NET Framework Provides Predefined Attributes(Intrinsic Attribute) The runtime contains code to examine values of attributes and act on them

5 Attribute Target It’s something which attribute apply to.
Such as, Assembly, Class, Constructor, Delegate, Enum, Field, Method, Interface, Module, Parameter, Property, Return Value, Struct, All

6 Applying Attributes Syntax: Use Square Brackets to Specify an Attribute To Apply Multiple Attributes to an Element, You Can: Specify multiple attributes in separate square brackets Use a single square bracket and separate attributes with commas [attribute(positional_parameters, named_parameter=value, ...)] element

7 Common Predefined Attributes
.NET Provides Many Predefined Attributes General attributes COM interoperability attributes Transaction handling attributes Visual designer component building attributes

8 Using the DllImport Attribute
With the DllImport Attribute, You Can: Invoke unmanaged code in DLLs from a C# environment Tag an external method to show that it resides in an unmanaged DLL [DllImport("MyDLL.dll", EntryPoint="MessageBox")] public static extern int MyFunction(string param1); public class MyClass( ) { ... int result = MyFunction("Hello Unmanaged Code"); ... }

9 Defining Custom Attributes
Defining Custom Attribute Scope Defining an Attribute Class Processing a Custom Attribute Using Multiple Attributes

10 Defining Custom Attribute Target
[AttributeUsage(AttributeTargets.Class, Inherited = false)] [Serializable] public sealed class AttributeUsageAttribute : Attribute { internal AttributeTargets m_attributeTarget = AttributeTargets.All; internal Boolean m_allowMultiple = false; internal Boolean m_inherited = true; public AttributeUsageAttribute(AttributeTargets validOn) { m_attributeTarget = validOn; } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class MyAttribute: System.Attribute { ... } Use the AttributeUsage Tag to Define Scope Use the Bitwise “or” Operator (|) to Specify Multiple Elements [AttributeUsage(AttributeTargets.Method)] public class MyAttribute: System.Attribute { ... }

11 public AttributeTargets ValidOn {
get { return m_attributeTarget; } } public Boolean AllowMultiple { get { return m_allowMultiple; } set { m_allowMultiple = value; } public Boolean Inherited { get { return m_inherited; } set { m_inherited = value; }

12 namespace System { [AttributeUsage(AttributeTargets.Enum, Inherited = false)] public class FlagsAttribute : System.Attribute { public FlagsAttribute() { }

13 Defining an Attribute Class 1/2
Deriving an Attribute Class All attribute classes must derive from System.Attribute, directly or indirectly Suffix name of attribute class with “Attribute” Components of an Attribute Class Define a single constructor for each attribute class by using a positional parameter Use properties to set an optional value by using a named parameter

14 Using a Custom Attribute
[BugFixAttribute(121, “David Yang", "Mar 7, 2003")] [BugFixAttribute(121, “David Yang", "Mar 17, 2003", Comment="Fixed off by one error")] public class MyDisplayer { //…………. }

15 Processing a Custom Attribute: The Compilation Process
1. Searches for the Attribute Class 2. Checks the Scope of the Attribute 3. Checks for a Constructor in the Attribute 4. Creates an Instance of the Class 5. Checks for a Named Parameter 6. Sets Field or Property to Named Parameter Value 7. Saves Current State of Attribute Object

16 Using Multiple Attributes
An Element Can Have More Than One Attribute Define both attributes separately An Element Can Have More Than One Instance of The Same Attribute Use AllowMultiple = true

17 Retrieving Attribute Values
Examining Class Metadata Querying for Attribute Information

18 Examining Class Metadata
To Query Class Metadata Information: Use the MemberInfo class in System.Reflection Populate a MemberInfo array by using the method System.Type.GetMembers Create a System.Type object by using the typeof operator Example System.Reflection.MemberInfo[ ] memberInfoArray; memberInfoArray = typeof(MyClass).GetMembers( );

19 Retrieving or Querying for Attribute Information
Use GetCustomAttributes to retrieve all attribute information as an array Iterate through the array and examine the values of each element in the array Use the IsDefined method to determine whether a particular attribute has been defined for a class

20 Contract Enabled Attribute Interact with other program
Interact with compiler Example: Serialization Example: Platform Invoke Interact with CLR Example: Caller Information

21

22 Common Uses for Attributes
Marking methods using the WebMethod attribute in Web services. Describing how to marshal method parameters when interoperating with native code. Describing the COM properties for classes, methods, and interfaces. Calling unmanaged code using the DllImportAttribute class. Describing your assembly in terms of title, version, description, or trademark. Describing which members of a class to serialize for persistence. Describing how to map between class members and XML nodes for XML serialization. Describing the security requirements for methods. Specifying characteristics used to enforce security. Controlling optimizations by the just-in-time (JIT) compiler. Obtaining information about the caller to a method.

23 Terms in this class Custom Attributes Positional parameter
Named parameter


Download ppt ".NET and .NET Core 10. Enabling Contracts Pan Wuming 2017."

Similar presentations


Ads by Google