Skip to content

Commit

Permalink
Added Singleton. Added into Environment, Reflection and String.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Dec 19, 2017
1 parent 5e3aea7 commit d138468
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 41 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# GM.Utility
.NET library with various static classes that provide universally useful functions and extensions.
.NET library with various static classes and tools that provide universally useful functions, extensions and utilities.

[![Release](https://img.shields.io/github/release/GregaMohorko/GM.Utility.svg?style=flat-square)](https://github.com/GregaMohorko/GM.Utility/releases/latest)
[![NuGet](https://img.shields.io/nuget/v/GM.Utility.svg?style=flat-square)](https://www.nuget.org/packages/GM.Utility)

## List of classes

**Utilities**:
**Utilities (static classes)**:
- [Array](src/GM.Utility/GM.Utility/ArrayUtility.cs)
- [Bool](src/GM.Utility/GM.Utility/BoolUtility.cs)
- [Cryptography](src/GM.Utility/GM.Utility/CryptographyUtility.cs)
Expand All @@ -27,6 +27,9 @@
- [String](src/GM.Utility/GM.Utility/StringUtility.cs)
- [XML](src/GM.Utility/GM.Utility/XMLUtility.cs)

**Software design patterns**:
- [Singleton](src/GM.Utility/GM.Utility/Singleton.cs)

**Other**:
- [KeyCodes](src/GM.Utility/GM.Utility/KeyCodes.cs)
- [Util](src/GM.Utility/GM.Utility/Util.cs)
Expand Down
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ ClientBin/
*.dbmdl
*.dbproj.schemaview
*.pfx
*.snk
*.publishsettings
node_modules/
orleans.codegen.cs
Expand Down
3 changes: 2 additions & 1 deletion src/GM.Utility/GM.Utility/CryptographyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public static string GetChecksum(string file)
}

string checksum = BitConverter.ToString(checksumBytes);

// remove the hyphen
return checksum.Replace("-", string.Empty);
return checksum.RemoveAllOf("-");
}
}
}
13 changes: 11 additions & 2 deletions src/GM.Utility/GM.Utility/EnvironmentUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -48,8 +49,7 @@ public static class EnvironmentUtility
[Obsolete("Method GetWindowsBit is deprecated, please use GetOperatingSystemBitness instead.",true)]
public static int GetWindowsBit()
{
// FIXME obsolete
// 2017-10-27
// FIXME obsolete 2017-10-27
return GetOperatingSystemBitness();
}

Expand Down Expand Up @@ -84,5 +84,14 @@ public static string GetProcessArchitecture()
{
return Environment.Is64BitProcess ? "x64" : "x86";
}

/// <summary>
/// Gets the root directory information of the system drive. The system drive is considered the one on which the windows directory is located.
/// </summary>
public static string GetSystemDrive()
{
string windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
return Path.GetPathRoot(windowsPath);
}
}
}
5 changes: 3 additions & 2 deletions src/GM.Utility/GM.Utility/GM.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>GM.StrongNameKey.pfx</AssemblyOriginatorKeyFile>
<AssemblyOriginatorKeyFile>GM.StrongNameKey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -70,13 +70,14 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionUtility.cs" />
<Compile Include="SecureStringUtility.cs" />
<Compile Include="Singleton.cs" />
<Compile Include="StatisticUtility.cs" />
<Compile Include="StringUtility.cs" />
<Compile Include="Util.cs" />
<Compile Include="XMLUtility.cs" />
</ItemGroup>
<ItemGroup>
<None Include="GM.StrongNameKey.pfx" />
<None Include="GM.StrongNameKey.snk" />
<None Include="GM.Utility.licenseheader" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
2 changes: 1 addition & 1 deletion src/GM.Utility/GM.Utility/GM.Utility.licenseheader
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ SOFTWARE.

