introduction to microsoft dot net framework and custom ...ctchen/pdf/itmdfaca.pdf · introduction...

67
Introduction to Microsoft Dot Net Framework and Custom Attributes 陳建村 2003/03/13 [email protected]

Upload: others

Post on 15-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Introduction to Microsoft Dot Net Framework and

Custom Attributes

陳建村

2003/03/[email protected]

Page 2: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

References

J. Newkirk & A. A. Vorontsov, “How .NET’s Custom Attributes Affect Design”, IEEE Software, Sep/Oct 2002.Bertrand Meyer, “.NET is Coming, ” IEEE Computer, August 2001.J. Liberty, Programming C#, O’Reilly, 2002.J. Liberty & D. Hurwitz, Programming ASP.NET, O’Reilly, 2002.J. Richter, Applied Microsoft .NET Framework Programming, MS Press, 2002.

Page 3: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Outline

.NET Platform OverviewCustom Attributes & Their Applications

BugFixAttributeDesign By ContractWeb ServicesConditionalAttributeNUnit 2.0

Conclusion

Page 4: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Part I: .NET Platform Overview

Page 5: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Features of the .NET Platform (1/2)

Multi-language developmentC#, C++, VB.NET, JScript, J#, Eiffel, Perl, ML, etc.

Platform and processor independenceMicrosoft Intermediate Language (MSIL)

Automatic memory managementGarbage Collection (GC)

Version supportNo more “DLL Hell”

Support for open standardsSOAP, UDDI, WSDL, XML, ECMA standards, etc.

Page 6: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Features of the .NET Platform (2/2)

Easy deploymentXcopy deployment, and no registry

Distributed architectureWeb services

Interoperability with unmanaged codeCalling DLL function, using COM components, and calling .NET services from COM components

SecurityPerformance and Scalability

Page 7: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Microsoft.NET Framework

Base Class Library

Common Language Specification

Common Language Runtime (CLR)

Data and XML

VB C++ C#Visual Studio.N

ETJScript …

WebServices

UserInterface

Page 8: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

The .NET Architecture (Meyer)

2-1

3-1

2-2

2-3

1

2

3

4

5

6

Page 9: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Web services

The top layer provides .NET users-persons and companies-with Web services for e-commerce and business-to-business applications.

Page 10: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Frameworks and libraries

A set of frameworks and libraries provides the most immediately attractive aspect for developers. These include ASP.NET, active server pages for developing smart Web sites and services; ADO.NET, and XML-based improvement to ActiveX Data Objects, for databases and object-relational processing; and Windows Forms for graphics. Altogether, .NET contains thousands of reusable components.

Page 11: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Development environment

The new Visual Studio.Net provides the tools of most direct use to developers: A common software development, compilation, browsing, and debugging shared by many languages. This environment allows third-party vendors to plug in tools and compilers for other languages.

Page 12: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Component model

Before .NET there were already three major contenders for leadership in the field of models and standards for component: CORBA, J2EE and COM.With .NET you can build assemblies, each consisting of a number of classes with well-defined interfaces.

Page 13: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Component model (2/2)ILDASM

Page 14: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Object model

The object model provides the conceptual basis on which everything else rests, in particular, .NET's OO type system. The common language specification defines restrictions ensuring language operability.

Page 15: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Common language runtime

A

Page 16: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Basic class library

Page 17: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Visual Studio.Net

Page 18: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Visual Studio.Net- new project

Page 19: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Visual Studio.Net- Win App. (1/3)

B

A

Page 20: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Visual Studio.Net- Win App. (2/3)

Page 21: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Visual Studio.Net- Win App. (3/3)

Page 22: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

ASP.NET

Web controlsServer side web controlsAutomatically state maintenance

View stateSession state

Compiled approachClosing the gap (web form)

With .NET, a Web page is a program, and a program can easily become a Web page

Demo

Page 23: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

ASP.NET- Demo

Page 24: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

ASP.NET- Demo

Page 25: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

ASP.NET- Demo

Page 26: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

ASP.NET- Demo

Page 27: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Security

Type verificationOrigin verificationA fine-grained permission mechanismA notion of "principal"

Page 28: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Beyond Windows

The open source project- Mono:http://www.go-mono.comLasted version: 0.23 (2003/03/07)

Mono includes: a compiler for the C# language, a runtime for the Common Language Infrastructure (also referred as the CLR) and a set of class libraries. The runtime can be embedded into your application.

Mono has implementations of both ADO.NET and ASP.NET as part of its distribution.

Page 29: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw
Page 30: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Part II: Custom Attributes

Page 31: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

What is Custom Attributes?

Attributes, such as public, private, staticcan be applied to types and members.How about defining our own attributes?Custom attributes is a way to associate additional information with a target.

The complier detects the attributes in the source code and emits the corresponding metadata.

Page 32: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Custom attributes example-in C#

Page 33: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Custom attributes example-in VB.Net

Page 34: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Custom attributes’s targetsAllAssemblyClassConstructorDelegateEnumEventField

InterfaceMethodModuleParameterPropertyReturn ValueStruct

Page 35: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

The System.Attribute class

A custom attribute is simply an instance of a type. Custom attribute types must derived from System.Attribute class (directly or indirectly)The type must have a public constructor to create an instance of it. When applying an attribute to a target, the syntax is similar to that for calling one of the type’s instance constructors.

Page 36: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Example- BugFixAttribute

Page 37: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Real example- BugFixAttribute

Page 38: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

MyMath

Page 39: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Tester

Page 40: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Output

Page 41: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Example- Design By Contract

Page 42: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

DBC in Eiffel

precondition

postcondition

Page 43: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

DBC in Java (iContract)

Page 44: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

DBC in .NET platform (1/2)

Page 45: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

DBC in .NET platform (2/2)

Page 46: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Example- Web Services

Page 47: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

A Simple StockTicker

The StockTicker web service exposes two web methods:

GetNameExpects a stock symbol as an argument and returns a string containing the name of the stock.

GetPriceExpects a stock symbol as an argument and returns a number containing the current price of the stock.

Page 48: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Source Code (1/2)

Page 49: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Source Code (2/2)

Page 50: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Two web methods

Page 51: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

The WebMethod attribute

It’s not necessary for the WebService class to expose all of its methods to consumers of the web service. Each method we want to expose must:

Be declared as public.Have the WebMethod attribute placed before the method declaration.

Page 52: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Invoke GetName (1/2)

Page 53: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Invoke GetName (2/2)

Page 54: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Invoke GetPrice (1/2)

Page 55: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Invoke GetPrice (2/2)

Page 56: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

StockTicker’s WSDL

Page 57: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Example- ConditionalAttribute

Page 58: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Demo ConditionalAttribute

Page 59: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Output

Page 60: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Specifying a preprocessor symbol in VS.NET

Page 61: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Output

Page 62: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Example- NUnit 2.0

Page 63: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

JUnit review

Page 64: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Custom attributes in NUnit 2.0

Page 65: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Conclusion

We now understand the major features of the .NET platform.JSR 175- A Metadata Facility for the Java Programming Language:

A metadata facility for the Java Programming Language would allow classes, interfaces, fields, and methods to be marked as having particular attributes.http://www.jcp.org/en/jsr/detail?id=175

Page 66: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

Q&A

Page 67: Introduction to Microsoft Dot Net Framework and Custom ...ctchen/pdf/ITMDFACA.pdf · Introduction to Microsoft Dot Net Framework and Custom Attributes 뎯ꯘꟸ 2003/03/13 ctchen@ctchen.idv.tw

The .NET program execution modelMulti-language

Platform independence

Automatic memory management

*

Common Language Runtime (CLR)