1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
/// <exclude/>
/// <summary>
/// A collection of <see cref="Object"/> extension methods.
/// </summary>
internal static class ObjectExtensions
{
/// <summary>
/// Tries to copy the specified source object properties to the target object properties.
/// </summary>
/// <param name="sourceObject">The source object.</param>
/// <param name="targetObject">The target object.</param>
internal static void MapTo(this object sourceObject,object targetObject)
{
foreach (PropertyInfo prop in sourceObject.GetType().GetProperties())
{
PropertyInfo prop2 = sourceObject.GetType().GetProperty(prop.Name);
prop2.SetValue(targetObject, prop.GetValue(sourceObject, null), null);
}
}
/// <summary>
/// Returns the name of the specified property.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TT">The type of the t.</typeparam>
/// <param name="obj">The object.</param>
/// <param name="propertyAccessor">The property accessor.</param>
/// <returns></returns>
internal static String NameOf<T, TT>(this T obj, Expression<Func<T, TT>> propertyAccessor)
{
if (propertyAccessor.Body.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = propertyAccessor.Body as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member.Name;
}
return null;
}
internal static string ToStringReflection(this object obj)
{
StringBuilder sb = new StringBuilder();
foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly))
{
sb.Append(property.Name);
sb.Append(": ");
if (property.GetIndexParameters().Length > 0)
{
sb.Append("Indexed Property cannot be used");
}
else
{
sb.Append(property.GetValue(obj, null));
}
sb.Append(System.Environment.NewLine);
}
return sb.ToString();
}
/// <summary>
/// Extension for 'Object' that copies the properties to a destination object.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="destination">The destination.</param>
internal static void CopyProperties(this object source, object destination)
{
// If any this null throw an exception
if (source == null || destination == null)
throw new Exception("Source or/and Destination Objects are null");
// Getting the Types of the objects
Type typeDest = destination.GetType();
Type typeSrc = source.GetType();
// Iterate the Properties of the source instance and
// populate them from their desination counterparts
PropertyInfo[] srcProps = typeSrc.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
foreach (PropertyInfo srcProp in srcProps)
{
//if (!srcProp.CanRead)
//{
// continue;
//}
//PropertyInfo targetProperty = typeDest.GetProperty(srcProp.Name);
//if (targetProperty == null)
//{
// continue;
//}
//if (!targetProperty.CanWrite)
//{
// continue;
//}
//if (targetProperty.GetSetMethod(true) != null && targetProperty.GetSetMethod(true).IsPrivate)
//{
// continue;
//}
//if ((targetProperty.GetSetMethod().Attributes & MethodAttributes.Static) != 0)
//{
// continue;
//}
//if (!targetProperty.PropertyType.IsAssignableFrom(srcProp.PropertyType))
//{
// continue;
//}
// Passed all tests, lets set the value
srcProp.SetValue(destination, srcProp.GetValue(source, null), null);
}
}
}
|