initial commit
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Prime31.Reflection
|
||||
{
|
||||
// Token: 0x02000021 RID: 33
|
||||
public class CacheResolver
|
||||
{
|
||||
// Token: 0x060000C5 RID: 197 RVA: 0x00007D46 File Offset: 0x00005F46
|
||||
public CacheResolver(MemberMapLoader memberMapLoader)
|
||||
{
|
||||
this._memberMapLoader = memberMapLoader;
|
||||
}
|
||||
|
||||
// Token: 0x060000C6 RID: 198 RVA: 0x00007D64 File Offset: 0x00005F64
|
||||
public static object getNewInstance(Type type)
|
||||
{
|
||||
CacheResolver.CtorDelegate ctorDelegate;
|
||||
object result;
|
||||
if (CacheResolver.constructorCache.tryGetValue(type, out ctorDelegate))
|
||||
{
|
||||
result = ctorDelegate();
|
||||
}
|
||||
else
|
||||
{
|
||||
ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
|
||||
ctorDelegate = (() => constructorInfo.Invoke(null));
|
||||
CacheResolver.constructorCache.add(type, ctorDelegate);
|
||||
result = ctorDelegate();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000C7 RID: 199 RVA: 0x00007DD4 File Offset: 0x00005FD4
|
||||
public SafeDictionary<string, CacheResolver.MemberMap> loadMaps(Type type)
|
||||
{
|
||||
SafeDictionary<string, CacheResolver.MemberMap> result;
|
||||
SafeDictionary<string, CacheResolver.MemberMap> safeDictionary;
|
||||
if (type == null || type == typeof(object))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else if (this._memberMapsCache.tryGetValue(type, out safeDictionary))
|
||||
{
|
||||
result = safeDictionary;
|
||||
}
|
||||
else
|
||||
{
|
||||
safeDictionary = new SafeDictionary<string, CacheResolver.MemberMap>();
|
||||
this._memberMapLoader(type, safeDictionary);
|
||||
this._memberMapsCache.add(type, safeDictionary);
|
||||
result = safeDictionary;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000C8 RID: 200 RVA: 0x00007E44 File Offset: 0x00006044
|
||||
private static GetHandler createGetHandler(FieldInfo fieldInfo)
|
||||
{
|
||||
return (object instance) => fieldInfo.GetValue(instance);
|
||||
}
|
||||
|
||||
// Token: 0x060000C9 RID: 201 RVA: 0x00007E74 File Offset: 0x00006074
|
||||
private static SetHandler createSetHandler(FieldInfo fieldInfo)
|
||||
{
|
||||
SetHandler result;
|
||||
if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = delegate(object instance, object value)
|
||||
{
|
||||
fieldInfo.SetValue(instance, value);
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000CA RID: 202 RVA: 0x00007ECC File Offset: 0x000060CC
|
||||
private static GetHandler createGetHandler(PropertyInfo propertyInfo)
|
||||
{
|
||||
MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true);
|
||||
GetHandler result;
|
||||
if (getMethodInfo == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = ((object instance) => getMethodInfo.Invoke(instance, Type.EmptyTypes));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000CB RID: 203 RVA: 0x00007F14 File Offset: 0x00006114
|
||||
private static SetHandler createSetHandler(PropertyInfo propertyInfo)
|
||||
{
|
||||
MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true);
|
||||
SetHandler result;
|
||||
if (setMethodInfo == null)
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = delegate(object instance, object value)
|
||||
{
|
||||
setMethodInfo.Invoke(instance, new object[]
|
||||
{
|
||||
value
|
||||
});
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x04000051 RID: 81
|
||||
private readonly MemberMapLoader _memberMapLoader;
|
||||
|
||||
// Token: 0x04000052 RID: 82
|
||||
private readonly SafeDictionary<Type, SafeDictionary<string, CacheResolver.MemberMap>> _memberMapsCache = new SafeDictionary<Type, SafeDictionary<string, CacheResolver.MemberMap>>();
|
||||
|
||||
// Token: 0x04000053 RID: 83
|
||||
private static readonly SafeDictionary<Type, CacheResolver.CtorDelegate> constructorCache = new SafeDictionary<Type, CacheResolver.CtorDelegate>();
|
||||
|
||||
// Token: 0x02000022 RID: 34
|
||||
// (Invoke) Token: 0x060000CE RID: 206
|
||||
private delegate object CtorDelegate();
|
||||
|
||||
// Token: 0x02000023 RID: 35
|
||||
public sealed class MemberMap
|
||||
{
|
||||
// Token: 0x060000D1 RID: 209 RVA: 0x00007F66 File Offset: 0x00006166
|
||||
public MemberMap(PropertyInfo propertyInfo)
|
||||
{
|
||||
this.MemberInfo = propertyInfo;
|
||||
this.Type = propertyInfo.PropertyType;
|
||||
this.Getter = CacheResolver.createGetHandler(propertyInfo);
|
||||
this.Setter = CacheResolver.createSetHandler(propertyInfo);
|
||||
}
|
||||
|
||||
// Token: 0x060000D2 RID: 210 RVA: 0x00007F9A File Offset: 0x0000619A
|
||||
public MemberMap(FieldInfo fieldInfo)
|
||||
{
|
||||
this.MemberInfo = fieldInfo;
|
||||
this.Type = fieldInfo.FieldType;
|
||||
this.Getter = CacheResolver.createGetHandler(fieldInfo);
|
||||
this.Setter = CacheResolver.createSetHandler(fieldInfo);
|
||||
}
|
||||
|
||||
// Token: 0x04000054 RID: 84
|
||||
public readonly MemberInfo MemberInfo;
|
||||
|
||||
// Token: 0x04000055 RID: 85
|
||||
public readonly Type Type;
|
||||
|
||||
// Token: 0x04000056 RID: 86
|
||||
public readonly GetHandler Getter;
|
||||
|
||||
// Token: 0x04000057 RID: 87
|
||||
public readonly SetHandler Setter;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Prime31.Reflection
|
||||
{
|
||||
// Token: 0x0200001E RID: 30
|
||||
// (Invoke) Token: 0x060000BA RID: 186
|
||||
public delegate object GetHandler(object source);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Prime31.Reflection
|
||||
{
|
||||
// Token: 0x02000020 RID: 32
|
||||
// (Invoke) Token: 0x060000C2 RID: 194
|
||||
public delegate void MemberMapLoader(Type type, SafeDictionary<string, CacheResolver.MemberMap> memberMaps);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Prime31.Reflection
|
||||
{
|
||||
// Token: 0x0200001D RID: 29
|
||||
public class ReflectionUtils
|
||||
{
|
||||
// Token: 0x060000B3 RID: 179 RVA: 0x00007BAC File Offset: 0x00005DAC
|
||||
public static Attribute getAttribute(MemberInfo info, Type type)
|
||||
{
|
||||
Attribute result;
|
||||
if (info == null || type == null || !Attribute.IsDefined(info, type))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = Attribute.GetCustomAttribute(info, type);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000B4 RID: 180 RVA: 0x00007BE8 File Offset: 0x00005DE8
|
||||
public static Attribute getAttribute(Type objectType, Type attributeType)
|
||||
{
|
||||
Attribute result;
|
||||
if (objectType == null || attributeType == null || !Attribute.IsDefined(objectType, attributeType))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = Attribute.GetCustomAttribute(objectType, attributeType);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000B5 RID: 181 RVA: 0x00007C24 File Offset: 0x00005E24
|
||||
public static bool isTypeGenericeCollectionInterface(Type type)
|
||||
{
|
||||
bool result;
|
||||
if (!type.IsGenericType)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type genericTypeDefinition = type.GetGenericTypeDefinition();
|
||||
result = (genericTypeDefinition == typeof(IList<>) || genericTypeDefinition == typeof(ICollection<>) || genericTypeDefinition == typeof(IEnumerable<>));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000B6 RID: 182 RVA: 0x00007C84 File Offset: 0x00005E84
|
||||
public static bool isTypeDictionary(Type type)
|
||||
{
|
||||
bool result;
|
||||
if (typeof(IDictionary).IsAssignableFrom(type))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
else if (!type.IsGenericType)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type genericTypeDefinition = type.GetGenericTypeDefinition();
|
||||
result = (genericTypeDefinition == typeof(IDictionary<, >));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Token: 0x060000B7 RID: 183 RVA: 0x00007CDC File Offset: 0x00005EDC
|
||||
public static bool isNullableType(Type type)
|
||||
{
|
||||
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
||||
}
|
||||
|
||||
// Token: 0x060000B8 RID: 184 RVA: 0x00007D14 File Offset: 0x00005F14
|
||||
public static object toNullableType(object obj, Type nullableType)
|
||||
{
|
||||
return (obj != null) ? Convert.ChangeType(obj, Nullable.GetUnderlyingType(nullableType), CultureInfo.InvariantCulture) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Prime31.Reflection
|
||||
{
|
||||
// Token: 0x02000024 RID: 36
|
||||
public class SafeDictionary<TKey, TValue>
|
||||
{
|
||||
// Token: 0x1700000A RID: 10
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._dictionary[key];
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x060000D5 RID: 213 RVA: 0x000080D0 File Offset: 0x000062D0
|
||||
public bool tryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return this._dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
// Token: 0x060000D6 RID: 214 RVA: 0x000080F4 File Offset: 0x000062F4
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<KeyValuePair<TKey, TValue>>)this._dictionary).GetEnumerator();
|
||||
}
|
||||
|
||||
// Token: 0x060000D7 RID: 215 RVA: 0x00008114 File Offset: 0x00006314
|
||||
public void add(TKey key, TValue value)
|
||||
{
|
||||
object padlock = this._padlock;
|
||||
lock (padlock)
|
||||
{
|
||||
if (!this._dictionary.ContainsKey(key))
|
||||
{
|
||||
this._dictionary.Add(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Token: 0x04000058 RID: 88
|
||||
private readonly object _padlock = new object();
|
||||
|
||||
// Token: 0x04000059 RID: 89
|
||||
private readonly Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Prime31.Reflection
|
||||
{
|
||||
// Token: 0x0200001F RID: 31
|
||||
// (Invoke) Token: 0x060000BE RID: 190
|
||||
public delegate void SetHandler(object source, object value);
|
||||
}
|
||||
Reference in New Issue
Block a user