Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C# is a statically typed language. The compiler wants to know you property you mean <em>at compile time</em>.</p> <p>However, you can do this with reflection if you want to. Alternatively, if you know the type of the data in advance, you could use a switch statement. (Example of both approaches coming.)</p> <pre><code>using System; using System.Reflection; public class Demo { public string Foo { get; set; } public string Bar { get; set; } public string Baz { get; set; } } public class Test { static void Main() { Demo demo = new Demo { Foo = "foo value", Bar = "bar value", Baz = "surprise!" }; ShowProperty(demo, "Foo"); ShowProperty(demo, "Bar"); ShowProperty(demo, "Baz"); ShowDemoProperty(demo, "Foo"); ShowDemoProperty(demo, "Bar"); ShowDemoProperty(demo, "Baz"); } // Here we don't know the type involved, so we have to use reflection static void ShowProperty(object x, string propName) { PropertyInfo property = x.GetType().GetProperty(propName); if (property == null) { Console.WriteLine("No such property: {0}", propName); } else { Console.WriteLine("{0}: {1}", propName, property.GetValue(x, null)); } } // Here we *know* it's a Demo static void ShowDemoProperty(Demo demo, string propName) { string value; // Note that this is very refactoring-unfriendly. Generally icky! switch (propName) { case "Foo": value = demo.Foo; break; case "Bar": value = demo.Bar; break; case "Baz": value = demo.Baz; break; default: Console.WriteLine("No such property: {0}", propName); return; } Console.WriteLine("{0}: {1}", propName, value); } } </code></pre> <p>I agree with the other answers which suggest this probably shows a slightly scary design...</p>
    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.
    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.
    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