private static Dictionary <Type, KnownResourceType> discoverResourceTypes() { var assembly = Assembly.GetExecutingAssembly(); Func <Type, bool> isAResourceGenerator = (Type t) => t.GetInterfaces().Any(interf => interf == typeof(IResourceGenerator)); Func <Type, KeyValuePair <Type, ConstructorInfo> > eligibleConstructor = (Type t) => t.GetConstructors() .Where(constructor => constructor.GetParameters() .Count() == 1 && constructor.GetParameters() .All(param => param.ParameterType.GetInterfaces() .Any(interf => interf == typeof(IResource)))) .Select(constructor => new KeyValuePair <Type, ConstructorInfo>(constructor.GetParameters()[0].ParameterType, constructor)) .FirstOrDefault(); return(assembly.GetTypes() .Where(isAResourceGenerator) .Select(type => eligibleConstructor(type)) .ToDictionary( kv => kv.Key, kv => { var resourceAttribute = DependencyUtils.GetResourceAttribute(kv.Key); return new KnownResourceType { Type = kv.Key, Description = resourceAttribute?.Description, Reference = (IReference) new GenericReference(kv.Key, kv.Value), Properties = discoverResourceProperties(kv.Key) }; })); }
private IEnumerable <DependencyRequirement> getResourceDependencyRequirements(IResource resource) { var requirements = context.ExplicitDependencyRequirements.ContainsKey(resource) ? context.ExplicitDependencyRequirements[resource] : new List <DependencyRequirement>(); var propertiesWithTypeResourceAndSetters = resource.GetType().GetProperties() .Where(DependencyUtils.PropertyIsResource) .Where(DependencyUtils.PropertiesHasSetter); foreach (var property in propertiesWithTypeResourceAndSetters) { var dependencyAttribute = DependencyUtils.GetDependencyAttribute(property); var propertyIsAlreadyInExplicitRequirements = requirements.Any(requirement => requirement.Property.Name == property.Name); var propertyIsAnOptionalDependency = dependencyAttribute != null ? dependencyAttribute.Optional : false; if (!propertyIsAlreadyInExplicitRequirements && !propertyIsAnOptionalDependency) { var missingDependency = new DependencyRequirement { Property = property, ValueName = "default" }; requirements.Add(missingDependency); } } return(requirements); }
private static IEnumerable <ResourceProperty> discoverResourceProperties(Type t) => t.GetProperties() .Where(DependencyUtils.IsUserFacingProperty) .Select(property => { var dependencyAttribute = DependencyUtils.GetDependencyAttribute(property); return(new ResourceProperty() { Name = property.Name, Type = formatPropertyType(property), Description = dependencyAttribute?.Description }); });