From 2a474aac0a41c8e920a363ced6b21fc40b263d77 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 22 May 2023 03:25:56 +0200 Subject: [PATCH] Enabled nullable checks and fix nullability warnings. --- VAR.WebFormsCore.TestWebApp/FrmDefault.cs | 9 ++-- VAR.WebFormsCore.TestWebApp/Program.cs | 3 -- VAR.WebFormsCore.TestWebApp/Startup.cs | 3 -- VAR.WebFormsCore.sln.DotSettings | 2 + VAR.WebFormsCore/Code/Bundler.cs | 14 +++--- VAR.WebFormsCore/Code/ExtensionMethods.cs | 2 +- VAR.WebFormsCore/Code/GlobalConfig.cs | 43 ++++++++++++++++--- VAR.WebFormsCore/Code/GlobalErrorHandler.cs | 2 +- .../Code/GlobalRouterMiddleware.cs | 10 ++--- VAR.WebFormsCore/Code/MultiLang.cs | 10 ++--- VAR.WebFormsCore/Code/ServerHelpers.cs | 2 +- VAR.WebFormsCore/Code/StaticFileHelper.cs | 2 +- VAR.WebFormsCore/Controls/Button.cs | 4 +- VAR.WebFormsCore/Controls/CTextBox.cs | 38 +++++++++------- VAR.WebFormsCore/Controls/Control.cs | 43 +++++++++++-------- VAR.WebFormsCore/Controls/HiddenField.cs | 4 +- VAR.WebFormsCore/Controls/HtmlForm.cs | 15 ++++--- VAR.WebFormsCore/Controls/HtmlHead.cs | 2 +- VAR.WebFormsCore/Controls/HtmlMeta.cs | 6 +-- VAR.WebFormsCore/Controls/HyperLink.cs | 4 +- VAR.WebFormsCore/Controls/LiteralControl.cs | 2 +- VAR.WebFormsCore/Controls/TextBox.cs | 4 +- VAR.WebFormsCore/Pages/FormUtils.cs | 2 +- VAR.WebFormsCore/Pages/FrmError.cs | 4 +- VAR.WebFormsCore/Pages/Page.cs | 4 +- VAR.WebFormsCore/Pages/PageCommon.cs | 42 ++++++++++-------- VAR.WebFormsCore/VAR.WebFormsCore.csproj | 1 + 27 files changed, 160 insertions(+), 117 deletions(-) create mode 100644 VAR.WebFormsCore.sln.DotSettings diff --git a/VAR.WebFormsCore.TestWebApp/FrmDefault.cs b/VAR.WebFormsCore.TestWebApp/FrmDefault.cs index dbf385a..ea99d82 100644 --- a/VAR.WebFormsCore.TestWebApp/FrmDefault.cs +++ b/VAR.WebFormsCore.TestWebApp/FrmDefault.cs @@ -1,4 +1,3 @@ -using System; using VAR.WebFormsCore.Code; using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Pages; @@ -7,9 +6,9 @@ namespace VAR.WebFormsCore.TestWebApp; public class FrmDefault: PageCommon { - private CTextBox _txtText = new CTextBox { ID = "txtText", CssClassExtra = "width150px", AllowEmpty = false }; - private Button _btnTest = new Button { ID = "btnTest", }; - private Label _lblMessage = new Label { ID = "lblMessage", }; + private readonly CTextBox _txtText = new() { ID = "txtText", CssClassExtra = "width150px", AllowEmpty = false }; + private readonly Button _btnTest = new() { ID = "btnTest", }; + private readonly Label _lblMessage = new() { ID = "lblMessage", }; public FrmDefault() { @@ -17,7 +16,7 @@ public class FrmDefault: PageCommon Init += FrmLogin_Init; } - private void FrmLogin_Init(object sender, EventArgs e) + private void FrmLogin_Init(object? sender, EventArgs e) { InitializeControls(); } diff --git a/VAR.WebFormsCore.TestWebApp/Program.cs b/VAR.WebFormsCore.TestWebApp/Program.cs index 0b4c917..0a416e1 100644 --- a/VAR.WebFormsCore.TestWebApp/Program.cs +++ b/VAR.WebFormsCore.TestWebApp/Program.cs @@ -1,6 +1,3 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; - namespace VAR.WebFormsCore.TestWebApp { public class Program diff --git a/VAR.WebFormsCore.TestWebApp/Startup.cs b/VAR.WebFormsCore.TestWebApp/Startup.cs index 24ff415..71fc70d 100644 --- a/VAR.WebFormsCore.TestWebApp/Startup.cs +++ b/VAR.WebFormsCore.TestWebApp/Startup.cs @@ -1,7 +1,4 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Core; -using Microsoft.Extensions.DependencyInjection; using VAR.WebFormsCore.Code; namespace VAR.WebFormsCore.TestWebApp diff --git a/VAR.WebFormsCore.sln.DotSettings b/VAR.WebFormsCore.sln.DotSettings new file mode 100644 index 0000000..d18f71a --- /dev/null +++ b/VAR.WebFormsCore.sln.DotSettings @@ -0,0 +1,2 @@ + + ID \ No newline at end of file diff --git a/VAR.WebFormsCore/Code/Bundler.cs b/VAR.WebFormsCore/Code/Bundler.cs index 9cec531..e8f8a4c 100644 --- a/VAR.WebFormsCore/Code/Bundler.cs +++ b/VAR.WebFormsCore/Code/Bundler.cs @@ -11,11 +11,11 @@ namespace VAR.WebFormsCore.Code { #region Declarations - private readonly Assembly _assembly; - private readonly string _assemblyNamespace; - private List _assemblyFiles; - private readonly string _absolutePath; - private List _absoluteFiles; + private readonly Assembly? _assembly; + private readonly string? _assemblyNamespace; + private List? _assemblyFiles; + private readonly string? _absolutePath; + private List? _absoluteFiles; #endregion Declarations @@ -62,7 +62,7 @@ namespace VAR.WebFormsCore.Code #region Creator - public Bundler(Assembly assembly = null, string assemblyNamespace = null, string absolutePath = null) + public Bundler(Assembly? assembly = null, string? assemblyNamespace = null, string? absolutePath = null) { _assembly = assembly; _assemblyNamespace = assemblyNamespace; @@ -81,7 +81,7 @@ namespace VAR.WebFormsCore.Code response.ContentType = contentType; foreach (string fileName in AssemblyFiles) { - Stream resourceStream = _assembly.GetManifestResourceStream(fileName); + Stream? resourceStream = _assembly?.GetManifestResourceStream(fileName); if (resourceStream != null) { string fileContent = new StreamReader(resourceStream).ReadToEnd(); diff --git a/VAR.WebFormsCore/Code/ExtensionMethods.cs b/VAR.WebFormsCore/Code/ExtensionMethods.cs index 4ce78eb..7e1e919 100644 --- a/VAR.WebFormsCore/Code/ExtensionMethods.cs +++ b/VAR.WebFormsCore/Code/ExtensionMethods.cs @@ -9,7 +9,7 @@ namespace VAR.WebFormsCore.Code { #region HttpContext - public static string GetRequestParm(this HttpContext context, string parm) + public static string? GetRequestParm(this HttpContext context, string parm) { if (context.Request.Method == "POST") { diff --git a/VAR.WebFormsCore/Code/GlobalConfig.cs b/VAR.WebFormsCore/Code/GlobalConfig.cs index c6ff1fe..b89e953 100644 --- a/VAR.WebFormsCore/Code/GlobalConfig.cs +++ b/VAR.WebFormsCore/Code/GlobalConfig.cs @@ -1,32 +1,61 @@ using System; +using System.Collections.Generic; using System.Linq; +using Microsoft.AspNetCore.Http; namespace VAR.WebFormsCore.Code { public static class GlobalConfig { - private static IGlobalConfig _globalConfig; + private static IGlobalConfig? _globalConfig; public static IGlobalConfig Get() { if (_globalConfig != null) { return _globalConfig; } Type iGlobalConfig = typeof(IGlobalConfig); - Type foundGlobalConfig = AppDomain.CurrentDomain + Type? foundGlobalConfig = AppDomain.CurrentDomain .GetAssemblies() - .SelectMany( - x => - x.GetTypes() - ) + .SelectMany(x => x.GetTypes()) .FirstOrDefault( x => x.IsAbstract == false && x.IsInterface == false && + x.IsPublic && iGlobalConfig.IsAssignableFrom(x) ); - _globalConfig = ObjectActivator.CreateInstance(foundGlobalConfig) as IGlobalConfig; + if(foundGlobalConfig != null) + { + _globalConfig = ObjectActivator.CreateInstance(foundGlobalConfig) as IGlobalConfig; + } + + if(_globalConfig == null) + { + _globalConfig = new DefaultGlobalConfig(); + } return _globalConfig; } + + // TODO: Better default global config + private class DefaultGlobalConfig : IGlobalConfig + { + public string Title { get; } = string.Empty; + public string TitleSeparator { get; } = string.Empty; + public string Author { get; } = string.Empty; + public string Copyright { get; } = string.Empty; + public string DefaultHandler { get; } = string.Empty; + public string LoginHandler { get; } = string.Empty; + public List AllowedExtensions { get; } = new List(); + + public bool IsUserAuthenticated(HttpContext context) + { + return false; + } + + public void UserUnauthenticate(HttpContext context) + { + } + } } } \ No newline at end of file diff --git a/VAR.WebFormsCore/Code/GlobalErrorHandler.cs b/VAR.WebFormsCore/Code/GlobalErrorHandler.cs index ca1a76e..8b74a26 100644 --- a/VAR.WebFormsCore/Code/GlobalErrorHandler.cs +++ b/VAR.WebFormsCore/Code/GlobalErrorHandler.cs @@ -17,7 +17,7 @@ namespace VAR.WebFormsCore.Code StringBuilder sbOutput = new StringBuilder(); sbOutput.Append("

Internal error

"); - Exception exAux = ex; + Exception? exAux = ex; while (exAux != null) { sbOutput.AppendFormat("

Message: {0}

", exAux.Message); diff --git a/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs b/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs index e11203e..a3b9d0c 100644 --- a/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs +++ b/VAR.WebFormsCore/Code/GlobalRouterMiddleware.cs @@ -68,7 +68,7 @@ namespace VAR.WebFormsCore.Code } } - IHttpHandler handler = GetHandler(file); + IHttpHandler? handler = GetHandler(file); if (handler == null) { // TODO: FrmNotFound @@ -81,16 +81,16 @@ namespace VAR.WebFormsCore.Code private static readonly Dictionary Handlers = new Dictionary(); - private static IHttpHandler GetHandler(string typeName) + private static IHttpHandler? GetHandler(string typeName) { if (string.IsNullOrEmpty(typeName)) { return null; } - Type type; + Type? type; lock (Handlers) { if (Handlers.TryGetValue(typeName, out type)) { - IHttpHandler handler = ObjectActivator.CreateInstance(type) as IHttpHandler; + IHttpHandler? handler = ObjectActivator.CreateInstance(type) as IHttpHandler; return handler; } } @@ -129,7 +129,7 @@ namespace VAR.WebFormsCore.Code // Use found type if (type != null) { - IHttpHandler handler = ObjectActivator.CreateInstance(type) as IHttpHandler; + IHttpHandler? handler = ObjectActivator.CreateInstance(type) as IHttpHandler; if (handler != null) { lock (Handlers) diff --git a/VAR.WebFormsCore/Code/MultiLang.cs b/VAR.WebFormsCore/Code/MultiLang.cs index 4e4de1d..56b0ab5 100644 --- a/VAR.WebFormsCore/Code/MultiLang.cs +++ b/VAR.WebFormsCore/Code/MultiLang.cs @@ -12,7 +12,7 @@ namespace VAR.WebFormsCore.Code string privatePath = Path.Combine(currentDir, baseDir); while (Directory.Exists(privatePath) == false) { - DirectoryInfo dirInfo = Directory.GetParent(currentDir); + DirectoryInfo? dirInfo = Directory.GetParent(currentDir); if (dirInfo == null) { break; } currentDir = dirInfo.FullName; @@ -22,11 +22,11 @@ namespace VAR.WebFormsCore.Code return Path.Combine(privatePath, fileName); } - private static Dictionary> _literals; + private static Dictionary?>? _literals; private static void InitializeLiterals() { - _literals = new Dictionary>(); + _literals = new Dictionary?>(); JsonParser jsonParser = new JsonParser(); foreach (string lang in new[] {"en", "es"}) @@ -75,7 +75,7 @@ namespace VAR.WebFormsCore.Code return DefaultLanguage; } - public static string GetLiteral(string resource, string culture = null) + public static string GetLiteral(string resource, string? culture = null) { if (_literals == null) { InitializeLiterals(); } @@ -83,7 +83,7 @@ namespace VAR.WebFormsCore.Code if (_literals == null || _literals.ContainsKey(culture) == false) { return resource; } - Dictionary literalCurrentCulture = _literals[culture]; + Dictionary? literalCurrentCulture = _literals[culture]; if (literalCurrentCulture == null || literalCurrentCulture.ContainsKey(resource) == false) { diff --git a/VAR.WebFormsCore/Code/ServerHelpers.cs b/VAR.WebFormsCore/Code/ServerHelpers.cs index dcc0fd6..b576473 100644 --- a/VAR.WebFormsCore/Code/ServerHelpers.cs +++ b/VAR.WebFormsCore/Code/ServerHelpers.cs @@ -5,7 +5,7 @@ namespace VAR.WebFormsCore.Code { public static class ServerHelpers { - private static string _contentRoot; + private static string? _contentRoot; public static void SetContentRoot(string contentRoot) { _contentRoot = contentRoot; } public static string MapContentPath(string path) diff --git a/VAR.WebFormsCore/Code/StaticFileHelper.cs b/VAR.WebFormsCore/Code/StaticFileHelper.cs index 42c84a0..7642e9c 100644 --- a/VAR.WebFormsCore/Code/StaticFileHelper.cs +++ b/VAR.WebFormsCore/Code/StaticFileHelper.cs @@ -71,7 +71,7 @@ namespace VAR.WebFormsCore.Code public static async void ResponseStaticFile(HttpContext context, string filePath) { string extension = Path.GetExtension(filePath).ToLower(); - MimeTypeByExtension.TryGetValue(extension, out string contentType); + MimeTypeByExtension.TryGetValue(extension, out string? contentType); if (string.IsNullOrEmpty(contentType) == false) { context.Response.ContentType = contentType; } diff --git a/VAR.WebFormsCore/Controls/Button.cs b/VAR.WebFormsCore/Controls/Button.cs index 3e566d0..ca42558 100644 --- a/VAR.WebFormsCore/Controls/Button.cs +++ b/VAR.WebFormsCore/Controls/Button.cs @@ -15,11 +15,11 @@ namespace VAR.WebFormsCore.Controls set => _text = value; } - public string OnClientClick { get; set; } + public string OnClientClick { get; set; } = string.Empty; public string CommandArgument { get; set; } = string.Empty; - public event EventHandler Click; + public event EventHandler? Click; protected override void Render(TextWriter textWriter) { diff --git a/VAR.WebFormsCore/Controls/CTextBox.cs b/VAR.WebFormsCore/Controls/CTextBox.cs index caefacb..28c9fc2 100644 --- a/VAR.WebFormsCore/Controls/CTextBox.cs +++ b/VAR.WebFormsCore/Controls/CTextBox.cs @@ -11,7 +11,7 @@ namespace VAR.WebFormsCore.Controls private readonly TextBox _txtContent = new TextBox(); - private HiddenField _hidSize; + private HiddenField? _hidSize; private const string CssClassBase = "textbox"; private string _cssClassExtra = ""; @@ -22,7 +22,7 @@ namespace VAR.WebFormsCore.Controls private bool _markedInvalid; - private Control _nextFocusOnEnter; + private Control? _nextFocusOnEnter; private bool _keepSize; @@ -54,7 +54,7 @@ namespace VAR.WebFormsCore.Controls set => _markedInvalid = value; } - public Control NextFocusOnEnter + public Control? NextFocusOnEnter { get => _nextFocusOnEnter; set => _nextFocusOnEnter = value; @@ -88,7 +88,7 @@ namespace VAR.WebFormsCore.Controls PreRender += CTextbox_PreRender; } - private void CTextBox_Init(object sender, EventArgs e) + private void CTextBox_Init(object? sender, EventArgs e) { Controls.Add(_txtContent); @@ -103,7 +103,7 @@ namespace VAR.WebFormsCore.Controls string strCfgName = $"{this.ClientID}_cfg"; Dictionary cfg = new Dictionary { - {"txtContent", _txtContent.ClientID}, {"hidSize", _hidSize.ClientID}, {"keepSize", _keepSize}, + {"txtContent", _txtContent.ClientID}, {"hidSize", _hidSize?.ClientID ?? string.Empty}, {"keepSize", _keepSize}, }; StringBuilder sbCfg = new StringBuilder(); sbCfg.AppendFormat("\n") ); @@ -105,9 +111,7 @@ namespace VAR.WebFormsCore.Pages new LiteralControl($"\n") ); - _body = new HtmlBody(); html.Controls.Add(_body); - _form = new HtmlForm {ID = "formMain"}; _body.Controls.Add(_form); var pnlHeader = new Panel {CssClass = "divHeader"}; diff --git a/VAR.WebFormsCore/VAR.WebFormsCore.csproj b/VAR.WebFormsCore/VAR.WebFormsCore.csproj index ee7f2fd..541ba73 100644 --- a/VAR.WebFormsCore/VAR.WebFormsCore.csproj +++ b/VAR.WebFormsCore/VAR.WebFormsCore.csproj @@ -3,6 +3,7 @@ Library net7.0 + enable