160 likes | 180 Views
.NET Attributes and Reflection. “What a developer needs to know……”. Presented By. Dan Douglas Blog: http://dandouglas.wordpress.com Twitter: @ Dan_Douglas E-mail: dan.douglas@gmail.com. What are Attributes?. A .NET Object
E N D
.NET Attributes and Reflection “What a developer needs to know……” Presented By Dan Douglas Blog: http://dandouglas.wordpress.com Twitter: @Dan_Douglas E-mail: dan.douglas@gmail.com
What are Attributes? • A .NET Object • Represents data you want to associate with a target within the assembly • Many possible targets, including, Assembly, Class, Member, Constructor, Enum, Interface, and Event • Intrinsic Attributes (part of the CLR) • Custom Attributes (developed)
…..Attributes • Create a custom attribute by creating a class that inherits from System.Attribute • Attributes are accessed by the application using reflection to get attribute information • Useful to easily provide additional data to a target without having to write a lot of additional code
…..Attributes • Multiple attributes can be assigned to a single target • Many attributes in the System.ComponentModel namespace used by UI controls • Provides information like Visibility, and Display Name
How to Specify an Attribute • In this example the LastName Property is given the category of Main Info and the Display Name of Last Name • UI Controls such as grids or the PropertyGrid use this information
What is Reflection? • “In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior” (from Wikipedia) • Application can read it’s own metadata • Late-binding access to objects without knowing the information at design time • Example: instantiate a class located outside the project by name or call a method of a class by name
Reflection Usage In .NET • View metadata within an assembly • Type Discovery • Look at types within an assembly and also be able to instantiate them and use them • Late Binding • Dynamically instantiate types • Invoke properties and methods dynamically from dynamically instantiated classes • Create your own types and IL at runtime using Reflection.Emit (advanced)
From a Technical Perspective… • Reflection objects are available in the System.Reflection namespace • Some of the classes available in the reflection namespace: • Assembly • ConstructorInfo • MethodInfo
From a Technical Perspective… • Some of the classes available in the reflection namespace: • EventInfo • PropertyInfo • ParameterInfo • CustomAttributeData
From a Technical Perspective… • System.Reflection.Emit Namespace • Advanced Level of Reflection • Used in Very Specialized Scenarios • Dynamically build assemblies and types • Allows you to generate and execute.NET (IL-Intermediate Language) code on demand at runtime
Red Gate .NET Reflector • Useful utility that uses reflection to get information about .NET assemblies • Allows you to view, navigate, and search through the class hierarchies of .NET assemblies • Look at the code behind the objects in .NET Framework classes or any .NET compiled components to see how they work • Because reflection allows access to private members, these members are visible within .NET reflector • (Demo - .NET Reflector)
Many Practical Uses….. • Pluggable Application Architectures • At runtime, load modules such as UI components into the application • Business Objects • Revert\cancel changes to a business object by cycling through its properties using reflection • Quick and Dirty User Interfaces • Get properties of a business object and add appropriate labels/text boxes to a form at runtime using property names
Reflection Performance • Reflection is faster in .NET 2.0 and higher than it was in .NET 1.1 • When using reflection on a server (ex: ASP.NET) special concern should be taken to ensure that performance won’t be impacted with much higher workloads • In situations where you do not know the object at design time, use a standard interface when possible • You can avoid further reflection calls once you have a reference to the object by calling the methods of the interface • Don’t avoid reflection due to performance concerns – rather use it where it makes sense
Demo….. Use reflection to dynamically load an assembly located outside of the project and access and invoke one its methods
Resources • Hanselminutes Podcast #37 – Reflectionhttp://www.hanselminutes.com/default.aspx?showID=37 • O’Reilly – Programming C# Chapter 18 (Attributes And Reflection)http://oreilly.com/catalog/progcsharp/chapter/ch18.html • Dodge Common Performance Pitfalls to Craft Speedy Applicationshttp://msdn.microsoft.com/en-us/magazine/cc163759.aspx • Real-world Reflection in .NEThttp://www.devx.com/dotnet/Article/31578/1954 • My Blog (Dan Douglas) • http://dandouglas.wordpress.com
Questions? Dan Douglas Blog: http://dandouglas.wordpress.com Twitter: @Dan_Douglas E-mail: dan.douglas@gmail.com • Consulting Inquiries? (519) 777-2258