1 / 16

.NET Attributes and Reflection

.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

overton
Download Presentation

.NET Attributes and Reflection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .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

  2. 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)

  3. …..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

  4. …..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

  5. 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

  6. 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

  7. 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)

  8. 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

  9. From a Technical Perspective… • Some of the classes available in the reflection namespace: • EventInfo • PropertyInfo • ParameterInfo • CustomAttributeData

  10. 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

  11. 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)

  12. 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

  13. 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

  14. Demo….. Use reflection to dynamically load an assembly located outside of the project and access and invoke one its methods

  15. 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

  16. Questions? Dan Douglas Blog: http://dandouglas.wordpress.com Twitter: @Dan_Douglas E-mail: dan.douglas@gmail.com • Consulting Inquiries? (519) 777-2258

More Related