Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a class that may get you started, though I haven't tested it much. Note that this will only work for classes that have a no-args constructor, and won't work for some types of recursive classes (e.g. a class with a property of its own type, such as a tree). You also may want to pre-populate more classes in the static constructor.</p> <pre><code>public static class PermutationGenerator { private static class Permutation&lt;T&gt; { public static IEnumerable&lt;T&gt; Choices { get; set; } } static PermutationGenerator() { Permutation&lt;int&gt;.Choices = new List&lt;int&gt; { -1, 0, 1 }.AsReadOnly(); Permutation&lt;string&gt;.Choices = new List&lt;string&gt; { null, "", "Hello World" }.AsReadOnly(); } public static IEnumerable&lt;T&gt; GetPermutations&lt;T&gt;() { if (Permutation&lt;T&gt;.Choices == null) { var props = typeof(T).GetProperties().Where(p =&gt; p.CanWrite); Permutation&lt;T&gt;.Choices = new List&lt;T&gt;(GeneratePermutations&lt;T&gt;(() =&gt; Activator.CreateInstance&lt;T&gt;(), props)).AsReadOnly(); } return Permutation&lt;T&gt;.Choices; } private static IEnumerable GetPermutations(Type t) { var method = typeof(PermutationGenerator).GetMethod("GetPermutations", new Type[] {}).MakeGenericMethod(t); return (IEnumerable)(method.Invoke(null,new object[] {})); } private delegate T Generator&lt;T&gt;(); private static IEnumerable&lt;T&gt; GeneratePermutations&lt;T&gt;(Generator&lt;T&gt; generator, IEnumerable&lt;PropertyInfo&gt; props) { if (!props.Any()) { yield return generator(); } else { var prop = props.First(); var rest = props.Skip(1); foreach (var propVal in GetPermutations(prop.PropertyType)) { Generator&lt;T&gt; gen = () =&gt; { var obj = generator(); prop.SetValue(obj, propVal, null); return (T)obj; }; foreach (var result in GeneratePermutations(gen, rest)) { yield return result; } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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