forked from arunmariappan/Ext.NET.Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectUtils.cs
More file actions
69 lines (57 loc) · 2.17 KB
/
ObjectUtils.cs
File metadata and controls
69 lines (57 loc) · 2.17 KB
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
/********
* @version : 1.3.0
* @author : Ext.NET, Inc. http://www.ext.net/
* @date : 2012-02-29
* @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license : See license.txt and http://www.ext.net/license/.
********/
using System.ComponentModel;
using System.Reflection;
namespace Ext.Net.Utilities
{
public static class ObjectUtils
{
public static bool IsNull(this object instance)
{
return instance == null;
}
public static T Apply<T>(object to, object from) where T : IComponent
{
return (T)ObjectUtils.Apply(to, from);
}
public static object Apply(object to, object from)
{
return ObjectUtils.Apply(to, from, true);
}
public static object Apply(object to, object from, bool ignoreDefaultValues)
{
PropertyInfo toProperty;
object fromValue = null;
object defaultValue = null;
foreach (PropertyInfo fromProperty in from.GetType().GetProperties())
{
if (fromProperty.CanRead)
{
fromValue = fromProperty.GetValue(from, null);
if (ignoreDefaultValues)
{
defaultValue = ReflectionUtils.GetDefaultValue(fromProperty);
if (fromValue != null && fromValue.Equals(defaultValue))
{
continue;
}
}
if (fromValue != null)
{
toProperty = to.GetType().GetProperty(fromProperty.Name, BindingFlags.Instance | BindingFlags.Public);
if (toProperty != null && toProperty.CanWrite && toProperty != null && toProperty.GetType().Equals(fromProperty.GetType()))
{
toProperty.SetValue(to, fromValue, null);
}
}
}
}
return to;
}
}
}