internal FastReflectionClassMap(Type type) { CreateInstance = type.DelegateForCtor(); var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public) .OrderBy(x => x.MetadataToken); foreach (var prop in props) { var pInfo = new FastReflectionPropertyInfo() { Getter = prop.DelegateForGet(), Setter = prop.DelegateForSet(), PropertyType = prop.PropertyType }; PropertyInfos[prop.Name] = pInfo; } }
public static CtorInvoker <T> DelegateForCtor <T>(this Type type, params Type[] paramTypes) { int key = kCtorInvokerName.GetHashCode() ^ type.GetHashCode(); for (int i = 0; i < paramTypes.Length; i++) { key ^= paramTypes[i].GetHashCode(); } Delegate result; if (cache.TryGetValue(key, out result)) { return((CtorInvoker <T>)result); } #if HAS_EMIT var dynMethod = new DynamicMethod(kCtorInvokerName, typeof(T), new Type[] { typeof(object[]) }); emit.il = dynMethod.GetILGenerator(); GenCtor <T>(type, paramTypes); result = dynMethod.CreateDelegate(typeof(CtorInvoker <T>)); cache[key] = result; return((CtorInvoker <T>)result); #else ConstructorInfo constructorInfo = type.GetDeclaredConstructor(paramTypes); CtorInvoker <T> methodDelegate = (object[] parameters) => { var value = constructorInfo.Invoke(parameters); return(value is T ? (T)value : default(T)); }; cache[key] = methodDelegate; return(methodDelegate); #endif }
/// <summary>Try generates or gets a strongly-typed open-instance delegate to the specified type constructor that takes the specified type params.</summary> public static bool TryMakeDelegateForCtor <T>(this Type instanceType, Type[] paramTypes, out CtorInvoker <T> result) { if (null == instanceType) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.instanceType); } ConstructorInfo ctor = null; if (!(instanceType == TypeConstants.StringType || instanceType.IsArray || instanceType.IsInterface || instanceType.IsGenericTypeDefinition)) { try { ctor = instanceType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, paramTypes ?? Type.EmptyTypes, null); } catch { } } if (ctor == null) { if (paramTypes == null || paramTypes.Length == 0) { var emptyInvoker = instanceType.GetConstructorMethod(); result = (object[] ps) => (T)emptyInvoker.Invoke(); return(true); } result = null; return(false); } result = MakeDelegateForCtor <T>(instanceType, paramTypes, ctor); return(true); }
/// <summary>Try generates or gets a weakly-typed open-instance delegate to the specified type constructor that takes the specified type params.</summary> public static bool TryMakeDelegateForCtor(this Type instanceType, Type[] paramTypes, out CtorInvoker <object> result) => TryMakeDelegateForCtor <object>(instanceType, paramTypes, out result);
public BenchmarkFactory() { var ctor = BenchmarkType.DelegateForCtor <TBenchmark>(BenchmarkType.Module, typeof(IBenchmarkConfiguration)); ConstructorInvoker = ctor; }