Skip to content

Commit

Permalink
Merge pull request #22 from I-RzR-I/feature/AddSmallImprovements
Browse files Browse the repository at this point in the history
Fix enums extensions. Add new methods in TExtensions.
  • Loading branch information
I-RzR-I authored Jan 31, 2024
2 parents 3fecf7f + ebefed1 commit 2567cf3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,6 @@ Report/
coverage.*

# Certificates
*.pfx
*.pfx

*.ncrunchsolution
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022-2023 RzR
Copyright (c) 2022-2024 RzR

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 5 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@
### **v.1.0.13.8399**
-> Add excel column name generator `GetExcelColumnName`.<br />
-> Adjust method modifier for `GetDuplicates`.<br />
-> Fix tests.
-> Fix tests.

### **v.1.0.14.6517**
-> Fix some enums extensions.<br />
-> Add new methods (`AppendTo`, `GetPropertiesInfoFromSource`) in 'TExtensions'.
41 changes: 41 additions & 0 deletions src/DomainCommonExtensions/CommonExtensions/TExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

using DomainCommonExtensions.DataTypeExtensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using CodeSource;

#pragma warning disable SCS0007

#endregion

namespace DomainCommonExtensions.CommonExtensions
Expand Down Expand Up @@ -144,5 +149,41 @@ public static XmlDocument SerializeToXmlDoc<T>(this T source, string rootName =

return doc;
}

///-------------------------------------------------------------------------------------------------
/// <summary>A T extension method that appends to.</summary>
/// <remarks></remarks>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="first">The first to act on.</param>
/// <param name="items">A variable-length parameters list containing items.</param>
/// <returns>A T[].</returns>
///=================================================================================================
public static T[] AppendTo<T>(this T first, params T[] items)
{
var result = new T[items.Length + 1];
result[0] = first;
items.CopyTo(result, 1);

return result;
}

///-------------------------------------------------------------------------------------------------
/// <summary>
/// A TSource extension method that gets properties information from source.
/// </summary>
/// <remarks></remarks>
/// <typeparam name="TSource">Type of the source.</typeparam>
/// <param name="source">.</param>
/// <returns>The properties information from source.</returns>
///=================================================================================================
public static IList<PropertyInfo> GetPropertiesInfoFromSource<TSource>(this TSource source) where TSource : class
{
try
{
return typeof(TSource).GetProperties()
.ToList();
}
catch { return null; }
}
}
}
12 changes: 6 additions & 6 deletions src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static Dictionary<int, string> GetEnumDefinition(this Type enumType)
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static string GetEnumMemberValue<T>(this T value) where T : struct, IConvertible
public static string GetEnumMemberValue<T>(this T value) where T : struct, Enum, IConvertible
{
var element = typeof(T).GetTypeInfo().DeclaredMembers
.SingleOrDefault(x => x.Name == value.ToString(CultureInfo.InvariantCulture));
Expand All @@ -73,7 +73,7 @@ public static string GetEnumMemberValue<T>(this T value) where T : struct, IConv
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <returns></returns>
public static T ToEnumMemberValue<T>(this string str)
public static T ToEnumMemberValue<T>(this string str) where T : struct, Enum, IConvertible
{
var enumType = typeof(T);
foreach (var name in Enum.GetNames(enumType))
Expand All @@ -96,7 +96,7 @@ public static T ToEnumMemberValue<T>(this string str)
/// <returns></returns>
/// <typeparam name="T"></typeparam>
/// <remarks></remarks>
public static int ToInt<T>(this T source) where T : IConvertible
public static int ToInt<T>(this T source) where T : Enum, IConvertible
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
Expand All @@ -111,7 +111,7 @@ public static int ToInt<T>(this T source) where T : IConvertible
/// <returns></returns>
/// <typeparam name="T"></typeparam>
/// <remarks></remarks>
public static string ToString<T>(this T source) where T : IConvertible
public static string ToString<T>(this T source) where T : Enum, IConvertible
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
Expand Down Expand Up @@ -145,7 +145,7 @@ public static string GetDescription(this Enum value, bool returnEmpty = false)
/// <returns></returns>
/// <typeparam name="T"></typeparam>
/// <remarks></remarks>
public static T GetEnumValue<T>(this string str) where T : struct, IConvertible
public static T GetEnumValue<T>(this string str) where T : struct, Enum, IConvertible
{
if (!typeof(T).IsEnum) throw new Exception("T must be an Enumeration type.");
var val = ((T[])Enum.GetValues(typeof(T)))[0];
Expand All @@ -167,7 +167,7 @@ public static T GetEnumValue<T>(this string str) where T : struct, IConvertible
/// <returns></returns>
/// <typeparam name="T"></typeparam>
/// <remarks></remarks>
public static T GetEnumValue<T>(this int intValue) where T : struct, IConvertible
public static T GetEnumValue<T>(this int intValue) where T : struct, Enum, IConvertible
{
if (!typeof(T).IsEnum) throw new Exception("T must be an Enumeration type.");
var val = ((T[])Enum.GetValues(typeof(T)))[0];
Expand Down
8 changes: 4 additions & 4 deletions src/shared/GeneralAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

[assembly: AssemblyCompany("RzR ®")]
[assembly: AssemblyProduct("Common data type extensions")]
[assembly: AssemblyCopyright("Copyright © 2022-2023 RzR All rights reserved.")]
[assembly: AssemblyCopyright("Copyright © 2022-2024 RzR All rights reserved.")]
[assembly: AssemblyTrademark("® RzR™")]
[assembly:
AssemblyDescription(
Expand All @@ -47,6 +47,6 @@
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
#endif

[assembly: AssemblyVersion("1.0.13.8399")]
[assembly: AssemblyFileVersion("1.0.13.8399")]
[assembly: AssemblyInformationalVersion("1.0.13.8399")]
[assembly: AssemblyVersion("1.0.14.6517")]
[assembly: AssemblyFileVersion("1.0.14.6517")]
[assembly: AssemblyInformationalVersion("1.0.14.6517")]

0 comments on commit 2567cf3

Please sign in to comment.