/// <summary> /// Scrutinizes the variable attribute and ensures it has been annotated correctly. /// </summary> /// <param name="attr">The attribute to scrutinize.</param> private static void _guardBadAttribute(AlgorithmPropertyAttribute attr) { if (attr.CompressorType != null) { CompressorAttribute cattr = attr.CompressorType.GetCustomAttribute( typeof(CompressorAttribute)) as CompressorAttribute; if (cattr == null) { throw new ArgumentException("Compressor type provided not annotated."); } if (attr.CompressorType.GetInterfaces().Contains(typeof(ICompressor)) == false) { throw new ArgumentException("Compressor type does not implement ICompressor."); } } if (attr.PublicType != null) { if (attr.PublicTypeConverter == null) { throw new ArgumentException("Faux type converter not provided."); } if (attr.PublicTypeConverter.GetInterfaces().Contains(typeof(IValueConverter)) == false) { throw new ArgumentException("PublicTypeConverter does not implement IValueConverter."); } } }
/// <summary> /// Constructs a set of Property objects representing the annotated properties /// in the type /// </summary> /// <param name="processType">The type to gather the properties from</param> /// <returns>A set of Property objects representing the annotated properties</returns> private static IEnumerable <Property> _gatherAlgorithmProperties(Type processType) { List <Property> properties = new List <Property>(); foreach (PropertyInfo property in processType.GetProperties()) { var varAttrs = property.GetCustomAttributes().OfType <AlgorithmPropertyAttribute>(); if (varAttrs.Any()) { AlgorithmPropertyAttribute attr = varAttrs.First(); Property p = _createProperty(property, attr); properties.Add(p); } } return(properties); }
/// <summary> /// Constructs the Property definition using the CLR information and /// the metadata provided in the attribute /// </summary> /// <param name="property">The reflected information about the property /// within the class</param> /// <param name="attr">The additional information provided by the /// plugin</param> /// <returns>A Property definition</returns> private static Property _createProperty(PropertyInfo property, AlgorithmPropertyAttribute attr) { _guardBadAttribute(attr); PropertyBuilder b = new PropertyBuilder(); b.Name = attr.VariableIdentifier; b.PropertyType = property.PropertyType; b.PublicType = attr.PublicType; b.DefaultValue = attr.DefaultValue; if (attr.CompressorType != null) { b.Compressor = Activator.CreateInstance(attr.CompressorType) as ICompressor; } if (attr.PublicTypeConverter != null) { b.Converter = Activator.CreateInstance(attr.PublicTypeConverter) as IValueConverter; } return(b.Build()); }