private static void CachePropertiesIfRequired(Type type, IPropertyInfoFetcher fetcher) { if (!PropertyCache.ContainsKey(type)) { PropertyCache[type] = GetPropertiesFor(type, fetcher); } }
/// <summary> /// Constructs a new instance of the ShimSham with the provided property info fetcher /// </summary> /// <param name="toWrap">Objects to wrap (wip: only the first object is considered)</param> /// <param name="interfaceToMimic">Interface type to mimick</param> /// <param name="isFuzzy">Flag allowing or preventing approximation</param> /// <param name="allowReadonlyDefaultsForMissingMembers">Whether to allow returning default(T) for properties which are missing on the wrapped source(s)</param> /// <param name="propertyInfoFetcher">Utility to fetch property information from the provided object and interface type</param> /// <exception cref="ArgumentNullException">Thrown if the mimick interface or property info fetch are null</exception> // ReSharper disable once MemberCanBePrivate.Global public ShimSham( object[] toWrap, Type interfaceToMimic, bool isFuzzy, bool allowReadonlyDefaultsForMissingMembers, IPropertyInfoFetcher propertyInfoFetcher) { if (interfaceToMimic == null) { throw new ArgumentNullException(nameof(interfaceToMimic)); } _propertyInfoFetcher = propertyInfoFetcher ?? throw new ArgumentNullException(nameof(propertyInfoFetcher)); _isFuzzy = isFuzzy; _wrapped = toWrap; _allowReadonlyDefaultsForMissingMembers = allowReadonlyDefaultsForMissingMembers; // TODO: store all wrapped types & use to determine proper pass-through _wrappedTypes = toWrap.Select(w => w.GetType()).ToArray(); _wrappingADuck = IsObjectADuck(); StaticallyCachePropertyInfosFor(_wrapped, _wrappingADuck); StaticallyCachePropertyInfosFor(interfaceToMimic); StaticallyCacheMethodInfosFor(_wrappedTypes); LocallyCachePropertyInfos(); LocallyCacheMethodInfos(); LocallyCacheMimickedPropertyInfos(); }
internal static Dictionary <string, PropertyInfo> FindFuzzyProperties(this Type type, IPropertyInfoFetcher fetcher) { lock (PropertyCache) { CachePropertiesIfRequired(type, fetcher); return(PropertyCache[type].FuzzyPropertyInfos); } }
private static PropertyInfoContainer GetPropertiesFor(Type type, IPropertyInfoFetcher fetcher) { var immediateProperties = fetcher.GetProperties(type, SEEK_FLAGS); var interfaceProperties = type.GetAllImplementedInterfaces() .Select(itype => fetcher.GetProperties(itype, SEEK_FLAGS)) .SelectMany(p => p); var all = immediateProperties.Union(interfaceProperties).ToArray(); return(new PropertyInfoContainer(all)); }