Add GetEvent so you can call events by string
Add OwnSingeton class
This commit is contained in:
parent
edb3069bb0
commit
baed3c9e08
Utils
158
Utils/Helper.cs
158
Utils/Helper.cs
@ -1,50 +1,50 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Reflection;
|
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
|
|
||||||
|
|
||||||
#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) {
|
public static Object GetField(this Object o, String name) {
|
||||||
FieldInfo field = o.GetType().GetField(name);
|
FieldInfo field = o.GetType().GetField(name);
|
||||||
return field.IsPublic ? field.GetValue(o) : null;
|
return field.IsPublic ? field.GetValue(o) : null;
|
||||||
@ -53,35 +53,39 @@ namespace BlubbFish.Utils {
|
|||||||
FieldInfo field = o.GetField(name);
|
FieldInfo field = o.GetField(name);
|
||||||
return field.IsPublic ? field.GetValue(o) : null;
|
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
|
#region InterfaceHelper
|
||||||
public static Boolean HasInterface(this Type o, Type interf) {
|
public static Boolean HasInterface(this Type o, Type interf) {
|
||||||
foreach (Type item in o.GetInterfaces()) {
|
foreach (Type item in o.GetInterfaces()) {
|
||||||
if (item == interf) {
|
if (item == interf) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean HasAbstract(this Type o, Type type) => o.BaseType == type;
|
public static Boolean HasAbstract(this Type o, Type type) => o.BaseType == type;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region StringHelper
|
#region StringHelper
|
||||||
public static String GetEnumDescription(Enum value) {
|
public static String GetEnumDescription(Enum value) {
|
||||||
FieldInfo fi = value.GetType().GetField(value.ToString());
|
FieldInfo fi = value.GetType().GetField(value.ToString());
|
||||||
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||||
return attributes != null && attributes.Length > 0 ? attributes[0].Description : value.ToString();
|
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 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) {
|
public static void WriteError(String text) {
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
Console.Error.WriteLine("ERROR: " + text);
|
Console.Error.WriteLine("ERROR: " + text);
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
Utils/OwnSingeton.cs
Normal file
11
Utils/OwnSingeton.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@ using System.Runtime.InteropServices;
|
|||||||
[assembly: AssemblyConfiguration("")]
|
[assembly: AssemblyConfiguration("")]
|
||||||
[assembly: AssemblyCompany("BlubbFish")]
|
[assembly: AssemblyCompany("BlubbFish")]
|
||||||
[assembly: AssemblyProduct("Utils")]
|
[assembly: AssemblyProduct("Utils")]
|
||||||
[assembly: AssemblyCopyright("Copyright © BlubbFish 2014 - 02.10.2018")]
|
[assembly: AssemblyCopyright("Copyright © BlubbFish 2014 - 10.04.2021")]
|
||||||
[assembly: AssemblyTrademark("BlubbFish")]
|
[assembly: AssemblyTrademark("BlubbFish")]
|
||||||
[assembly: AssemblyCulture("")]
|
[assembly: AssemblyCulture("")]
|
||||||
[assembly: NeutralResourcesLanguage("de-DE")]
|
[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
|
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
|
||||||
// übernehmen, indem Sie "*" eingeben:
|
// übernehmen, indem Sie "*" eingeben:
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
[assembly: AssemblyVersion("1.4.0")]
|
[assembly: AssemblyVersion("1.5.0")]
|
||||||
[assembly: AssemblyFileVersion("1.4.0")]
|
[assembly: AssemblyFileVersion("1.5.0")]
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 1.5.0 Add GetEvent so you can call events by string; Add OwnSingeton class
|
||||||
* 1.4.0 Add Helper to Utils
|
* 1.4.0 Add Helper to Utils
|
||||||
*/
|
*/
|
||||||
|
@ -8,16 +8,19 @@
|
|||||||
<Company>BlubbFish</Company>
|
<Company>BlubbFish</Company>
|
||||||
<Authors>BlubbFish</Authors>
|
<Authors>BlubbFish</Authors>
|
||||||
<PackageId>Utils.BlubbFish</PackageId>
|
<PackageId>Utils.BlubbFish</PackageId>
|
||||||
<Copyright>Copyright © BlubbFish 2014 - 02.10.2018</Copyright>
|
<Copyright>Copyright © BlubbFish 2014 - 10.04.2021</Copyright>
|
||||||
<AssemblyVersion>1.4.0</AssemblyVersion>
|
<AssemblyVersion>1.5.0</AssemblyVersion>
|
||||||
<FileVersion>1.4.0</FileVersion>
|
<FileVersion>1.5.0</FileVersion>
|
||||||
<NeutralLanguage>de-DE</NeutralLanguage>
|
<NeutralLanguage>de-DE</NeutralLanguage>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Utils</PackageProjectUrl>
|
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Utils</PackageProjectUrl>
|
||||||
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Utils.git</RepositoryUrl>
|
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Utils.git</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<Version>1.4.0</Version>
|
<Version>1.5.0</Version>
|
||||||
<PackageReleaseNotes>1.4.0 Add Helper to Utils</PackageReleaseNotes>
|
<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>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user