public static bool CanTarget(this IFragment fragment, Type targetType) { if (fragment == null) { return(false); } List <TargetsAttribute> attributes = fragment.GetType().GetCustomAttributes(typeof(TargetsAttribute), false).OfType <TargetsAttribute>().ToList(); if (attributes.Count == 0) { return(typeof(IBHoMObject).IsAssignableFrom(targetType)); } else { return(attributes.SelectMany(x => x.ValidTypes).Distinct().Any(x => x.IsAssignableFrom(targetType))); } }
public static IBHoMObject AddFragment(this IBHoMObject iBHoMObject, IFragment fragment, bool replace = false) { if (iBHoMObject == null || fragment == null) { return(null); } IBHoMObject o = iBHoMObject.DeepClone(); // Give a warning if the fragment is supposed to be unique but is found on the object List <Type> currentFragmentTypes = iBHoMObject.Fragments.Select(x => x.GetType()).ToList(); foreach (Type restriction in fragment.GetType().UniquenessRestrictions()) { if (currentFragmentTypes.Any(x => restriction.IsAssignableFrom(x) && x != fragment.GetType())) { Engine.Reflection.Compute.RecordWarning("There is already a fragment of type " + restriction + " on this object. \nThe Fragment will still be added but consider reviewing this task as fragments of that type are supposed to be unique."); } } // Make sure this fragment can be added to that object if (fragment.CanTarget(iBHoMObject)) { if (!replace) { o.Fragments.Add(fragment); } else { o.Fragments.AddOrReplace(fragment); } } else { Engine.Reflection.Compute.RecordError("An object of type " + iBHoMObject.GetType() + " is not a valid target for a fragment of type " + fragment.GetType() + ". The fragment was not added."); } return(o); }