Project: %Project%
Created: %CreationYear%-%CreationMonth%-%CreationDay%
Author: %UserDisplayName%
Author: %UserName%
*/
6 changes: 3 additions & 3 deletions src/GM.Utility/GM.Utility/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("GM.Utility")]
[assembly: AssemblyDescription("Library with various static classes that provide universally useful functions and extensions.")]
[assembly: AssemblyDescription("Library with various static classes and tools that provide universally useful functions, extensions and utilities.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Grega Mohorko")]
[assembly: AssemblyProduct("GM.Utility")]
Expand All @@ -60,5 +60,5 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyVersion("1.1.2.0")]
[assembly: AssemblyFileVersion("1.1.2.0")]
99 changes: 69 additions & 30 deletions src/GM.Utility/GM.Utility/ReflectionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace GM.Utility
/// </summary>
public static class ReflectionUtility
{
#region ASSEMBLY
/// <summary>
/// Specifies the type of the assembly.
/// </summary>
Expand Down Expand Up @@ -237,32 +238,7 @@ public static string GetAssemblyFileLocalPath(this Assembly assembly)
var uri = new Uri(codebase, UriKind.Absolute);
return uri.LocalPath;
}

/// <summary>
/// Sets the specified property to the provided value in the object.
/// </summary>
/// <param name="obj">The object with the property.</param>
/// <param name="propertyName">The name of the property to set.</param>
/// <param name="value">The value to set the property to.</param>
public static void SetProperty(this object obj, string propertyName, object value)
{
PropertyInfo property = GetPropertyInfo(obj, propertyName);
property.SetValue(obj, value);
}

/// <summary>
/// Sets the specified property to a value that will be extracted from the provided string value using the <see cref="TypeDescriptor.GetConverter(Type)"/> and <see cref="TypeConverter.ConvertFromString(string)"/>.
/// </summary>
/// <param name="obj">The object with the property.</param>
/// <param name="propertyName">The name of the property to set.</param>
/// <param name="valueAsString">The string representation of the value to set to the property.</param>
public static void SetPropertyFromString(this object obj, string propertyName, string valueAsString)
{
PropertyInfo property = GetPropertyInfo(obj, propertyName);
TypeConverter converter = TypeDescriptor.GetConverter(property.PropertyType);
object value = converter.ConvertFromString(valueAsString);
property.SetValue(obj, value);
}
#endregion // Assembly

