Note that there are some explanatory texts on larger screens.

plurals
  1. POGet collection of class instance with property combinations
    primarykey
    data
    text
    <p>I'm stuck with the following problem.</p> <p>Consider the following simple attribute.</p> <pre><code>[AttributeUsage(AttributeTargets.Property)] public class CombinationsAttribute : Attribute { public object[] PossibleValues { get; private set; } public CombinationsAttribute(params object[] values) { this.PossibleValues = values; } } </code></pre> <p>Following is the attribute usage example - just some class with some dummy properties, the value array going into the attribute is always of the property type. </p> <pre><code>public class MyClass { [Combinations(1, 2, 3, 4, 5)] public int IntProperty1 { get; set; } [Combinations(10, 15, 20, 25, 30)] public int IntProperty2 { get; set; } [Combinations("X", "Y", "Z")] public string StringProperty { get; set; } } </code></pre> <p>I want to get all instances (5 * 5 * 3 in this example) for all the combinations. How can I do that writing as little code as possible (LINQ favoured)?</p> <p><strong>EDIT: I don't know the class (MyClass) - there are many classes with public properties having CombinationsAttribute for which I need to compute all their possible combinations. .</strong> These classes always have parameterless constructors.</p> <p>Expected result example (pseudo c# for visualization):</p> <pre><code>List&lt;MyClass&gt; Combinations = GetCombinationMagicFunction(typeof(MyClass)); List[0] = MyClass { IntProperty1 = 1, IntProperty2 = 10, StringProperty = "X" } List[1] = MyClass { IntProperty1 = 1, IntProperty2 = 10, StringProperty = "Y" } List[2] = MyClass { IntProperty1 = 1, IntProperty2 = 10, StringProperty = "Z" } List[2] = MyClass { IntProperty1 = 1, IntProperty2 = 15, StringProperty = "X" } ... List[74] = MyClass { IntProperty1 = 5, IntProperty2 = 30, StringProperty = "Z" } </code></pre>
    singulars
    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.
 

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