forked from kerryjiang/SuperSocket.ClientEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssemblyUtil.cs
174 lines (145 loc) · 5.33 KB
/
AssemblyUtil.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
#if SILVERLIGHT || NETFX_CORE
#else
using System.Runtime.Serialization.Formatters.Binary;
#endif
using System.Linq;
namespace SuperSocket.ClientEngine.Protocol
{
public static class AssemblyUtil
{
public static bool TryCreateInstance<T>(string type, out T result)
{
Exception e;
return TryCreateInstance<T>(type, out result, out e);
}
public static bool TryCreateInstance<T>(string type, out T result, out Exception e)
{
return TryCreateInstance<T>(type, new object[0], out result, out e);
}
public static bool TryCreateInstance<T>(string type, object[] parameters, out T result, out Exception e)
{
Type instanceType = null;
result = default(T);
if (!TryGetType(type, out instanceType, out e))
return false;
try
{
object instance = Activator.CreateInstance(instanceType, parameters);
result = (T)instance;
return true;
}
catch (Exception exc)
{
e = exc;
return false;
}
}
public static bool TryGetType(string type, out Type result)
{
Exception e;
return TryGetType(type, out result, out e);
}
public static bool TryGetType(string type, out Type result, out Exception e)
{
e = null;
try
{
result = Type.GetType(type, true);
return true;
}
catch (Exception exc)
{
e = exc;
result = null;
return false;
}
}
public static IEnumerable<Type> GetImplementTypes<TBaseType>(this Assembly assembly)
{
#if NETFX_CORE
return assembly.GetExportedTypes().Where(t =>
{
var typeInfo = t.GetTypeInfo();
return typeInfo.IsSubclassOf(typeof(TBaseType)) && typeInfo.IsClass && !typeInfo.IsAbstract;
});
#else
return assembly.GetExportedTypes().Where(t =>
t.IsSubclassOf(typeof(TBaseType)) && t.IsClass && !t.IsAbstract);
#endif
}
public static IEnumerable<TBaseInterface> GetImplementedObjectsByInterface<TBaseInterface>(this Assembly assembly)
where TBaseInterface : class
{
Type interfaceType = typeof(TBaseInterface);
Type[] arrType = assembly.GetExportedTypes();
var result = new List<TBaseInterface>();
for (int i = 0; i < arrType.Length; i++)
{
var currentImplementType = arrType[i];
#if NETFX_CORE
if (currentImplementType.GetTypeInfo().IsAbstract)
continue;
#else
if (currentImplementType.IsAbstract)
continue;
#endif
var foundInterface = currentImplementType.GetInterfaces().SingleOrDefault(x => x == interfaceType);
if (foundInterface != null)
{
result.Add(currentImplementType.GetConstructor(new Type[0]).Invoke(new object[0]) as TBaseInterface);
}
}
return result;
}
#if SILVERLIGHT || NETFX_CORE
#else
public static T BinaryClone<T>(this T target)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, target);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
#endif
private static object[] m_EmptyObjectArray = new object[] { };
public static void CopyPropertiesTo(this object source, object target)
{
PropertyInfo[] properties = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
Dictionary<string, PropertyInfo> sourcePropertiesDict = properties.ToDictionary(p => p.Name);
PropertyInfo[] targetProperties = target.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < targetProperties.Length; i++)
{
var p = targetProperties[i];
PropertyInfo sourceProperty;
if (sourcePropertiesDict.TryGetValue(p.Name, out sourceProperty))
{
if (sourceProperty.PropertyType != p.PropertyType)
continue;
}
p.SetValue(target, sourceProperty.GetValue(source, m_EmptyObjectArray), m_EmptyObjectArray);
}
}
public static IEnumerable<Assembly> GetAssembliesFromString(string assemblyDef)
{
string[] assemblies = assemblyDef.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
List<Assembly> result = new List<Assembly>(assemblies.Length);
foreach (var a in assemblies)
{
#if NETFX_CORE
result.Add(Assembly.Load(new AssemblyName(a)));
#else
result.Add(Assembly.Load(a));
#endif
}
return result;
}
}
}