/// <summary> /// Cleanups up injectable properties. All injectable properties will be set to <c>null</c>. /// </summary> /// <remarks> /// Since this method does not perform dependency validation /// <see cref="InjectProperties" /> , <paramref name="cache" /> is used as read-only. /// Calling <see cref="CleanupInjectableProperties" /> without call to <see cref="InjectProperties" /> on the same type /// will incur performance penalty as list of properties will be evaluated every time. /// </remarks> /// <param name="kernel">Windsor kernel.</param> /// <param name="target">The target object to cleanup injectable properties.</param> /// <param name="cache">Injectable property descriptor cache.</param> public static void CleanupInjectableProperties([NotNull] this IKernel kernel, [NotNull] object target, ITypePropertyDescriptorCache cache) { if (kernel == null) { throw new ArgumentNullException(nameof(kernel)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } Type type = target.GetType(); // Cache miss expected only once per given type, so call Find() first to prevent extra closure allocation in GetOrAdd. TypePropertyDescriptor info = cache?.Find(type) ?? GetInjectableProperties(type, kernel, null); if (!info.HasProperties()) { return; } // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < info.Properties.Length; i++) { info.Properties[i].SetValue(target, null, null); } }
/// <summary> /// Returns the properties of the current object that make up the object's signature. /// </summary> public virtual PropertyInfo[] GetSignatureProperties() { Type type = GetTypeUnproxied(); // Since data won't be in cache on first request only, use .GetOrAdd as second attempt to prevent allocation of extra lambda object. TypePropertyDescriptor descriptor = signaturePropertiesCache.Find(type) ?? GetOrAdd(type); return(descriptor.Properties); }
private static TypePropertyDescriptor GetInjectableProperties(IKernel kernel, ITypePropertyDescriptorCache cache, Action <PropertyInfo, ComponentModel> validatePropertyRegistration, Type type) { // Cache miss expected only once per given type, so call Find() first to prevent extra closure allocation in GetOrAdd. TypePropertyDescriptor info = cache != null ? cache.Find(type) ?? GetOrAdd(kernel, cache, type, validatePropertyRegistration) : GetInjectableProperties(type, kernel, validatePropertyRegistration); return(info); }
private static TypePropertyDescriptor GetInjectableProperties(IKernel kernel, ITypePropertyDescriptorCache cache, Action<PropertyInfo, ComponentModel> validatePropertyRegistration, Type type) { // Cache miss expected only once per given type, so call Find() first to prevent extra closure allocation in GetOrAdd. TypePropertyDescriptor info = cache != null ? cache.Find(type) ?? GetOrAdd(kernel, cache, type, validatePropertyRegistration) : GetInjectableProperties(type, kernel, validatePropertyRegistration); return info; }