12. C# Compiler Attribute
• ObsoleteAttribute marks code as being
deprecated
• ConditionalAttribute removes code
when a pre-processing directive isn’t
defined
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
18. Deriving From Attribute
• Derive a new class from Attribute,
a .NET Framework type
• Give your class a suffix of “Attribute”
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. 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
23. Specifying Named and Positional
Parameters
• Positional parameters appear first
• Named parameters follow
24. Specifying Named and Positional
Parameters
• Positional parameters appear first
• Named parameters follow
• For:
25. Specifying Named and Positional
Parameters
• Positional parameters appear first
• Named parameters follow
• For:
[DllImport("shell32.dll", EntryPoint="FindExecutable")]
26. Specifying Named and Positional
Parameters
• Positional parameters appear first
• Named parameters follow
• For:
[DllImport("shell32.dll", EntryPoint="FindExecutable")]
• “shell32.dll” is positional
27. Specifying Named and Positional
Parameters
• Positional parameters appear first
• Named parameters follow
• For:
[DllImport("shell32.dll", EntryPoint="FindExecutable")]
• “shell32.dll” is positional
• EntryPoint="FindExecutable" is named
32. Decorating with
AttributeUsageAttribute
• Three Parameters
• AllowMultiple – can use attribute more
than once
• Inherited – applies to derived classes
• ValidOn – defines where allowed to use
42. Exploring Type Members
• Once you have a type, you can query
its members:
• GetFields
• GetMethods
43. Exploring Type Members
• Once you have a type, you can query
its members:
• GetFields
• GetMethods
• GetProperties
44. Exploring Type Members
• Once you have a type, you can query
its members:
• GetFields
• GetMethods
• GetProperties
• GetXxx
45. Exploring Type Members
• Once you have a type, you can query
its members:
• GetFields
• GetMethods
• GetProperties
• GetXxx
• Continue to explore as you need
48. Dynamic Invocation
• You can dynamically invoke/run
methods discovered through reflection
• Get object reference, if instance
49. Dynamic Invocation
• You can dynamically invoke/run
methods discovered through reflection
• Get object reference, if instance
• Specify parameters
50. Dynamic Invocation
• You can dynamically invoke/run
methods discovered through reflection
• Get object reference, if instance
• Specify parameters
• Call Invoke(…)
51. Dynamic Invocation
• You can dynamically invoke/run
methods discovered through reflection
• Get object reference, if instance
• Specify parameters
• Call Invoke(…)
• Code runs
57. Using Dynamic Types
• Declare type as dynamic
• Can call any member
• Can assign to any member
58. Using Dynamic Types
• Declare type as dynamic
• Can call any member
• Can assign to any member
• No compile-time error
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
62. The Expando Object
• Lets you add/remove members
dynamically
• Can pass object reference as
parameter
63. The Expando Object
• Lets you add/remove members
dynamically
• Can pass object reference as
parameter
• Use members dynamically too
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
68. Implementing
• Derive from DynamicObject
• Overrides let you do anything
dynamically
• Examples of methods you can override
69. Implementing
• Derive from DynamicObject
• Overrides let you do anything
dynamically
• Examples of methods you can override
• TrySetMember
70. Implementing
• Derive from DynamicObject
• Overrides let you do anything
dynamically
• Examples of methods you can override
• TrySetMember
• TryGetMember
71. Implementing
• Derive from DynamicObject
• Overrides let you do anything
dynamically
• Examples of methods you can override
• TrySetMember
• TryGetMember
• TryInvokeMember