diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs index fb93cc5ec..25b6e034b 100644 --- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs @@ -81,23 +81,28 @@ namespace Discord.Commands foreach (var attribute in attributes) { - // TODO: C#7 type switch - if (attribute is NameAttribute) - builder.Name = (attribute as NameAttribute).Text; - else if (attribute is SummaryAttribute) - builder.Summary = (attribute as SummaryAttribute).Text; - else if (attribute is RemarksAttribute) - builder.Remarks = (attribute as RemarksAttribute).Text; - else if (attribute is AliasAttribute) - builder.AddAliases((attribute as AliasAttribute).Aliases); - else if (attribute is GroupAttribute) + switch (attribute) { - var groupAttr = attribute as GroupAttribute; - builder.Name = builder.Name ?? groupAttr.Prefix; - builder.AddAliases(groupAttr.Prefix); + case NameAttribute name: + builder.Name = name.Text; + break; + case SummaryAttribute summary: + builder.Summary = summary.Text; + break; + case RemarksAttribute remarks: + builder.Remarks = remarks.Text; + break; + case AliasAttribute alias: + builder.AddAliases(alias.Aliases); + break; + case GroupAttribute group: + builder.Name = builder.Name ?? group.Prefix; + builder.AddAliases(group.Prefix); + break; + case PreconditionAttribute precondition: + builder.AddPrecondition(precondition); + break; } - else if (attribute is PreconditionAttribute) - builder.AddPrecondition(attribute as PreconditionAttribute); } //Check for unspecified info diff --git a/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs b/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs index 054b80119..94596e0e6 100644 --- a/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs +++ b/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs @@ -20,13 +20,14 @@ namespace Discord /// Gets a ChannelPermissions that grants all permissions for a given channelType. public static ChannelPermissions All(IChannel channel) { - //TODO: C#7 Candidate for typeswitch - if (channel is ITextChannel) return Text; - if (channel is IVoiceChannel) return Voice; - if (channel is IDMChannel) return DM; - if (channel is IGroupChannel) return Group; - - throw new ArgumentException("Unknown channel type", nameof(channel)); + switch (channel) + { + case ITextChannel _: return Text; + case IVoiceChannel _: return Voice; + case IDMChannel _: return DM; + case IGroupChannel _: return Group; + default: throw new ArgumentException("Unknown channel type", nameof(channel)); + } } /// Gets a packed value representing all the permissions in this ChannelPermissions. diff --git a/src/Discord.Net.Core/Utils/Permissions.cs b/src/Discord.Net.Core/Utils/Permissions.cs index b69b103e1..c2b7e83ea 100644 --- a/src/Discord.Net.Core/Utils/Permissions.cs +++ b/src/Discord.Net.Core/Utils/Permissions.cs @@ -150,9 +150,7 @@ namespace Discord if (perms != null) resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; - //TODO: C#7 Typeswitch candidate - var textChannel = channel as ITextChannel; - if (textChannel != null) + if (channel is ITextChannel textChannel) { if (!GetValue(resolvedPermissions, ChannelPermission.ReadMessages)) { diff --git a/src/Discord.Net.Rest/Net/DefaultRestClient.cs b/src/Discord.Net.Rest/Net/DefaultRestClient.cs index ceda90035..493fc3aff 100644 --- a/src/Discord.Net.Rest/Net/DefaultRestClient.cs +++ b/src/Discord.Net.Rest/Net/DefaultRestClient.cs @@ -87,29 +87,26 @@ namespace Discord.Net.Rest { foreach (var p in multipartParams) { - //TODO: C#7 Typeswitch candidate - var stringValue = p.Value as string; - if (stringValue != null) { content.Add(new StringContent(stringValue), p.Key); continue; } - var byteArrayValue = p.Value as byte[]; - if (byteArrayValue != null) { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; } - var streamValue = p.Value as Stream; - if (streamValue != null) { content.Add(new StreamContent(streamValue), p.Key); continue; } - if (p.Value is MultipartFile) + switch (p.Value) { - var fileValue = (MultipartFile)p.Value; - var stream = fileValue.Stream; - if (!stream.CanSeek) + case string stringValue: { content.Add(new StringContent(stringValue), p.Key); continue; } + case byte[] byteArrayValue: { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; } + case Stream streamValue: { content.Add(new StreamContent(streamValue), p.Key); continue; } + case MultipartFile fileValue: { - var memoryStream = new MemoryStream(); - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; - stream = memoryStream; + var stream = fileValue.Stream; + if (!stream.CanSeek) + { + var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream).ConfigureAwait(false); + memoryStream.Position = 0; + stream = memoryStream; + } + content.Add(new StreamContent(stream), p.Key, fileValue.Filename); + continue; } - content.Add(new StreamContent(stream), p.Key, fileValue.Filename); - continue; + default: throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\""); } - - throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\""); } } restRequest.Content = content; diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs index 1bc0fc9b5..ca53315aa 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs @@ -61,28 +61,24 @@ namespace Discord.WebSocket public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord, SocketMessage msg) { - //TODO: C#7 Candidate for pattern matching - if (channel is SocketDMChannel) - (channel as SocketDMChannel).AddMessage(msg); - else if (channel is SocketGroupChannel) - (channel as SocketGroupChannel).AddMessage(msg); - else if (channel is SocketTextChannel) - (channel as SocketTextChannel).AddMessage(msg); - else - throw new NotSupportedException("Unexpected ISocketMessageChannel type"); + switch (channel) + { + case SocketDMChannel dmChannel: dmChannel.AddMessage(msg); break; + case SocketGroupChannel groupChannel: groupChannel.AddMessage(msg); break; + case SocketTextChannel textChannel: textChannel.AddMessage(msg); break; + default: throw new NotSupportedException("Unexpected ISocketMessageChannel type"); + } } public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, ulong id) { - //TODO: C#7 Candidate for pattern matching - if (channel is SocketDMChannel) - return (channel as SocketDMChannel).RemoveMessage(id); - else if (channel is SocketGroupChannel) - return (channel as SocketGroupChannel).RemoveMessage(id); - else if (channel is SocketTextChannel) - return (channel as SocketTextChannel).RemoveMessage(id); - else - throw new NotSupportedException("Unexpected ISocketMessageChannel type"); + switch (channel) + { + case SocketDMChannel dmChannel: return dmChannel.RemoveMessage(id); + case SocketGroupChannel groupChannel: return groupChannel.RemoveMessage(id); + case SocketTextChannel textChannel: return textChannel.RemoveMessage(id); + default: throw new NotSupportedException("Unexpected ISocketMessageChannel type"); + } } } }