Your SlideShare is downloading. ×
Attributes, Reflection, andDynamic Programming     Learn More @ http://www.learnnowonline.com        Copyright © by Applica...
Decorating Code With
Decorating Code With• Demo
Examples ofApplications That Use       Learn More @ http://www.learnnowonline.com          Copyright © by Application Deve...
Examples ofApplications That Use• Attributes are meta-data you can add  to code         Learn More @ http://www.learnnowon...
Examples ofApplications That Use• Attributes are meta-data you can add  to code• Examples of applications:         Learn M...
Examples ofApplications That Use• Attributes are meta-data you can add  to code• Examples of applications:  • The C# compi...
Examples ofApplications That Use• Attributes are meta-data you can add  to code• Examples of applications:  • The C# compi...
Examples ofApplications That Use• Attributes are meta-data you can add  to code• Examples of applications:  • The C# compi...
C# Compiler Attribute
C# Compiler Attribute• ObsoleteAttribute marks code as being  deprecated
C# Compiler Attribute• ObsoleteAttribute marks code as being  deprecated• ConditionalAttribute removes code  when a pre-pr...
C# Compiler Attribute• ObsoleteAttribute marks code as being  deprecated• ConditionalAttribute removes code  when a pre-pr...
Creating New Attributes
Creating New Attributes• Demo
Deriving From Attribute
Deriving From Attribute• Derive a new class from Attribute,  a .NET Framework type
Deriving From Attribute• Derive a new class from Attribute,  a .NET Framework type• Give your class a suffix of “Attribute”
Deriving From Attribute• Derive a new class from Attribute,  a .NET Framework type• Give your class a suffix of “Attribute...
Deriving From Attribute• Derive a new class from Attribute,  a .NET Framework type• Give your class a suffix of “Attribute...
Specifying Named and PositionalParameters
Specifying Named and PositionalParameters• Positional parameters appear first
Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow
Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:
Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("she...
Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("she...
Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("she...
Decorating withAttributeUsageAttribute
Decorating withAttributeUsageAttribute• Three Parameters
Decorating withAttributeUsageAttribute• Three Parameters  • AllowMultiple – can use attribute more   than once
Decorating withAttributeUsageAttribute• Three Parameters  • AllowMultiple – can use attribute more    than once  • Inherit...
Decorating withAttributeUsageAttribute• Three Parameters  • AllowMultiple – can use attribute more    than once  • Inherit...
Using Reflection
Using Reflection• Demo
Getting Type and TypeInfo
Getting Type and TypeInfo• Reflection API split into two types
Getting Type and TypeInfo• Reflection API split into two types• Type provides reference support
Getting Type and TypeInfo• Reflection API split into two types• Type provides reference support• TypeInfo offers execution ...
Exploring Type Members
Exploring Type Members• Once you have a type, you can query  its members:
Exploring Type Members• Once you have a type, you can query  its members:  • GetFields
Exploring Type Members• Once you have a type, you can query  its members:  • GetFields  • GetMethods
Exploring Type Members• Once you have a type, you can query  its members:  • GetFields  • GetMethods  • GetProperties
Exploring Type Members• Once you have a type, you can query  its members:  • GetFields  • GetMethods  • GetProperties  • G...
Exploring Type Members• Once you have a type, you can query  its members:  • GetFields  • GetMethods  • GetProperties  • G...
Dynamic Invocation
Dynamic Invocation• You can dynamically invoke/run  methods discovered through reflection
Dynamic Invocation• You can dynamically invoke/run  methods discovered through reflection  • Get object reference, if insta...
Dynamic Invocation• You can dynamically invoke/run  methods discovered through reflection  • Get object reference, if insta...
Dynamic Invocation• You can dynamically invoke/run  methods discovered through reflection  • Get object reference, if insta...
Dynamic Invocation• You can dynamically invoke/run  methods discovered through reflection  • Get object reference, if insta...
Coding Dynamic Types
Coding Dynamic Types• Demo
Using Dynamic Types
Using Dynamic Types• Declare type as dynamic
Using Dynamic Types• Declare type as dynamic• Can call any member
Using Dynamic Types• Declare type as dynamic• Can call any member• Can assign to any member
Using Dynamic Types•   Declare type as dynamic•   Can call any member•   Can assign to any member•   No compile-time error
Using Dynamic Types•   Declare type as dynamic•   Can call any member•   Can assign to any member•   No compile-time error...
The Expando Object
The Expando Object• Lets you add/remove members  dynamically
The Expando Object• Lets you add/remove members  dynamically• Can pass object reference as  parameter
The Expando Object• Lets you add/remove members  dynamically• Can pass object reference as  parameter• Use members dynamic...
The Expando Object• Lets you add/remove members  dynamically• Can pass object reference as  parameter• Use members dynamic...
Implementing
Implementing• Derive from DynamicObject
Implementing• Derive from DynamicObject• Overrides let you do anything  dynamically
Implementing• Derive from DynamicObject• Overrides let you do anything  dynamically• Examples of methods you can override
Implementing• Derive from DynamicObject• Overrides let you do anything  dynamically• Examples of methods you can override ...
Implementing• Derive from DynamicObject• Overrides let you do anything  dynamically• Examples of methods you can override ...
Implementing• Derive from DynamicObject• Overrides let you do anything  dynamically• Examples of methods you can override ...
Questions?     Learn More @ http://www.learnnowonline.com        Copyright © by Application Developers Training Company
Questions?           http://   www.LearnNowOnline.com     Learn More @ http://www.learnnowonline.com        Copyright © by...
Upcoming SlideShare
Loading in...5
×

Attributes, reflection, and dynamic programming

856

Published on

All about Attributes, Reflection, and Dynamic Programming

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
856
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Attributes, reflection, and dynamic programming"

    1. 1. Attributes, Reflection, andDynamic Programming Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    2. 2. Decorating Code With
    3. 3. Decorating Code With• Demo
    4. 4. Examples ofApplications That Use Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    5. 5. Examples ofApplications That Use• Attributes are meta-data you can add to code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    6. 6. Examples ofApplications That Use• Attributes are meta-data you can add to code• Examples of applications: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    7. 7. Examples ofApplications That Use• Attributes are meta-data you can add to code• Examples of applications: • The C# compiler Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    8. 8. Examples ofApplications That Use• Attributes are meta-data you can add to code• Examples of applications: • The C# compiler • Web Services Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    9. 9. Examples ofApplications That Use• Attributes are meta-data you can add to code• Examples of applications: • The C# compiler • Web Services • Unit testing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    10. 10. C# Compiler Attribute
    11. 11. C# Compiler Attribute• ObsoleteAttribute marks code as being deprecated
    12. 12. C# Compiler Attribute• ObsoleteAttribute marks code as being deprecated• ConditionalAttribute removes code when a pre-processing directive isn’t defined
    13. 13. C# Compiler Attribute• ObsoleteAttribute marks code as being deprecated• ConditionalAttribute removes code when a pre-processing directive isn’t defined• DllImportAttribute enables calling Windows OS APIs
    14. 14. Creating New Attributes
    15. 15. Creating New Attributes• Demo
    16. 16. Deriving From Attribute
    17. 17. Deriving From Attribute• Derive a new class from Attribute, a .NET Framework type
    18. 18. Deriving From Attribute• Derive a new class from Attribute, a .NET Framework type• Give your class a suffix of “Attribute”
    19. 19. Deriving From Attribute• Derive a new class from Attribute, a .NET Framework type• Give your class a suffix of “Attribute” • i.e. TestAttribute
    20. 20. Deriving From Attribute• Derive a new class from Attribute, a .NET Framework type• Give your class a suffix of “Attribute” • i.e. TestAttribute • Not required, but a common convention
    21. 21. Specifying Named and PositionalParameters
    22. 22. Specifying Named and PositionalParameters• Positional parameters appear first
    23. 23. Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow
    24. 24. Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:
    25. 25. Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("shell32.dll", EntryPoint="FindExecutable")]
    26. 26. Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("shell32.dll", EntryPoint="FindExecutable")] • “shell32.dll” is positional
    27. 27. Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("shell32.dll", EntryPoint="FindExecutable")] • “shell32.dll” is positional • EntryPoint="FindExecutable" is named
    28. 28. Decorating withAttributeUsageAttribute
    29. 29. Decorating withAttributeUsageAttribute• Three Parameters
    30. 30. Decorating withAttributeUsageAttribute• Three Parameters • AllowMultiple – can use attribute more than once
    31. 31. Decorating withAttributeUsageAttribute• Three Parameters • AllowMultiple – can use attribute more than once • Inherited – applies to derived classes
    32. 32. Decorating withAttributeUsageAttribute• Three Parameters • AllowMultiple – can use attribute more than once • Inherited – applies to derived classes • ValidOn – defines where allowed to use
    33. 33. Using Reflection
    34. 34. Using Reflection• Demo
    35. 35. Getting Type and TypeInfo
    36. 36. Getting Type and TypeInfo• Reflection API split into two types
    37. 37. Getting Type and TypeInfo• Reflection API split into two types• Type provides reference support
    38. 38. Getting Type and TypeInfo• Reflection API split into two types• Type provides reference support• TypeInfo offers execution support
    39. 39. Exploring Type Members
    40. 40. Exploring Type Members• Once you have a type, you can query its members:
    41. 41. Exploring Type Members• Once you have a type, you can query its members: • GetFields
    42. 42. Exploring Type Members• Once you have a type, you can query its members: • GetFields • GetMethods
    43. 43. Exploring Type Members• Once you have a type, you can query its members: • GetFields • GetMethods • GetProperties
    44. 44. Exploring Type Members• Once you have a type, you can query its members: • GetFields • GetMethods • GetProperties • GetXxx
    45. 45. Exploring Type Members• Once you have a type, you can query its members: • GetFields • GetMethods • GetProperties • GetXxx• Continue to explore as you need
    46. 46. Dynamic Invocation
    47. 47. Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection
    48. 48. Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance
    49. 49. Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance • Specify parameters
    50. 50. Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance • Specify parameters • Call Invoke(…)
    51. 51. Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance • Specify parameters • Call Invoke(…) • Code runs
    52. 52. Coding Dynamic Types
    53. 53. Coding Dynamic Types• Demo
    54. 54. Using Dynamic Types
    55. 55. Using Dynamic Types• Declare type as dynamic
    56. 56. Using Dynamic Types• Declare type as dynamic• Can call any member
    57. 57. Using Dynamic Types• Declare type as dynamic• Can call any member• Can assign to any member
    58. 58. Using Dynamic Types• Declare type as dynamic• Can call any member• Can assign to any member• No compile-time error
    59. 59. Using Dynamic Types• Declare type as dynamic• Can call any member• Can assign to any member• No compile-time error• Errors surface as runtime exceptions
    60. 60. The Expando Object
    61. 61. The Expando Object• Lets you add/remove members dynamically
    62. 62. The Expando Object• Lets you add/remove members dynamically• Can pass object reference as parameter
    63. 63. The Expando Object• Lets you add/remove members dynamically• Can pass object reference as parameter• Use members dynamically too
    64. 64. The Expando Object• Lets you add/remove members dynamically• Can pass object reference as parameter• Use members dynamically too• Get notified when add/remove occurs
    65. 65. Implementing
    66. 66. Implementing• Derive from DynamicObject
    67. 67. Implementing• Derive from DynamicObject• Overrides let you do anything dynamically
    68. 68. Implementing• Derive from DynamicObject• Overrides let you do anything dynamically• Examples of methods you can override
    69. 69. Implementing• Derive from DynamicObject• Overrides let you do anything dynamically• Examples of methods you can override • TrySetMember
    70. 70. Implementing• Derive from DynamicObject• Overrides let you do anything dynamically• Examples of methods you can override • TrySetMember • TryGetMember
    71. 71. Implementing• Derive from DynamicObject• Overrides let you do anything dynamically• Examples of methods you can override • TrySetMember • TryGetMember • TryInvokeMember
    72. 72. Questions? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    73. 73. Questions? http:// www.LearnNowOnline.com Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

    ×