c-jumpstart module 4
TRANSCRIPT
-
7/28/2019 C-Jumpstart Module 4
1/23
04 | Features of C#, Part 3Jerry Nixon | Microsoft Developer EvangelistDaren May | President & Co-founder, Crank211
-
7/28/2019 C-Jumpstart Module 4
2/23
Code Reflection and Information Working with Garbage Collection
Module Overview
-
7/28/2019 C-Jumpstart Module 4
3/23
MVA
Code Reflection and Information
-
7/28/2019 C-Jumpstart Module 4
4/23
Reflection inspects type metadata at runtime The type metadata contains information such as:
The type Name
The containing Assembly
Constructors
Properties
Methods Attributes
This data can be used to create instances, access valuexecute methods dynamically at runtime
What is Reflection?
-
7/28/2019 C-Jumpstart Module 4
5/23
Two methods: Statically at compile time
Dynamically at runtime
How do I get Type data?
-
7/28/2019 C-Jumpstart Module 4
6/23
There are two ways to dynamicallyinstantiate a type: Activator.CreateInstance
Calling Invoke on a ConstructorInfo object(advanced scenarios)
How can I create an instance of a Type?
-
7/28/2019 C-Jumpstart Module 4
7/23
Accessing a Property
-
7/28/2019 C-Jumpstart Module 4
8/23
Invoking a Method
-
7/28/2019 C-Jumpstart Module 4
9/23
Reflection (004)
-
7/28/2019 C-Jumpstart Module 4
10/23
MVA
Working with Garbage Collection
-
7/28/2019 C-Jumpstart Module 4
11/23
Garbage collection is automatic memory manageme De-referenced objects (orphans) are not collected imbut periodically.
Many factors influence Garbage Collection frequency
Not all orphans are collected at the same time
Garbage Collection is computationally expensive
What is Garbage Collection?
-
7/28/2019 C-Jumpstart Module 4
12/23
In most cases, let the Garbage Collectordo its thing. For a periodic activity it may makesense to force the collector to run:
Windows Service
Forcing Garbage Collection
-
7/28/2019 C-Jumpstart Module 4
13/23
If an object consumes many resources when instantia If you want to proactively free expensive resources
You dont want to force a full collection cycle.
Force Garbage Collection? Implement IDisposable.
But I want to help!
-
7/28/2019 C-Jumpstart Module 4
14/23
Some objects need explicit code torelease resources. The IDisposable interface marks thatthese types implement the Disposemethod. The simple dispose pattern works wellfor simple scenarios and sealed types
use the advanced pattern in most cases.
Disposable Objects
-
7/28/2019 C-Jumpstart Module 4
15/23
Use for any non-trivial disposableobject.
Advanced Dispose Pattern
-
7/28/2019 C-Jumpstart Module 4
16/23
The using keyword provides a usefulshortcut for invoking Dispose on typesthat implement IDisposable.
Using a shortcut
l
-
7/28/2019 C-Jumpstart Module 4
17/23
Close May be functionally the same as Dispose
May be a subset of the Dispose functionality
A closed object may be reopened IDbConnection
Stop is similar to Close May be restarted.
Timer, etc.
Dispose versus Close versus Stop
-
7/28/2019 C-Jumpstart Module 4
18/23
Streams and Dispose (005)
M L k
-
7/28/2019 C-Jumpstart Module 4
19/23
Despite having automatic memory management, it ispossible to create managed memory leaks.
Objects that fall out of scope may be referenced by oscope, keeping them alive. Events can be a common source of memory leaks:
Events can hold references to objects Solution! Unsubscribe from events proactively
Weak references can be used to avoid some memoryscenarios.
Memory Leaks
W k R f
-
7/28/2019 C-Jumpstart Module 4
20/23
Weak references create a reference that the Garbageignores.
The Garbage Collector will assume an object is eligibcollection if it is only referred to by weak references. To hold an object with only weak references, create a
variable referring to the weak reference value. This prevents collection until the local variable is out of sco
Weak References
-
7/28/2019 C-Jumpstart Module 4
21/23
Memory Leaks (036)
M d l R
-
7/28/2019 C-Jumpstart Module 4
22/23
Code Reflection and Information Working with Garbage Collection
Module Recap
-
7/28/2019 C-Jumpstart Module 4
23/23
2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trathe U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee tprovided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTA