/// <summary> /// Creates a <see cref="IStumpRule"/> from a <see cref="RuleContract"/>. /// </summary> /// <param name="contract">The <see cref="RuleContract"/> used to create the <see cref="IStumpRule"/>.</param> /// <returns>A <see cref="IStumpRule"/> object created from the specified <paramref name="contract"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="contract"/> is <c>null</c>.</exception> public static IStumpRule CreateRuleFromContract(RuleContract contract) { contract = contract ?? throw new ArgumentNullException(nameof(contract)); var type = KnownRules[contract.RuleName]; var rule = Activator.CreateInstance(type) as IStumpRule; rule.InitializeFromSettings(contract.GetRuleSettings()); return(rule); }
/// <summary> /// Creates an object based on an <see cref="IStumpRule"/> from a <see cref="RuleContract"/>. /// </summary> /// <typeparam name="T">The concrete implementation of the <see cref="IStumpRule"/> rule to create.</typeparam> /// <param name="contract">The <see cref="RuleContract"/> used to create the <see cref="IStumpRule"/>.</param> /// <returns>A <see cref="IStumpRule"/> object created from the specified <paramref name="contract"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="contract"/> is <c>null</c>.</exception> public static T CreateRuleFromContract <T>(RuleContract contract) where T : IStumpRule, new() { contract = contract ?? throw new ArgumentNullException(nameof(contract)); var type = KnownRules[contract.RuleName]; var rule = Activator.CreateInstance(type) as IStumpRule; if (rule is T) { rule.InitializeFromSettings(contract.GetRuleSettings()); } else { return((T)Activator.CreateInstance(typeof(T))); } return((T)rule); }