Skip to content

Commit 1752f23

Browse files
committed
Using pattern matching
1 parent 72e4953 commit 1752f23

File tree

25 files changed

+48
-86
lines changed

25 files changed

+48
-86
lines changed

src/net45/Default/WampSharp.WebSocket4Net/WAMP2/V2/Fluent/WebSocket4NetActivator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ public IControlledWampConnection<TMessage> Activate<TMessage>(IWampBinding<TMess
4242

4343
private IControlledWampConnection<TMessage> GetConnectionFactory<TMessage>(IWampBinding<TMessage> binding)
4444
{
45-
IWampTextBinding<TMessage> textBinding = binding as IWampTextBinding<TMessage>;
4645

47-
if (textBinding != null)
46+
if (binding is IWampTextBinding<TMessage> textBinding)
4847
{
4948
return CreateTextConnection(textBinding);
5049
}
5150

52-
IWampBinaryBinding<TMessage> binaryBinding = binding as IWampBinaryBinding<TMessage>;
5351

54-
if (binaryBinding != null)
52+
if (binding is IWampBinaryBinding<TMessage> binaryBinding)
5553
{
5654
return CreateBinaryConnection(binaryBinding);
5755
}

src/net45/Extensions/WampSharp.SignalR/Server/SignalRTransport.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ public void Open()
5555

5656
public IWampConnectionListener<TMessage> GetListener<TMessage>(IWampBinding<TMessage> binding)
5757
{
58-
IWampTextBinding<TMessage> textBinding = binding as IWampTextBinding<TMessage>;
5958

60-
if (textBinding == null)
59+
if (!(binding is IWampTextBinding<TMessage> textBinding))
6160
{
6261
throw new ArgumentException("This transport supports only text binding.", nameof(binding));
6362
}

src/net45/Extensions/WampSharp.WebSockets/WAMP2/V2/Fluent/WebSocketActivator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ public IControlledWampConnection<TMessage> Activate<TMessage>(IWampBinding<TMess
3333

3434
private IControlledWampConnection<TMessage> GetConnectionFactory<TMessage>(IWampBinding<TMessage> binding)
3535
{
36-
IWampTextBinding<TMessage> textBinding = binding as IWampTextBinding<TMessage>;
3736

38-
if (textBinding != null)
37+
if (binding is IWampTextBinding<TMessage> textBinding)
3938
{
4039
return CreateTextConnection(textBinding);
4140
}
4241

43-
IWampBinaryBinding<TMessage> binaryBinding = binding as IWampBinaryBinding<TMessage>;
4442

45-
if (binaryBinding != null)
43+
if (binding is IWampBinaryBinding<TMessage> binaryBinding)
4644
{
4745
return CreateBinaryConnection(binaryBinding);
4846
}

src/net45/Tests/WampSharp.Tests.TestHelpers/MockRaw.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ public class MockRaw
99
{
1010
public MockRaw(object value)
1111
{
12-
MockRaw raw = value as MockRaw;
1312
object[] rawArray = value as object[];
1413

15-
if (raw != null)
14+
if (value is MockRaw raw)
1615
{
1716
Value = Clone(raw.Value);
1817
}
@@ -66,9 +65,8 @@ private static object Clone(object value)
6665
private static bool TryClone(object value, out object result)
6766
{
6867
result = null;
69-
ICloneable cloneable = value as ICloneable;
7068

71-
if (cloneable != null)
69+
if (value is ICloneable cloneable)
7270
{
7371
result = cloneable.Clone();
7472
return true;

src/net45/Tests/WampSharp.Tests.Wampv2/Client/RawTest.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ private object SerializeArgument(object x)
4545
}
4646

4747
IWampFormatter<TMessage> formatter = mBinding.Formatter;
48-
object[] array = x as object[];
49-
50-
if (array != null)
48+
49+
if (x is object[] array)
5150
{
52-
TMessage[] arguments =
51+
TMessage[] arguments =
5352
array.Select(y => formatter.Serialize(y)).ToArray();
54-
53+
5554
return arguments;
5655
}
5756

src/net45/Tests/WampSharp.Tests.Wampv2/RecordedTests/MockBuilder/MessageMapper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ private JArray Format(WampMessage<MockRaw> message, int[] indexes)
7979

8080
private static JToken Convert(MockRaw raw)
8181
{
82-
MockRaw[] array = raw.Value as MockRaw[];
8382

84-
if (array != null)
83+
if (raw.Value is MockRaw[] array)
8584
{
8685
return new JArray(array.Select(x => Convert(x)));
8786
}

src/net45/Tests/WampSharp.Tests.Wampv2/WampMessagePrinter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ public static string ToString(WampMessage<MockRaw> message)
3232

3333
private static JToken Convert(MockRaw raw)
3434
{
35-
MockRaw[] array = raw.Value as MockRaw[];
3635

37-
if (array != null)
36+
if (raw.Value is MockRaw[] array)
3837
{
3938
return new JArray(array.Select(x => Convert(x)));
4039
}

src/net45/Tests/WampSharp.Tests/MockListener.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ public void OnNext(IWampConnection<TMessage> value)
1818
mSubject.OnNext(value);
1919

2020
// Yuck
21-
IControlledWampConnection<TMessage> casted = value as IControlledWampConnection<TMessage>;
22-
23-
if (casted != null)
21+
22+
if (value is IControlledWampConnection<TMessage> casted)
2423
{
25-
casted.Connect();
24+
casted.Connect();
2625
}
2726
}
2827

src/net45/WampSharp.WAMP1/WAMP1/V1/Cra/WampCraServer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,16 @@ public void Publish(IWampClient client, string topicUri, TMessage @event, string
146146

147147
private WampCraAuthenticator<TMessage> GetOrCreateWampAuthenticatorForClient(IWampClient client)
148148
{
149-
WampCraAuthenticator<TMessage> authenticator = client.CraAuthenticator as WampCraAuthenticator<TMessage>;
150-
151-
if (authenticator == null)
149+
150+
if (!(client.CraAuthenticator is WampCraAuthenticator<TMessage> authenticator))
152151
{
153152
if (!mAuthFactory.IsValid)
154153
{
155154
throw new InvalidOperationException("WampCraAuthenticaticatorBuilder is not valid.");
156155
}
157-
156+
158157
authenticator = mAuthFactory.BuildAuthenticator(client.SessionId);
159-
158+
160159
// Very important to give the client permissions to the auth APIs...
161160
foreach (IWampRpcMethod method in mWampCraProceduredMetadata.GetServiceMethods())
162161
{

src/net45/WampSharp.WAMP1/WAMP1/V1/PubSub/Server/WampTopic.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@ public void Unsubscribe(string sessionId)
8787

8888
public IDisposable Subscribe(IObserver<object> observer)
8989
{
90-
WampObserver casted = observer as WampObserver;
9190

9291
IDisposable result;
9392

94-
if (casted == null)
93+
if (!(observer is WampObserver casted))
9594
{
9695
IObservable<object> events = mSubject.Select(x => x.Event);
9796

98-
result =
97+
result =
9998
events.Subscribe(observer);
10099

101100
result = GetSubscriptionDisposable(result);
@@ -118,11 +117,11 @@ public IDisposable Subscribe(IObserver<object> observer)
118117
(Disposable.Create(() => OnWampObserverLeaving(sessionId)),
119118
observerDisposable,
120119
Disposable.Create(() => OnWampObserverLeft(sessionId)));
121-
120+
122121
result =
123122
GetSubscriptionDisposable(result);
124123

125-
mSessionIdToSubscription[sessionId] =
124+
mSessionIdToSubscription[sessionId] =
126125
new Subscription(this, casted, result);
127126

128127
RaiseSubscriptionAdded(casted);

0 commit comments

Comments
 (0)