/// <summary>
/// Gets the value of the specified property in the object.
Expand All @@ -278,16 +254,14 @@ public static object GetPropertyValue(this object obj, string propertyName)
/// <summary>
/// Gets the type of the specified property in the type.
/// <para>
/// If the type is nullable, this function gets its generic definition.
/// If the type is nullable, this function gets its generic definition. To get the real type, use <see cref="GetPropertyTypeReal(Type, string)"/>.
/// </para>
/// </summary>
/// <param name="type">The type that has the specified property.</param>
/// <param name="propertyName">The name of the property.</param>
public static Type GetPropertyType(this Type type, string propertyName)
{
PropertyInfo property = GetPropertyInfo(type, propertyName);

Type propertyType = property.PropertyType;
Type propertyType = GetPropertyTypeReal(type, propertyName);

// get the generic type of nullable, not THE nullable
if(propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) {
Expand All @@ -297,6 +271,17 @@ public static Type GetPropertyType(this Type type, string propertyName)
return propertyType;
}

/// <summary>
/// Gets the type of the specified property in the type.
/// </summary>
/// <param name="type">The type that has the specified property.</param>
/// <param name="propertyName">The name of the property.</param>
public static Type GetPropertyTypeReal(this Type type, string propertyName)
{
PropertyInfo property = GetPropertyInfo(type, propertyName);
return property.PropertyType;
}

/// <summary>
/// Gets the property information by name for the type of the object.
/// </summary>
Expand Down Expand Up @@ -350,6 +335,32 @@ public static IEnumerable<FieldInfo> GetConstants(this Type type, bool includeIn
return fields.Where(fi => fi.IsLiteral && !fi.IsInitOnly);
}

/// <summary>
/// Sets the specified property to the provided value in the object.
/// </summary>
/// <param name="obj">The object with the property.</param>
/// <param name="propertyName">The name of the property to set.</param>
/// <param name="value">The value to set the property to.</param>
public static void SetProperty(this object obj, string propertyName, object value)
{
PropertyInfo property = GetPropertyInfo(obj, propertyName);
property.SetValue(obj, value);
}

/// <summary>
/// Sets the specified property to a value that will be extracted from the provided string value using the <see cref="TypeDescriptor.GetConverter(Type)"/> and <see cref="TypeConverter.ConvertFromString(string)"/>.
/// </summary>
/// <param name="obj">The object with the property.</param>
/// <param name="propertyName">The name of the property to set.</param>
/// <param name="valueAsString">The string representation of the value to set to the property.</param>
public static void SetPropertyFromString(this object obj, string propertyName, string valueAsString)
{
PropertyInfo property = GetPropertyInfo(obj, propertyName);
TypeConverter converter = TypeDescriptor.GetConverter(property.PropertyType);
object value = converter.ConvertFromString(valueAsString);
property.SetValue(obj, value);
}

/// <summary>
/// Determines whether or not this object has a property with the specified name.
/// </summary>
Expand Down Expand Up @@ -430,15 +441,43 @@ public static bool IsGenericList(this Type type)

/// <summary>
/// Determines whether this type is a primitive.
/// <para><see cref="string"/> is considered a primitive.</para>
/// </summary>
/// <param name="type">The type.</param>
public static bool IsPrimitive(this Type type)
{
if(type == typeof(string)) {
// string is considered as a primitive
return true;
}

return type.IsValueType && type.IsPrimitive;
}

/// <summary>
/// Gets the default value of this type.
/// </summary>
/// <param name="type">The type for which to get the default value.</param>
public static object GetDefault(this Type type)
{
// https://stackoverflow.com/questions/325426/programmatic-equivalent-of-defaulttype

if(!type.IsValueType) {
// is a reference type
return null;
}

Func<object> f = GetDefaultGeneric<object>;
return f.Method.GetGenericMethodDefinition().MakeGenericMethod(type).Invoke(null, null);
}

/// <summary>
/// Gets the default value of the specified type.
/// </summary>
/// <typeparam name="T">The type for which to get the default value.</typeparam>
public static T GetDefaultGeneric<T>()
{
return default(T);
}
}
}
64 changes: 64 additions & 0 deletions src/GM.Utility/GM.Utility/Singleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
MIT License
Copyright (c) 2017 Grega Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Project: GM.Utility
Created: 2017-12-19
Author: GregaMohorko
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GM.Utility
{
/// <summary>
/// Base class for a thread-safe singleton class.
/// </summary>
/// <typeparam name="T">The type of the actual singleton class.</typeparam>
public abstract class Singleton<T> where T:Singleton<T>
{
private static object _lock_instance = new object();
private static T _instance;
/// <summary>
/// Gets the instance of this singleton.
/// </summary>
public static T Instance
{
get
{
if(_instance == null) {
lock(_lock_instance) {
if(_instance == null) {
// create an instance of T using the public, protected or private parameterless constructor
_instance = (T)Activator.CreateInstance(typeof(T), true);
}
}
}
return _instance;
}
}
}
}
35 changes: 35 additions & 0 deletions src/GM.Utility/GM.Utility/StringUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ namespace GM.Utility
/// </summary>
public static class StringUtility
{
/// <summary>
/// Returns a new string in which all occurences of the specified value are removed.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="value">The string to seek and remove.</param>
public static string RemoveAllOf(this string text, string value)
{
// TEST performance
//return text.Replace(value, string.Empty);

int index = text.LastIndexOf(value);
while(index >= 0) {
text = text.Remove(index, value.Length);
--index;
index = text.LastIndexOf(value, index);
}
return text;
}

/// <summary>
/// Returns a new string in which the first occurence of the specified value is removed.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="value">The string to seek and remove its first occurence.</param>
public static string RemoveFirstOf(this string text, string value)
{
int index = text.IndexOf(value);
if(index < 0) {
// the value is not present in the text
return text;
}

return text.Remove(index, value.Length);
}

/// <summary>
/// Removes the whitespace in this text.
/// </summary>
Expand Down

0 comments on commit d138468

Please sign in to comment.