diff --git a/src/Audit.DynamicProxy/AuditEventExtensions.cs b/src/Audit.DynamicProxy/AuditEventExtensions.cs index c1bcfc77..87710e52 100644 --- a/src/Audit.DynamicProxy/AuditEventExtensions.cs +++ b/src/Audit.DynamicProxy/AuditEventExtensions.cs @@ -19,9 +19,9 @@ public static InterceptEvent GetAuditInterceptEvent(this AuditScope auditScope) /// The audit event. public static InterceptEvent GetAuditInterceptEvent(this AuditEvent auditEvent) { - if (auditEvent is AuditEventIntercept) + if (auditEvent is AuditEventIntercept intercept) { - return (auditEvent as AuditEventIntercept).InterceptEvent; + return intercept.InterceptEvent; } // For backwards compatibility return auditEvent.CustomFields.ContainsKey("InterceptEvent") diff --git a/src/Audit.EntityFramework/ConfigurationApi/EntityFrameworkProviderConfigurator.cs b/src/Audit.EntityFramework/ConfigurationApi/EntityFrameworkProviderConfigurator.cs index 70fc2457..174036bf 100644 --- a/src/Audit.EntityFramework/ConfigurationApi/EntityFrameworkProviderConfigurator.cs +++ b/src/Audit.EntityFramework/ConfigurationApi/EntityFrameworkProviderConfigurator.cs @@ -121,9 +121,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction(Action { - if (obj is T) + if (obj is T t) { - action.Invoke(ev, ent, (T)obj); + action.Invoke(ev, ent, t); } return Task.FromResult(true); }; @@ -134,9 +134,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction(Func { - if (obj is T) + if (obj is T t) { - await asyncAction.Invoke(ev, ent, (T)obj); + await asyncAction.Invoke(ev, ent, t); } return true; }; @@ -147,9 +147,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction(Func { - if (obj is T) + if (obj is T t) { - return Task.FromResult(function.Invoke(ev, ent, (T)obj)); + return Task.FromResult(function.Invoke(ev, ent, t)); } return Task.FromResult(true); }; @@ -160,9 +160,9 @@ public IEntityFrameworkProviderConfiguratorExtra AuditEntityAction(Func { - if (obj is T) + if (obj is T t) { - return await asyncFunction.Invoke(ev, ent, (T)obj); + return await asyncFunction.Invoke(ev, ent, t); } return true; }; diff --git a/src/Audit.EntityFramework/Providers/EntityFrameworkDataProvider.cs b/src/Audit.EntityFramework/Providers/EntityFrameworkDataProvider.cs index 95cabf7f..40d553a2 100644 --- a/src/Audit.EntityFramework/Providers/EntityFrameworkDataProvider.cs +++ b/src/Audit.EntityFramework/Providers/EntityFrameworkDataProvider.cs @@ -164,9 +164,9 @@ public override object InsertEvent(AuditEvent auditEvent) } if (save) { - if (auditDbContext is IAuditBypass) + if (auditDbContext is IAuditBypass bypass) { - (auditDbContext as IAuditBypass).SaveChangesBypassAudit(); + bypass.SaveChangesBypassAudit(); } else { diff --git a/src/Audit.FileSystem/FileSystemMonitor.cs b/src/Audit.FileSystem/FileSystemMonitor.cs index 9a655f6b..2c6602b4 100644 --- a/src/Audit.FileSystem/FileSystemMonitor.cs +++ b/src/Audit.FileSystem/FileSystemMonitor.cs @@ -132,7 +132,7 @@ private void ProcessEvent(FileSystemEventArgs e, FileSystemEventType type) Extension = System.IO.Path.GetExtension(e.FullPath), FullPath = e.FullPath, Event = type, - OldName = (e is RenamedEventArgs) ? System.IO.Path.GetFileName((e as RenamedEventArgs).OldFullPath) : null + OldName = (e is RenamedEventArgs args) ? System.IO.Path.GetFileName(args.OldFullPath) : null }; var fsAuditEvent = new AuditEventFileSystem() { diff --git a/src/Audit.HttpClient/AuditEventExtensions.cs b/src/Audit.HttpClient/AuditEventExtensions.cs index 4216b93e..a6432339 100644 --- a/src/Audit.HttpClient/AuditEventExtensions.cs +++ b/src/Audit.HttpClient/AuditEventExtensions.cs @@ -20,9 +20,9 @@ public static HttpAction GetHttpAction(this AuditScope auditScope) /// The audit event. public static HttpAction GetHttpAction(this AuditEvent auditEvent) { - if (auditEvent is AuditEventHttpClient) + if (auditEvent is AuditEventHttpClient client) { - return (auditEvent as AuditEventHttpClient).Action; + return client.Action; } // For backwards compatibility return auditEvent.CustomFields.ContainsKey("Action") diff --git a/src/Audit.Mvc/AuditEventExtensions.cs b/src/Audit.Mvc/AuditEventExtensions.cs index 89833cae..e240806e 100644 --- a/src/Audit.Mvc/AuditEventExtensions.cs +++ b/src/Audit.Mvc/AuditEventExtensions.cs @@ -19,9 +19,9 @@ public static AuditAction GetMvcAuditAction(this AuditScope auditScope) /// The audit event. public static AuditAction GetMvcAuditAction(this AuditEvent auditEvent) { - if (auditEvent is AuditEventMvcAction) + if (auditEvent is AuditEventMvcAction action) { - return (auditEvent as AuditEventMvcAction).Action; + return action.Action; } // For backwards compatibility return auditEvent.CustomFields.ContainsKey("Action") diff --git a/src/Audit.Mvc/AuditPageFilter.cs b/src/Audit.Mvc/AuditPageFilter.cs index dc174178..a5d099a6 100644 --- a/src/Audit.Mvc/AuditPageFilter.cs +++ b/src/Audit.Mvc/AuditPageFilter.cs @@ -149,8 +149,8 @@ public virtual async Task AfterExecutedAsync(PageHandlerExecutedContext context) auditAction.RedirectLocation = httpContext.Response.Headers?["Location"]; auditAction.ResponseStatusCode = context.Result == null && context.Exception != null && !context.ExceptionHandled ? 500 - : context.Result is StatusCodeResult - ? (context.Result as StatusCodeResult).StatusCode + : context.Result is StatusCodeResult result + ? result.StatusCode : httpContext.Response.StatusCode; var bodyType = context.Result?.GetType().GetFullTypeName(); diff --git a/src/Audit.NET.JsonNewtonsoftAdapter/JsonNewtonsoftAdapter.cs b/src/Audit.NET.JsonNewtonsoftAdapter/JsonNewtonsoftAdapter.cs index d1db5771..ae0d1714 100644 --- a/src/Audit.NET.JsonNewtonsoftAdapter/JsonNewtonsoftAdapter.cs +++ b/src/Audit.NET.JsonNewtonsoftAdapter/JsonNewtonsoftAdapter.cs @@ -82,9 +82,9 @@ public T ToObject(object value) { return (T)value; } - if (value is JContainer) + if (value is JContainer container) { - return (value as JContainer).ToObject(); + return container.ToObject(); } return default(T); } diff --git a/src/Audit.NET.JsonSystemAdapter/JsonSystemAdapter.cs b/src/Audit.NET.JsonSystemAdapter/JsonSystemAdapter.cs index 7f03af9b..f5b591df 100644 --- a/src/Audit.NET.JsonSystemAdapter/JsonSystemAdapter.cs +++ b/src/Audit.NET.JsonSystemAdapter/JsonSystemAdapter.cs @@ -61,9 +61,8 @@ public T ToObject(object value) return (T)value; } // TODO: Workaround to convert from JsonElement to Object, until https://github.com/dotnet/runtime/issues/31274 fixed - if (value is JsonElement) + if (value is JsonElement element) { - var element = (JsonElement)value; using (var bufferWriter = new MemoryStream()) { using (var writer = new Utf8JsonWriter(bufferWriter)) diff --git a/src/Audit.NET/JsonAdapter.cs b/src/Audit.NET/JsonAdapter.cs index 9abaffa9..01e3404b 100644 --- a/src/Audit.NET/JsonAdapter.cs +++ b/src/Audit.NET/JsonAdapter.cs @@ -86,15 +86,14 @@ public T ToObject(object value) return (T)value; } #if IS_NK_JSON - if (value is JContainer) + if (value is JContainer container) { - return (value as JContainer).ToObject(JsonSerializer.Create(Configuration.JsonSettings)); + return container.ToObject(JsonSerializer.Create(Configuration.JsonSettings)); } #else // TODO: Workaround to convert from JsonElement to Object, until https://github.com/dotnet/runtime/issues/31274 fixed - if (value is JsonElement) + if (value is JsonElement element) { - var element = (JsonElement)value; var bufferWriter = new ArrayBufferWriter(); using (var writer = new Utf8JsonWriter(bufferWriter)) { diff --git a/src/Audit.WCF.Client/AuditEventExtensions.cs b/src/Audit.WCF.Client/AuditEventExtensions.cs index 256451ec..a2e722eb 100644 --- a/src/Audit.WCF.Client/AuditEventExtensions.cs +++ b/src/Audit.WCF.Client/AuditEventExtensions.cs @@ -19,9 +19,9 @@ public static WcfClientAction GetWcfClientAction(this AuditScope auditScope) /// The audit event. public static WcfClientAction GetWcfClientAction(this AuditEvent auditEvent) { - if (auditEvent is AuditEventWcfClient) + if (auditEvent is AuditEventWcfClient client) { - return (auditEvent as AuditEventWcfClient).WcfClientEvent; + return client.WcfClientEvent; } return auditEvent.CustomFields.ContainsKey("WcfClientEvent") ? Configuration.JsonAdapter.ToObject(auditEvent.CustomFields["WcfClientEvent"]) diff --git a/src/Audit.WCF/AuditEventExtensions.cs b/src/Audit.WCF/AuditEventExtensions.cs index 09401225..83bae021 100644 --- a/src/Audit.WCF/AuditEventExtensions.cs +++ b/src/Audit.WCF/AuditEventExtensions.cs @@ -19,9 +19,9 @@ public static WcfEvent GetWcfAuditAction(this AuditScope auditScope) /// The audit event. public static WcfEvent GetWcfAuditAction(this AuditEvent auditEvent) { - if (auditEvent is AuditEventWcfAction) + if (auditEvent is AuditEventWcfAction action) { - return (auditEvent as AuditEventWcfAction).WcfEvent; + return action.WcfEvent; } // For backwards compatibility return auditEvent.CustomFields.ContainsKey("WcfEvent") diff --git a/src/Audit.WCF/AuditOperationInvoker.cs b/src/Audit.WCF/AuditOperationInvoker.cs index b8caac29..a3f123bc 100644 --- a/src/Audit.WCF/AuditOperationInvoker.cs +++ b/src/Audit.WCF/AuditOperationInvoker.cs @@ -215,10 +215,9 @@ private AuditWcfEventFault GetWcfFaultData(Exception ex) { var result = new AuditWcfEventFault(); result.Exception = ex.GetExceptionInfo(); - if (ex is FaultException) + if (ex is FaultException fault) { result.FaultType = "Fault"; - var fault = ex as FaultException; if (fault.GetType().GetProperty("Detail") != null) { var detail = fault.GetType().GetProperty("Detail").GetGetMethod().Invoke(fault, null); diff --git a/src/Audit.WebApi/AuditApiAttribute.Core.cs b/src/Audit.WebApi/AuditApiAttribute.Core.cs index b289ebb8..d51f8622 100644 --- a/src/Audit.WebApi/AuditApiAttribute.Core.cs +++ b/src/Audit.WebApi/AuditApiAttribute.Core.cs @@ -116,10 +116,11 @@ internal bool ShouldIncludeResponseBody(HttpStatusCode statusCode) private HttpStatusCode GetStatusCode(ActionExecutedContext context) { - var statusCode = context.Result is ObjectResult && (context.Result as ObjectResult).StatusCode.HasValue - ? (context.Result as ObjectResult).StatusCode.Value - : context.Result is StatusCodeResult - ? (context.Result as StatusCodeResult).StatusCode + var result = context.Result; + var statusCode = result is ObjectResult objectResult && objectResult.StatusCode.HasValue + ? objectResult.StatusCode.Value + : result is StatusCodeResult statusCodeResult + ? statusCodeResult.StatusCode : context.HttpContext.Response.StatusCode; return (HttpStatusCode)statusCode; } diff --git a/src/Audit.WebApi/AuditEventExtensions.cs b/src/Audit.WebApi/AuditEventExtensions.cs index 1335e7e7..3cdff131 100644 --- a/src/Audit.WebApi/AuditEventExtensions.cs +++ b/src/Audit.WebApi/AuditEventExtensions.cs @@ -19,9 +19,9 @@ public static AuditApiAction GetWebApiAuditAction(this AuditScope auditScope) /// The audit event. public static AuditApiAction GetWebApiAuditAction(this AuditEvent auditEvent) { - if (auditEvent is AuditEventWebApi) + if (auditEvent is AuditEventWebApi api) { - return (auditEvent as AuditEventWebApi).Action; + return api.Action; } // For backwards compatibility return auditEvent.CustomFields.ContainsKey("Action") diff --git a/test/Audit.EntityFramework.UnitTest/InheritanceTests.cs b/test/Audit.EntityFramework.UnitTest/InheritanceTests.cs index 5640429f..10b93204 100644 --- a/test/Audit.EntityFramework.UnitTest/InheritanceTests.cs +++ b/test/Audit.EntityFramework.UnitTest/InheritanceTests.cs @@ -70,11 +70,9 @@ public void Test_Ef_Inheritance() { DBEntityBase bse = context.Entities.First(x => x.ID == id); - if (!(bse is DBEntity)) + if (!(bse is DBEntity ent)) throw new Exception("1"); - DBEntity ent = (DBEntity) bse; - ent.Name = "Base 2"; ent.Name2 = "Inherited 2"; ent.Ticks = 456;