/// <summary> /// Obtains cached fieldInfo or creates a new entry, if none is found. /// </summary> private static DynamicFieldCacheEntry GetOrCreateDynamicField(FieldInfo field) { DynamicFieldCacheEntry fieldInfo; if (!fieldCache.TryGetValue(field, out fieldInfo)) { fieldInfo = new DynamicFieldCacheEntry(DynamicReflectionManager.CreateFieldGetter(field), DynamicReflectionManager.CreateFieldSetter(field)); lock (fieldCache) { fieldCache[field] = fieldInfo; } } return(fieldInfo); }
/// <summary> /// Obtains cached property info or creates a new entry, if none is found. /// </summary> private static DynamicPropertyCacheEntry GetOrCreateDynamicProperty(PropertyInfo property) { DynamicPropertyCacheEntry propertyInfo; if (!propertyCache.TryGetValue(property, out propertyInfo)) { propertyInfo = new DynamicPropertyCacheEntry(DynamicReflectionManager.CreatePropertyGetter(property), DynamicReflectionManager.CreatePropertySetter(property)); lock (propertyCache) { propertyCache[property] = propertyInfo; } } return(propertyInfo); }
/// <summary> /// Obtains cached constructor info or creates a new entry, if none is found. /// </summary> private static ConstructorDelegate GetOrCreateDynamicConstructor(ConstructorInfo constructorInfo) { ConstructorDelegate method; if (!constructorCache.TryGetValue(constructorInfo, out method)) { method = DynamicReflectionManager.CreateConstructor(constructorInfo); lock (constructorCache) { constructorCache[constructorInfo] = method; } } return(method); }
/// <summary> /// Creates a new instance of the safe method wrapper. /// </summary> /// <param name="methodInfo">Method to wrap.</param> public SafeMethod(MethodInfo methodInfo) { AssertUtils.ArgumentNotNull(methodInfo, "You cannot create a dynamic method for a null value."); state = (SafeMethodState)stateCache[methodInfo]; if (state == null) { SafeMethodState newState = new SafeMethodState(DynamicReflectionManager.CreateMethod(methodInfo), new object[methodInfo.GetParameters().Length] ); lock (stateCache.SyncRoot) { state = (SafeMethodState)stateCache[methodInfo]; if (state == null) { state = newState; stateCache[methodInfo] = state; } } } this.methodInfo = methodInfo; }