Attributes C#.Net Software Development Version 1.0.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Fields, Constructors, Methods
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Attributes Programming in C# Attributes CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Reflection, Conversions, and Exceptions Tom Roeder CS fa.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
.NET Framework Introduction: Metadata
1 Namespaces and Assemblies © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++
Other Types in OOP Enumerations, Structures, Generic Classes, Attributes Svetlin Nakov Technical Trainer Software University
Writing Classes (Chapter 4)
Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam.
The Java Programming Language
Fun with Java Annotations Brian McGinnis. Java Annotations Introduced in “Tiger” release (Java 1.5) Introduced in “Tiger” release (Java 1.5) One of most.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
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.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Reflection.NET Support for Reflection. What is Reflection Reflection: the process by which a program can observe and modify its own structure and behavior.
Module 14: Attributes. Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Lambda Expressions Version 1.0
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
Special Features of C# : Delegates, Events and Attributes.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Reflection Programming under the hood SoftUni Team Technical Trainers Software University
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Reflection and Attributes.
Version 1.0 C#.Net Software Development. Overview Namespaces using Statement Pre-Processor Directives 2Copyright © by Dennis A. Fairclough all.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Lambda Expressions Version 1.1
More Sophisticated Behavior
Static data members Constructors and Destructors
Examples of Classes & Objects
2.7 Inheritance Types of inheritance
Advanced .NET Programming I 7th Lecture
CS360 Windows Programming
Lecture 22 Inheritance Richard Gesick.
.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.
Classes & Objects: Examples
Java Programming Course
How to organize and document your classes
CIS 199 Final Review.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Advanced .NET Programming I 8th Lecture
Presentation transcript:

Attributes C#.Net Software Development Version 1.0

Overview  Attributes Provide Declarative Metadata  Must inherit from System.Attribute  Conventions  TestAttribute class  ildasm utility Copyright © by Dennis A. Fairclough all rights reserved.

Attributes (C# Standard 1.2/3.0)  Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality.  Key to such components is that they present a programming model with properties, methods, and events; They have attributes that provide declarative information about the component; and they incorporate their own documentation. Attributes are such and provide declarative metadata Copyright © by Dennis A. Fairclough all rights reserved.

Declarative Information (Attributes)  C# generalizes this capability such that user-defined types of declarative information can be attached to program entities and retrieved at runtime. C# programs specify this additional declarative information by defining and using attributes. Copyright © by Dennis A. Fairclough all rights reserved.

System.Attribute  All attribute classes must derive from the System.Attribute base class provided by the.NET Framework.  If an attribute’s name ends in Attribute, that part of the name can be omitted when the attribute is referenced i.e. TestAttritbute or Test Copyright © by Dennis A. Fairclough all rights reserved.

What are Attributes?  Declarative Information Provider (Decorator Pattern)  Attributes encapsulate meta information about assemblies types (classes, structs, enums, etc.) data and method members  Attributes can indicate Threading Model Security Access COM Access style How a type is to be used Serialization style Custom information  Attribute information is stored in the Assembly Manifest

An User-Defined Attribute Class public class TestAttritbute : Attribute { } Copyright © by Dennis A. Fairclough all rights reserved.

Example TestAttribute public class TestAttribute : Attribute { private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private string _Mode; public string Mode { get { return _Mode; } set { _Mode = value; } } public TestAttribute(string name) { _Name = name; _Mode = " "; } }//END class TestAttribute Copyright © by Dennis A. Fairclough all rights reserved.

Attribute Usage [Test("Dennis", Mode="ReadOnly")] class Program { static void Main(string[] args) { MemberInfo member = typeof(Program); TestAttribute mattr = (TestAttribute)Attribute.GetCustomAttribute(member,typeof(TestAttribute) ) as TestAttribute; if (mattr == null) Console.WriteLine("No attribute information"); else Console.WriteLine("TestAttribute for: {0} with {1} and {2}",member,mattr.Name,mattr.Mode); Console.Read(); }//END Main() }//END class Program Attribute (implied) Attribute Property Copyright © by Dennis A. Fairclough all rights reserved.

ildasm (MSIL Dissassembler)  ildasm Copyright © by Dennis A. Fairclough all rights reserved.

FlagsAttribute (Flags)  Used to modify the behavior of an enum Use reflector (or another reflection tool) to view System.Enum  Look at ToString (which has to test for the FlagsAttribute) (See Test_Attributes Demo)

Copyright © by Dennis A. Fairclough all rights reserved. Library Attributes  CLSCompliantAttribute  CategoryAttribute  ToolboxBitmapAttribute  FlagsAttribute  SecurityAttribute  WebMethodAttribute  And many more.

Copyright © by Dennis A. Fairclough all rights reserved. Attribute Usage  Attribute suffix can be omitted [FlagsAttribute] [Flags]  The attribute tag is actually a constructor call [Flags( )] [PrincipalPermission(SecurityAction.Demand)] Note: You can omit the parens if there are no params  Named constructor parameters: Set Public property values  [PrincipalPermission( SecurityAction.Demand, Name="DEMO/demo", Role="User")]  Property accesses must follow actual parameters

Copyright © by Dennis A. Fairclough all rights reserved. Attribute Usage  Attributes can be used on Assemblies Types Data & Method Members  Attribute usage can be restricted to one type of entity See AttributeTargets enum

Copyright © by Dennis A. Fairclough all rights reserved. Assembly Level Attributes  In the AssemblyInfo.cs file

Copyright © by Dennis A. Fairclough all rights reserved. Querying Attribute Info  Use reflection to access attributes Type.GetCustomAttributes MemberInfo.GetCustomAttributes MethodInfo.GetCustomAttributes MemberInfo.IsDefined etc.

Copyright © by Dennis A. Fairclough all rights reserved. Custom Attribute  Must ultimately inherit from System.Attribute  Limit where the attribute can be used using the AttributeUsageAttribute ValidOn (positional parameter)  AttributeTargets enum AllowMultiple (named parameter)  bool (default is false) Inherited (named parameter)  bool (default is true)

Copyright © by Dennis A. Fairclough all rights reserved. The Conditional Attribute  Allows you to define conditional methods Calls to this method are based upon whether the conditional symbol is defined at the time of the call  Parameter(s) are not evaluated if the call is not made Similar to #define DEBUG

Copyright © by Dennis A. Fairclough all rights reserved. ObsoleteAttribute  Tag an entity (Class, Struct, Method, etc.) to generate a compile-time warning or error if that entity is used.

Copyright © by Dennis A. Fairclough all rights reserved. Demo See Attributes Example Code

Copyright © by Dennis A. Fairclough all rights reserved. What did you learn?  ??