Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.

Commit 2cb4f7b

Browse files
add IDisposable sample to Resources chapter
1 parent 3f73fa5 commit 2cb4f7b

File tree

7 files changed

+191
-1
lines changed

7 files changed

+191
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>4e3c803f-5ebd-44bd-96f3-886735c2ecd9</ProjectGuid>
11+
<RootNamespace>DisposableSample</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace DisposableSample
2+
{
3+
public class Program
4+
{
5+
public static void Main()
6+
{
7+
using (var resource = new SomeResource())
8+
{
9+
resource.Foo();
10+
}
11+
}
12+
}
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyConfiguration("")]
9+
[assembly: AssemblyCompany("")]
10+
[assembly: AssemblyProduct("DisposableSample")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
// The following GUID is for the ID of the typelib if this project is exposed to COM
19+
[assembly: Guid("4e3c803f-5ebd-44bd-96f3-886735c2ecd9")]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using static System.Console;
3+
4+
namespace DisposableSample
5+
{
6+
public class SomeInnerResource : IDisposable
7+
{
8+
public SomeInnerResource()
9+
{
10+
WriteLine("simulation to allocate native memory");
11+
}
12+
13+
public void Foo()
14+
{
15+
if (disposedValue) throw new ObjectDisposedException(nameof(SomeInnerResource));
16+
WriteLine($"{nameof(SomeInnerResource)}.{nameof(Foo)}");
17+
}
18+
19+
private bool disposedValue = false; // To detect redundant calls
20+
21+
protected virtual void Dispose(bool disposing)
22+
{
23+
if (!disposedValue)
24+
{
25+
if (disposing)
26+
{
27+
// TODO: dispose managed state (managed objects).
28+
}
29+
30+
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
31+
WriteLine("simulation to release native memory");
32+
// TODO: set large fields to null.
33+
34+
disposedValue = true;
35+
}
36+
}
37+
38+
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
39+
~SomeInnerResource()
40+
{
41+
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
42+
Dispose(false);
43+
}
44+
45+
// This code added to correctly implement the disposable pattern.
46+
public void Dispose()
47+
{
48+
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
49+
Dispose(true);
50+
// TODO: uncomment the following line if the finalizer is overridden above.
51+
GC.SuppressFinalize(this);
52+
}
53+
}
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using static System.Console;
3+
4+
namespace DisposableSample
5+
{
6+
public class SomeResource : IDisposable
7+
{
8+
private SomeInnerResource _innerResource;
9+
10+
public SomeResource()
11+
{
12+
_innerResource = new SomeInnerResource();
13+
}
14+
15+
public void Foo()
16+
{
17+
if (disposedValue) throw new ObjectDisposedException(nameof(SomeResource));
18+
WriteLine($"{nameof(SomeResource)}.{nameof(Foo)}");
19+
_innerResource?.Foo();
20+
}
21+
22+
#region IDisposable Support
23+
private bool disposedValue = false; // To detect redundant calls
24+
25+
protected virtual void Dispose(bool disposing)
26+
{
27+
if (!disposedValue)
28+
{
29+
if (disposing)
30+
{
31+
// dispose managed state (managed objects).
32+
_innerResource?.Dispose();
33+
}
34+
35+
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
36+
// TODO: set large fields to null.
37+
38+
disposedValue = true;
39+
}
40+
}
41+
42+
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
43+
// ~SomeResource() {
44+
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
45+
// Dispose(false);
46+
// }
47+
48+
// This code added to correctly implement the disposable pattern.
49+
public void Dispose()
50+
{
51+
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
52+
Dispose(true);
53+
// TODO: uncomment the following line if the finalizer is overridden above.
54+
// GC.SuppressFinalize(this);
55+
}
56+
#endregion
57+
}
58+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "1.0.0-*",
3+
"buildOptions": {
4+
"emitEntryPoint": true
5+
},
6+
7+
"dependencies": {
8+
"Microsoft.NETCore.App": {
9+
"type": "platform",
10+
"version": "1.0.0-rc2-3002702"
11+
}
12+
},
13+
14+
"frameworks": {
15+
"netcoreapp1.0": {
16+
"imports": "dnxcore50"
17+
}
18+
}
19+
}

Resources/ResourcesSamples/ResourcesSamples.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.23107.0
4+
VisualStudioVersion = 14.0.25123.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PointerPlayground", "PointerPlayground\PointerPlayground.xproj", "{0E49CD52-2514-4B5F-BF99-298BF8E28236}"
77
EndProject
@@ -13,6 +13,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PinvokeSample", "PinvokeSam
1313
EndProject
1414
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PInvokeSampleLib", "PInvokeSampleLib\PInvokeSampleLib.xproj", "{BA9D6B4B-23EE-4F03-942F-D66AB5796A03}"
1515
EndProject
16+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "DisposableSample", "DisposableSample\DisposableSample.xproj", "{4E3C803F-5EBD-44BD-96F3-886735C2ECD9}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{BA9D6B4B-23EE-4F03-942F-D66AB5796A03}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{BA9D6B4B-23EE-4F03-942F-D66AB5796A03}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{BA9D6B4B-23EE-4F03-942F-D66AB5796A03}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{4E3C803F-5EBD-44BD-96F3-886735C2ECD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{4E3C803F-5EBD-44BD-96F3-886735C2ECD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{4E3C803F-5EBD-44BD-96F3-886735C2ECD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{4E3C803F-5EBD-44BD-96F3-886735C2ECD9}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)