.NET Attributes and Reflection - What a Developer Needs to Know...


Published on

An overview of .NET Attributes and Reflection. Pro's, Con's, and when to use them along with a practical demo of .NET reflection in use.

Video demos can be found here:

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Custom Attributes are created by the developer to be used for his or her own purpose or requirements
  • Reflection – we will talk about that in the upcoming slidesUseful to easily provide additional data to a target without having to write a lot of additional code-Example: An attribute that specifies what a Display or Friendly Name should be for a field or a Tooltip – this functionality can be added to the property of the class simply by using a custom attribute
  • Metadata - - info about the data about the types, code, assembly
  • -The PropertyGrid uses the information to group Properties by Category and also use a friendly display name-3rd Part Grid controls typically use Display Name to display a friendly column name
  • Metadata - - info about the data about the types, code, assembly
  • Metadata - - info about the data about the types, code, assembly
  • Assembly Load assemblies at runtime Locate the types used within the assemblyConstructorInfo Discover attributes of a class constructor Access to constructor metadata Invoke a ConstructorMethodInfoDiscover attributes of a method Access to method metadata Invoke a method (late binding)
  • CustomAttributeData – used for getting access to custom attribute information from Classes, Properties, Methods, etc
  • Microsoft Intermediate LanguageSome primary uses include Script Engines and CompilersThis can have a speed increase in certain scenarios – for example when doing a lot of looping within a calculation could have a speed increase if you generate IL code of the entire calculation and then just do the calculation.. No looping. Only recommended in very advanced and specialized scenarios.
  • |Quick and Dirty User Interfaces – could be expanded by using custom attributes that specify size, read-only, color, label name, etc
  • Think about how you use reflection because there is a performance hitASP.NET – There may not be a performance impact using reflection with just a few users, but it may have a largely magnified impact with higher workloads…Example: If you know the interface of the type of object you are instantiating using reflection (because you don’t know the name of the object until runtime) you can eliminate further reflection calls by casting the new instance of the Interface to an object declared of that interface type. Method calls to interface methods will not require a reflection call because you know the methods of the Interface.Don’t avoid reflection due to performance concerns – rather use it where it makes sense  Even the .NET Framework uses Reflection internally in the framework.
  • .NET Attributes and Reflection - What a Developer Needs to Know...

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