Add GetEvent so you can call events by string

Add OwnSingeton class
This commit is contained in:
BlubbFish 2021-04-10 01:18:21 +02:00
parent edb3069bb0
commit baed3c9e08
4 changed files with 104 additions and 85 deletions

View File

@ -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
}
}

11
Utils/OwnSingeton.cs Normal file
View File

@ -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;
}
}

View File

@ -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
*/

View File

@ -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>