From baed3c9e083195c6f3410867ee383e3738062f64 Mon Sep 17 00:00:00 2001 From: BlubbFish <git@blubbfish.net> Date: Sat, 10 Apr 2021 01:18:21 +0200 Subject: [PATCH] Add GetEvent so you can call events by string Add OwnSingeton class --- Utils/Helper.cs | 158 ++++++++++++++++--------------- Utils/OwnSingeton.cs | 11 +++ Utils/Properties/AssemblyInfo.cs | 7 +- Utils/Utils.csproj | 13 ++- 4 files changed, 104 insertions(+), 85 deletions(-) create mode 100644 Utils/OwnSingeton.cs diff --git a/Utils/Helper.cs b/Utils/Helper.cs index 4a583b0..7aa8b63 100644 --- a/Utils/Helper.cs +++ b/Utils/Helper.cs @@ -1,50 +1,50 @@ -using System; -using System.ComponentModel; -using System.Reflection; - -namespace BlubbFish.Utils { - public static class Helper { - #region PropertyHelper - public static Boolean HasProperty(this Object o, String type) { - Type t = o.GetType(); - foreach (PropertyInfo item in t.GetProperties()) { - if (item.Name == type) { - return true; - } - } - return false; - } - - public static Object GetProperty(this Object o, String name) { - PropertyInfo prop = o.GetType().GetProperty(name); - return prop.CanRead ? prop.GetValue(o) : null; - } - - public static void SetProperty(this Object o, String name, String value) { - PropertyInfo prop = o.GetType().GetProperty(name); - if (prop.CanWrite) { - if (prop.PropertyType == typeof(Boolean) && Boolean.TryParse(value, out Boolean vb)) { - prop.SetValue(o, vb); - } else if (prop.PropertyType == typeof(Byte) && Byte.TryParse(value, out Byte v8)) { - prop.SetValue(o, v8); - } else if (prop.PropertyType == typeof(Int32) && Int32.TryParse(value, out Int32 v32)) { - prop.SetValue(o, v32); - } else if (prop.PropertyType == typeof(Single) && Single.TryParse(value, out Single vs)) { - prop.SetValue(o, vs); - } else if (prop.PropertyType == typeof(Double) && Double.TryParse(value, out Double vd)) { - prop.SetValue(o, vd); - } else if (prop.PropertyType == typeof(Int64) && Int64.TryParse(value, out Int64 v64)) { - prop.SetValue(o, v64); - } else if (prop.PropertyType.BaseType == typeof(Enum)) { - try { - prop.SetValue(o, Enum.Parse(prop.PropertyType, value)); - } catch (Exception) { } - } - } - } - #endregion +using System; +using System.ComponentModel; +using System.Reflection; - #region FieldHelper +namespace BlubbFish.Utils { + public static class Helper { + #region PropertyHelper + public static Boolean HasProperty(this Object o, String type) { + Type t = o.GetType(); + foreach (PropertyInfo item in t.GetProperties()) { + if (item.Name == type) { + return true; + } + } + return false; + } + + public static Object GetProperty(this Object o, String name) { + PropertyInfo prop = o.GetType().GetProperty(name); + return prop.CanRead ? prop.GetValue(o) : null; + } + + public static void SetProperty(this Object o, String name, String value) { + PropertyInfo prop = o.GetType().GetProperty(name); + if (prop.CanWrite) { + if (prop.PropertyType == typeof(Boolean) && Boolean.TryParse(value, out Boolean vb)) { + prop.SetValue(o, vb); + } else if (prop.PropertyType == typeof(Byte) && Byte.TryParse(value, out Byte v8)) { + prop.SetValue(o, v8); + } else if (prop.PropertyType == typeof(Int32) && Int32.TryParse(value, out Int32 v32)) { + prop.SetValue(o, v32); + } else if (prop.PropertyType == typeof(Single) && Single.TryParse(value, out Single vs)) { + prop.SetValue(o, vs); + } else if (prop.PropertyType == typeof(Double) && Double.TryParse(value, out Double vd)) { + prop.SetValue(o, vd); + } else if (prop.PropertyType == typeof(Int64) && Int64.TryParse(value, out Int64 v64)) { + prop.SetValue(o, v64); + } else if (prop.PropertyType.BaseType == typeof(Enum)) { + try { + prop.SetValue(o, Enum.Parse(prop.PropertyType, value)); + } catch (Exception) { } + } + } + } + #endregion + + #region FieldHelper public static Object GetField(this Object o, String name) { FieldInfo field = o.GetType().GetField(name); return field.IsPublic ? field.GetValue(o) : null; @@ -53,35 +53,39 @@ namespace BlubbFish.Utils { FieldInfo field = o.GetField(name); return field.IsPublic ? field.GetValue(o) : null; } - #endregion + public static T GetEvent<T>(this Object o, String name) { + FieldInfo field = o.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic); + return (T)field?.GetValue(o); + } + #endregion - #region InterfaceHelper - public static Boolean HasInterface(this Type o, Type interf) { - foreach (Type item in o.GetInterfaces()) { - if (item == interf) { - return true; - } - } - return false; - } - - public static Boolean HasAbstract(this Type o, Type type) => o.BaseType == type; - #endregion - - #region StringHelper - public static String GetEnumDescription(Enum value) { - FieldInfo fi = value.GetType().GetField(value.ToString()); - DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); - return attributes != null && attributes.Length > 0 ? attributes[0].Description : value.ToString(); - } - - public static String ToUpperLower(this String s) => s.Length == 0 ? "" : s.Length == 1 ? s.ToUpper() : s[0].ToString().ToUpper() + s.Substring(1).ToLower(); - - public static void WriteError(String text) { - Console.ForegroundColor = ConsoleColor.Red; - Console.Error.WriteLine("ERROR: " + text); - Console.ResetColor(); - } - #endregion - } -} + #region InterfaceHelper + public static Boolean HasInterface(this Type o, Type interf) { + foreach (Type item in o.GetInterfaces()) { + if (item == interf) { + return true; + } + } + return false; + } + + public static Boolean HasAbstract(this Type o, Type type) => o.BaseType == type; + #endregion + + #region StringHelper + public static String GetEnumDescription(Enum value) { + FieldInfo fi = value.GetType().GetField(value.ToString()); + DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); + return attributes != null && attributes.Length > 0 ? attributes[0].Description : value.ToString(); + } + + public static String ToUpperLower(this String s) => s.Length == 0 ? "" : s.Length == 1 ? s.ToUpper() : s[0].ToString().ToUpper() + s[1..].ToLower(); + + public static void WriteError(String text) { + Console.ForegroundColor = ConsoleColor.Red; + Console.Error.WriteLine("ERROR: " + text); + Console.ResetColor(); + } + #endregion + } +} diff --git a/Utils/OwnSingeton.cs b/Utils/OwnSingeton.cs new file mode 100644 index 0000000..5f6413b --- /dev/null +++ b/Utils/OwnSingeton.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BlubbFish.Utils { + public abstract class OwnSingeton<T> where T : class { + private static readonly Lazy<T> _instance = new Lazy<T>(() => CreateInstanceOfT()); + public static T Instance => _instance.Value; + private static T CreateInstanceOfT() => Activator.CreateInstance(typeof(T), true) as T; + } +} diff --git a/Utils/Properties/AssemblyInfo.cs b/Utils/Properties/AssemblyInfo.cs index 7364dd0..c4e69c2 100644 --- a/Utils/Properties/AssemblyInfo.cs +++ b/Utils/Properties/AssemblyInfo.cs @@ -11,7 +11,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("BlubbFish")] [assembly: AssemblyProduct("Utils")] -[assembly: AssemblyCopyright("Copyright © BlubbFish 2014 - 02.10.2018")] +[assembly: AssemblyCopyright("Copyright © BlubbFish 2014 - 10.04.2021")] [assembly: AssemblyTrademark("BlubbFish")] [assembly: AssemblyCulture("")] [assembly: NeutralResourcesLanguage("de-DE")] @@ -35,10 +35,11 @@ using System.Runtime.InteropServices; // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.4.0")] -[assembly: AssemblyFileVersion("1.4.0")] +[assembly: AssemblyVersion("1.5.0")] +[assembly: AssemblyFileVersion("1.5.0")] #endif /** + * 1.5.0 Add GetEvent so you can call events by string; Add OwnSingeton class * 1.4.0 Add Helper to Utils */ diff --git a/Utils/Utils.csproj b/Utils/Utils.csproj index d9330fe..380dbd2 100644 --- a/Utils/Utils.csproj +++ b/Utils/Utils.csproj @@ -8,16 +8,19 @@ <Company>BlubbFish</Company> <Authors>BlubbFish</Authors> <PackageId>Utils.BlubbFish</PackageId> - <Copyright>Copyright © BlubbFish 2014 - 02.10.2018</Copyright> - <AssemblyVersion>1.4.0</AssemblyVersion> - <FileVersion>1.4.0</FileVersion> + <Copyright>Copyright © BlubbFish 2014 - 10.04.2021</Copyright> + <AssemblyVersion>1.5.0</AssemblyVersion> + <FileVersion>1.5.0</FileVersion> <NeutralLanguage>de-DE</NeutralLanguage> <PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageProjectUrl>http://git.blubbfish.net/vs_utils/Utils</PackageProjectUrl> <RepositoryUrl>http://git.blubbfish.net/vs_utils/Utils.git</RepositoryUrl> <RepositoryType>git</RepositoryType> - <Version>1.4.0</Version> - <PackageReleaseNotes>1.4.0 Add Helper to Utils</PackageReleaseNotes> + <Version>1.5.0</Version> + <PackageReleaseNotes> + 1.5.0 Add GetEvent so you can call events by string; Add OwnSingeton class + 1.4.0 Add Helper to Utils + </PackageReleaseNotes> </PropertyGroup> <ItemGroup>