Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>private class PropertyItem { public PropertyInfo Property { get; set; } public List&lt;object&gt; PossibleValues { get; set; } public int CurrentIndex { get; set; } public int Count { get; set; } } public IEnumerable&lt;T&gt; GenerateCombinations&lt;T&gt;() where T : new() { return GenerateCombinations(typeof(T)).Cast&lt;T&gt;(); } public IEnumerable&lt;object&gt; GenerateCombinations(Type type) { // Collect nessecery information var constructor = type.GetConstructor(Type.EmptyTypes); var properties = ( from p in type.GetProperties() let values = p.GetCustomAttributes(typeof(CombinationsAttribute), true) .Cast&lt;CombinationsAttribute&gt;() .SelectMany(a =&gt; a.PossibleValues) .ToList() where values.Count &gt; 0 select new PropertyItem { Property = p, PossibleValues = values, CurrentIndex = 0, Count = values.Count, } ) .ToList(); bool isDone; do { // Construct and return the current item var item = constructor.Invoke(new object[0]); foreach (var prop in properties) { prop.Property.SetValue(item, prop.PossibleValues[prop.CurrentIndex]); } yield return item; // Move the indices to the next item isDone = true; for (int i = 0; i &lt; properties.Count; i++) { var prop = properties[i]; if (prop.CurrentIndex == prop.Count - 1) { prop.CurrentIndex = 0; } else { prop.CurrentIndex++;; isDone = false; break; } } } while (!isDone); } </code></pre> <p><strong>Usage:</strong></p> <pre><code>var allObjects1 = GenerateCombinations&lt;MyClass&gt;().ToList(); var allObjects2 = GenerateCombinations(typeof(MyClass)).ToList(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload