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

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Using.NET Platform Note: Most of the material of these slides have been taken & extended from Nakov’s excellent overview for.NET framework, MSDN and wikipedia.
Attributes Programming in C# Attributes CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
.NET Framework Overview Pingping Ma Nov 16 th, 2006.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
AP 08/01 Component Programming with C# and.NET 1st Class Component Support Robust and Versionable Creating and using attributes API integration –DLL import.
MD. SAIFULLAH AL AZAD SPEAKER, TECH FORUM USER GROUP Previously Speak on: IIS 7 Available in facebook:
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
February 24 th -25 th 2004 Daragh Byrne – EPCC Additional.NET Concepts.
Lecture Roger Sutton CO530 Automation Tools 5: Class Libraries and Assemblies 1.
.NET Framework Introduction: Metadata
Introduction to .Net Framework
The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++
.NET Framework & C#.
Other Types in OOP Enumerations, Structures, Generic Classes, Attributes Svetlin Nakov Technical Trainer Software University
Kalpesh Padia Reflection in.Net. OVERVIEW 9/19/
Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam.
Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual.
.NET Framework Danish Sami UG Lead.NetFoundry
All types in the CLR are self-describing – CLR provides a reader and writer for type definitions System.Reflection & System.Reflection.emit – You can ‘read’
Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Effective C# 50 Specific Way to Improve Your C# Item 42, 43.
Module 14: Attributes. Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values.
ASP.NET Web Services.  A unit of managed code installed under IIS that can be remotely invoked using HTTP.
.NET Ying Chen Junwei Chen. What is Microsoft.NET. NET is a development platform Incorporated into.NET COM+ component services ASP web development framework.
Attributes C#.Net Software Development Version 1.0.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Introduction to Object-Oriented Programming Lesson 2.
.NET Mobile Application Development XML Web Services.
July 22, 2001Introduction to.NET1 Introduction to.NET Framework Gholamali Semsarzadeh July 2001.
Building Custom Controls with ASP.NET and the Microsoft ®.NET Framework Rames Gantanant Microsoft Regional Director, Thailand
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Reflection and Attributes.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
.Net Reflection Taipan Tamsare. Overview Reflection core concepts Exploring metadata Detail information Attributes Building Types at runtime.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Java Generics.
Basic Introduction to C#
Jim Fawcett CSE775 – Distributed Objects Spring 2009
Browne Bag Seminar Applied .Net Attributes
Module 5: Common Type System
Advanced .NET Programming I 7th Lecture
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Indexer AKEEL AHMED.
CS360 Windows Programming
Module 1: Getting Started
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Introduction to C# AKEEL AHMED.
5.1 Being Objects and A Glimpse into Coding
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
6 Delegate and Lambda Expressions
Java Programming Course
Module 10: Implementing Managed Code in the Database
Fundaments of Game Design
How to organize and document your classes
CIS 199 Final Review.
Quiz Points 1 Rules Raise your hand if you know the question
5. 3 Coding with Denotations
Jim Fawcett CSE775 – Distributed Objects Spring 2006
Advanced .NET Programming I 8th Lecture
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2006
Generics, Lambdas and Reflection
Presentation transcript:

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

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

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

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

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

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

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"); ... }

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

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 { ... }

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; }

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

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

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 { //…………. }

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

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

Retrieving Attribute Values Examining Class Metadata Querying for Attribute Information

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( );

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

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

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.

Terms in this class Custom Attributes Positional parameter Named parameter