-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/HuppyService/HuppyService.Core/Abstraction/IReflectionMappable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HuppyService.Core.Abstraction | ||
{ | ||
public interface IReflectionMappableToT<T> where T : class { } | ||
} |
20 changes: 20 additions & 0 deletions
20
src/HuppyService/HuppyService.Core/Abstraction/MappableToAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HuppyService.Core.Abstraction | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class MappableToAttribute : Attribute | ||
{ | ||
public Type MappableTo; | ||
public string AlternativeName; | ||
public MappableToAttribute(Type type, string mappingName) | ||
{ | ||
MappableTo = type; | ||
AlternativeName = mappingName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using HuppyService.Core.Abstraction; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HuppyService.Core.Utilities | ||
{ | ||
public static class ReflectionMapper | ||
{ | ||
public static D? Map<T, D>(T input) | ||
where T : class | ||
where D : class, new() | ||
{ | ||
//var type1 = typeof(T); | ||
//var type2 = typeof(D); | ||
|
||
// create instance of D | ||
D result = new(); | ||
|
||
// get props of D | ||
var propsOfD = typeof(D).GetProperties(); | ||
|
||
// get props of T | ||
var propsOfT = input.GetType().GetProperties(); | ||
|
||
// map props to each other via names | ||
foreach (var tProp in propsOfT) | ||
{ | ||
MappableToAttribute? attributeInfo = tProp.GetCustomAttribute(typeof(MappableToAttribute)) as MappableToAttribute; | ||
//string searchName = attributeInfo is not null ? attributeInfo.AlternativeName : tProp.Name; | ||
|
||
// get instance of D prop | ||
PropertyInfo propInfo; | ||
if (attributeInfo != null) | ||
propInfo = propsOfD.First(prop => prop.Name == tProp.Name || prop.Name == attributeInfo.AlternativeName); | ||
else | ||
propInfo = propsOfD.First(prop => prop.Name == tProp.Name); | ||
|
||
//PropertyInfo propInfo = propsOfD.First(prop => prop.Name == searchName); | ||
var propInstance = propInfo.GetValue(result, null); | ||
|
||
propInfo.SetValue(result, GetFinalProperty(attributeInfo, propInstance)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static object? GetFinalProperty(MappableToAttribute? attributeInfo, object? instance) | ||
{ | ||
// get Mappable attribute | ||
//MappableToAttribute? attributeInfo = propInfo.GetCustomAttribute(typeof(MappableToAttribute)) as MappableToAttribute; | ||
|
||
// get instance of value | ||
//var value = propInfo.GetValue(instance, null); | ||
|
||
// perform custom mappings | ||
if (attributeInfo is not null) | ||
{ | ||
if (instance is DateTime dateValue && attributeInfo.MappableTo == typeof(ulong)) | ||
{ | ||
return Miscellaneous.DateTimeToUnixTimeStamp(dateValue); | ||
} | ||
else if (instance is ulong ulongValue && attributeInfo.MappableTo == typeof(DateTime)) | ||
{ | ||
return Miscellaneous.UnixTimeStampToUtcDateTime(ulongValue); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
return instance; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/HuppyService/HuppyService.Core/Utilities/ReflectionMapperHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HuppyService.Core.Utilities | ||
{ | ||
internal class ReflectionMapperHelpers | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters