Home > DeveloperSection > Forums > Getting property names at compile time
Ankit Singh

Total Post:341

Posted on    April-10-2013 1:43 AM

 C# C# 

 1 Reply(s)
 835  View(s)
Rate this:
Hi Everyone!

I have a class with some properties:

class Foo
    int Bar { get; set; }
    string Baz { get; set; }
    bool Quux { get; set; }
For use in some storage API, I need to specify a subset of these properties, by name as strings:

var props = new string[]
    // Don't want this one... "Baz",
This works, but is unsafe - if I mistype "Quux", I won't get a compilation error, just (hopefully) a runetime error. I tried reflection - typeof(Foo).GetProperties

("Bar") - but that would also fail only in runtime.

Ideally, I'd like to do something like:

var props = new string[]
    // Don't want this one... Foo.Baz,

How can I achieve that?
Thanks in advance!


Total Post:604

Posted on    April-15-2013 7:49 AM

Hi Ankit!

Make the properties public to access them outside the class

class Foo
    public int Bar { get; set; }
    public string Baz { get; set; }
    public bool Quux { get; set; }

Don't want to miss updates? Please click the below button!

Follow MindStick