Module 14: Attributes. Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values.

Slides:



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

Structures Spring 2013Programming and Data Structure1.
Attributes Programming in C# Attributes CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
AP 08/01 Component Programming with C# and.NET 1st Class Component Support Robust and Versionable Creating and using attributes API integration –DLL import.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Reflection, Conversions, and Exceptions Tom Roeder CS fa.
MD. SAIFULLAH AL AZAD SPEAKER, TECH FORUM USER GROUP Previously Speak on: IIS 7 Available in facebook:
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Lecture Roger Sutton CO530 Automation Tools 5: Class Libraries and Assemblies 1.
.NET Framework Introduction: Metadata
Java2C# Antonio Cisternino Part III. Outline Classes  Fields  Properties  virtual methods  new names  operator overloading Reflection  Custom attributes.
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
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
.NET Framework Danish Sami UG Lead.NetFoundry
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
Reflection.NET Support for Reflection. What is Reflection Reflection: the process by which a program can observe and modify its own structure and behavior.
Introduction to Generics
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Mechanisms for Reuse CMPS OOP billed as technology that permits software to be constructed from general-purpose reusable components Two main mechanisms.
Attributes C#.Net Software Development Version 1.0.
Introduction to Object-Oriented Programming Lesson 2.
Rachana George.NET Security, Summer Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke.
Special Features of C# : Delegates, Events and Attributes.
SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Reflection Programming under the hood SoftUni Team Technical Trainers Software University
Chapter 14 Using JavaBeans Components in JSP Documents.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
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.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
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.
Modern Programming Tools And Techniques-I
Java Generics.
2.7 Inheritance Types of inheritance
Advanced .NET Programming I 7th Lecture
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.
CS360 Windows Programming
Interface.
.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.
Classes & Objects: Examples
Java Programming Course
Module 10: Implementing Managed Code in the Database
CIS 199 Final Review.
Jim Fawcett CSE775 – Distributed Objects Spring 2006
Advanced .NET Programming I 8th Lecture
Presentation transcript:

Module 14: Attributes

Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values

 Overview of Attributes Introduction to Attributes Applying Attributes Common Predefined Attributes Using the Conditional Attribute Using the DllImport Attribute Using the Transaction Attribute

Introduction to Attributes Attributes are: Declarative tags that convey information to the runtime Stored with the metadata of the element.NET Framework provides predefined attributes The runtime contains code to examine values of attributes and act on them

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 For some elements such as assemblies, specify the element name associated with the attribute explicitly [attribute(positional_parameters,named_parameter=value,...)] element [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 Conditional Attribute Serves as a debugging tool Causes conditional compilation of method calls, depending on the value of a programmer-defined symbol Does not cause conditional compilation of the method itself Restrictions on methods Must have return type of void Must not be declared as override Must not be from an inherited interface using System.Diagnostics;... class MyClass { [Conditional ("DEBUGGING")] public static void MyMethod( ) {... } using System.Diagnostics;... class MyClass { [Conditional ("DEBUGGING")] public static void MyMethod( ) {... }

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 using System.Runtime.InteropServices;... public class MyClass( ) { [DllImport("MyDLL.dll", EntryPoint="MyFunction")] public static extern int MyFunction(string param1);... int result = MyFunction("Hello Unmanaged Code");... } using System.Runtime.InteropServices;... public class MyClass( ) { [DllImport("MyDLL.dll", EntryPoint="MyFunction")] public static extern int MyFunction(string param1);... int result = MyFunction("Hello Unmanaged Code");... }

Using the Transaction Attribute To manage transactions in COM+ Specify that your component be included when a transaction commit is requested Use a Transaction attribute on the class that implements the component using System.EnterpriseServices;... [Transaction(TransactionOption.Required)] public class MyTransactionalComponent {... } using System.EnterpriseServices;... [Transaction(TransactionOption.Required)] public class MyTransactionalComponent {... }

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

Defining Custom Attribute Scope Use the AttributeUsage tag to define scope Example Use the bitwise “or” operator ( | ) to specify multiple elements Example [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class MyAttribute: System.Attribute {... } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class MyAttribute: System.Attribute {... } [AttributeUsage(AttributeTargets.Method)] public class MyAttribute: System.Attribute {... } [AttributeUsage(AttributeTargets.Method)] public class MyAttribute: System.Attribute {... }

Defining an Attribute Class 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

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 object 5. Checks for a named parameter 6. Sets field or property to named parameter value 7. Saves current state of attribute class

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 object by using System.Type Create a System.Type object by using the typeof operator Example System.Reflection.MemberInfo typeInfo; typeInfo = typeof(MyClass); System.Reflection.MemberInfo typeInfo; typeInfo = typeof(MyClass);

Querying for Attribute Information To retrieve 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 System.Reflection.MemberInfo typeInfo; typeInfo = typeof(MyClass); object[ ] attrs = typeInfo.GetCustomAttributes(false); System.Reflection.MemberInfo typeInfo; typeInfo = typeof(MyClass); object[ ] attrs = typeInfo.GetCustomAttributes(false);

Lab 14.1: Defining and Using Attributes

Review Overview of Attributes Defining Custom Attributes Retrieving Attribute Values

Course Evaluation