• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Attributes, reflection, and dynamic programming
 

Attributes, reflection, and dynamic programming

on

  • 943 views

All about Attributes, Reflection, and Dynamic Programming

All about Attributes, Reflection, and Dynamic Programming

Statistics

Views

Total Views
943
Views on SlideShare
942
Embed Views
1

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 1

http://www.pinterest.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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

Attributes, reflection, and dynamic programming Attributes, reflection, and dynamic programming Presentation Transcript

  • Attributes, Reflection, andDynamic Programming Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Decorating Code With
  • Decorating Code With• Demo
  • Examples ofApplications That Use Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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-processing directive isn’t defined
  • 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
  • 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” • i.e. TestAttribute
  • 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
  • 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("shell32.dll", EntryPoint="FindExecutable")]
  • Specifying Named and PositionalParameters• Positional parameters appear first• Named parameters follow• For:[DllImport("shell32.dll", EntryPoint="FindExecutable")] • “shell32.dll” is positional
  • 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
  • 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 • Inherited – applies to derived classes
  • Decorating withAttributeUsageAttribute• Three Parameters • AllowMultiple – can use attribute more than once • Inherited – applies to derived classes • ValidOn – defines where allowed to use
  • 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 support
  • 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 • GetXxx
  • Exploring Type Members• Once you have a type, you can query its members: • GetFields • GetMethods • GetProperties • GetXxx• Continue to explore as you need
  • 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 instance
  • Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance • Specify parameters
  • Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance • Specify parameters • Call Invoke(…)
  • Dynamic Invocation• You can dynamically invoke/run methods discovered through reflection • Get object reference, if instance • Specify parameters • Call Invoke(…) • Code runs
  • 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• Errors surface as runtime exceptions
  • 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 dynamically too
  • 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
  • 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 • TrySetMember
  • Implementing• Derive from DynamicObject• Overrides let you do anything dynamically• Examples of methods you can override • TrySetMember • TryGetMember
  • Implementing• Derive from DynamicObject• Overrides let you do anything dynamically• Examples of methods you can override • TrySetMember • TryGetMember • TryInvokeMember
  • 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 Application Developers Training Company