forked from DotNetAnalyzers/IDisposableAnalyzers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Misc.cs
129 lines (110 loc) · 3.75 KB
/
Misc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// ReSharper disable All
namespace ValidCode
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reactive.Disposables;
public sealed class Misc : IDisposable
{
#pragma warning disable CS0169
private static readonly PropertyChangedEventArgs IsDirtyPropertyChangedEventArgs = new PropertyChangedEventArgs(nameof(IsDirty));
private readonly SingleAssignmentDisposable subscription = new SingleAssignmentDisposable();
private readonly CompositeDisposable compositeDisposable = new CompositeDisposable();
private readonly Lazy<IDisposable> lazyDisposable;
private readonly IDisposable? meh1;
private readonly IDisposable? meh2;
private readonly IDisposable disposable;
private bool isDirty;
public Misc(IDisposable disposable)
{
this.subscription.Disposable = File.OpenRead(string.Empty);
this.disposable = Bar(disposable);
using (var temp = this.CreateDisposableProperty)
{
}
using (var temp = this.CreateDisposable())
{
}
this.lazyDisposable = new Lazy<IDisposable>(() =>
{
var temp = new Disposable();
return temp;
});
}
public event PropertyChangedEventHandler? PropertyChanged
{
add { this.PropertyChangedCore += value; }
remove { this.PropertyChangedCore -= value; }
}
private event PropertyChangedEventHandler? PropertyChangedCore;
public IDisposable? Disposable => this.subscription.Disposable;
#pragma warning disable IDISP012 // Property should not return created disposable
public IDisposable CreateDisposableProperty => new Disposable();
#pragma warning restore IDISP012 // Property should not return created disposable
public bool IsDirty
{
get
{
return this.isDirty;
}
private set
{
if (value == this.isDirty)
{
return;
}
this.isDirty = value;
this.PropertyChangedCore?.Invoke(this, IsDirtyPropertyChangedEventArgs);
}
}
public static IDisposable? AssignLocalInSwitch(int i)
{
IDisposable? result;
if (i == 0)
{
result = null;
}
else
{
switch (i)
{
case 1:
result = File.OpenRead(string.Empty);
break;
case 2:
result = File.OpenRead(string.Empty);
break;
default:
result = null;
break;
}
}
return result;
}
public void Dispose()
{
this.subscription.Dispose();
this.compositeDisposable.Dispose();
if (this.lazyDisposable.IsValueCreated)
{
this.lazyDisposable.Value.Dispose();
}
}
public IDisposable CreateDisposable() => new Disposable();
public static void Touch(string fileName)
{
File.Create(fileName).Dispose();
File.Create(fileName)?.Dispose();
}
private static IDisposable Bar(IDisposable disposable, IEnumerable<IDisposable>? disposables = null)
{
if (disposables == null)
{
return Bar(disposable, new[] { disposable });
}
return disposable;
}
}
}