Your SlideShare is downloading. ×
.NET Attributes and Reflection - What a Developer Needs to Know...
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

3,807

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

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:
http://dandouglas.wordpress.com/talks-webcasts/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,807
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
48
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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.
  • 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
      • 6. 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
    • 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
      Revertcancel 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

    ×