public RequiredArgument(ParameterInfo parameterInfo) { Name = parameterInfo.Name; Type = parameterInfo.ParameterType; ReflectionObject = parameterInfo; HasDefaultValue = parameterInfo.HasDefaultValue; DefaultValue = parameterInfo.DefaultValue; DemandIfAny = parameterInfo.GetCustomAttribute <DemandsInitializationAttribute>(); }
public RequiredArgument(PropertyInfo propertyInfo) { Name = propertyInfo.Name; Type = propertyInfo.PropertyType; ReflectionObject = propertyInfo; HasDefaultValue = false; DefaultValue = null; DemandIfAny = propertyInfo.GetCustomAttribute <DemandsInitializationAttribute>(); }
public RequiredArgument(IArgument a) { Name = a.Name; Type = a.GetSystemType(); ReflectionObject = a; HasDefaultValue = true; DefaultValue = a.GetValueAsSystemType(); DemandIfAny = null; }
public void SetPropertiesForClass(RuntimeArgumentCollection args, object toSetPropertiesOf) { if (toSetPropertiesOf == null) { throw new NullReferenceException(ProcessTask.Path + " instance has not been created yet! Call SetProperties after the factory has created the instance."); } if (UsefulStuff.IsAssignableToGenericType(toSetPropertiesOf.GetType(), typeof(IPipelineRequirement <>))) { throw new Exception("ProcessTask '" + ProcessTask.Name + "' was was an instance of Class '" + ProcessTask.Path + "' which declared an IPipelineRequirement<>. RuntimeTask classes are not the same as IDataFlowComponents, IDataFlowComponents can make IPipelineRequirement requests but RuntimeTasks cannot"); } if (UsefulStuff.IsAssignableToGenericType(toSetPropertiesOf.GetType(), typeof(IPipelineOptionalRequirement <>))) { throw new Exception("ProcessTask '" + ProcessTask.Name + "' was was an instance of Class '" + ProcessTask.Path + "' which declared an IPipelineOptionalRequirement<>. RuntimeTask classes are not the same as IDataFlowComponents, IDataFlowComponents can make IPipelineRequirement requests but RuntimeTasks cannot"); } //get all possible properties that we could set foreach (var propertyInfo in toSetPropertiesOf.GetType().GetProperties()) { //see if any demand initialization DemandsInitializationAttribute initialization = (DemandsInitializationAttribute)System.Attribute.GetCustomAttributes(propertyInfo).FirstOrDefault(a => a is DemandsInitializationAttribute); //this one does if (initialization != null) { try { //get the approrpriate value from arguments var value = args.GetCustomArgumentValue(propertyInfo.Name); //use reflection to set the value propertyInfo.SetValue(toSetPropertiesOf, value, null); } catch (NotSupportedException e) { throw new Exception("Class " + toSetPropertiesOf.GetType().Name + " has a property " + propertyInfo.Name + " but is of unexpected type " + propertyInfo.GetType(), e); } catch (KeyNotFoundException e) { if (initialization.Mandatory) { throw new ArgumentException( "Class " + toSetPropertiesOf.GetType().Name + " has a Mandatory property '" + propertyInfo.Name + "' marked with DemandsInitialization but no corresponding argument was provided in ArgumentCollection", e); } } } } }
/// <summary> /// Interrogates a class via reflection and enumerates it's properties to find any that have the attribute [DemandsInitialization] /// Each one of these that is found is created as a ProcessTaskArgument of the appropriate Name and PropertyType under the parent ProcessTask /// </summary> /// <returns>Each new ProcessTaskArgument created - note that it will not return existing ones that were already present (and therefore not created)</returns> public IEnumerable <IArgument> CreateArgumentsForClassIfNotExistsGeneric( Type underlyingClassTypeForWhichArgumentsWillPopulate, IArgumentHost host, IArgument[] existingArguments) { var classType = underlyingClassTypeForWhichArgumentsWillPopulate; //get all the properties that must be set on AnySeparatorFileAttacher (Those marked with the attribute DemandsInitialization var propertiesWeHaveToSet = GetRequiredProperties(classType); foreach (var required in propertiesWeHaveToSet) { //theres already a property with the same name if (existingArguments.Any(a => a.Name.Equals(required.Name))) { continue; } //create a new one var argument = host.CreateNewArgument(); //set the type and name argument.SetType(required.PropertyInfo.PropertyType); argument.Name = required.Name; DemandsInitializationAttribute attribute = required.Demand; argument.Description = attribute.Description; if (attribute.DefaultValue != null) { argument.SetValue(attribute.DefaultValue); } var saveable = argument as ISaveable; if (saveable != null) { saveable.SaveToDatabase(); } yield return(argument); } }
public void Check(ICheckNotifier notifier) { //get all possible properties that we could set foreach (var propertyInfo in _classInstanceToCheck.GetType().GetProperties()) { //see if any demand initialization DemandsInitializationAttribute demand = System.Attribute.GetCustomAttributes(propertyInfo).OfType <DemandsInitializationAttribute>().FirstOrDefault(); //this one does if (demand != null) { if (demand.Mandatory) { var value = propertyInfo.GetValue(_classInstanceToCheck); if (value == null || string.IsNullOrEmpty(value.ToString())) { notifier.OnCheckPerformed(new CheckEventArgs("DemandsInitialization Property '" + propertyInfo.Name + "' is marked Mandatory but does not have a value", CheckResult.Fail)); } } } } }
/// <summary> /// Records the fact that a given public property on a class is marked with <see cref="DemandsInitializationAttribute"/> and that the user is supposed /// to provide a value for it in an <see cref="IArgument"/> /// </summary> /// <param name="demand"></param> /// <param name="propertyInfo"></param> /// <param name="parentPropertyInfo"></param> public RequiredPropertyInfo(DemandsInitializationAttribute demand, PropertyInfo propertyInfo, PropertyInfo parentPropertyInfo = null) { Demand = demand; ParentPropertyInfo = parentPropertyInfo; PropertyInfo = propertyInfo; }