forum

Home / DeveloperSection / Forums / Getting property names at compile time

Getting property names at compile time

Anonymous User 1670 10-Apr-2013
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[]
{
    "Bar",
    // Don't want this one... "Baz",
    "Quux",
     ...
};
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[]
{
    Magic_GetName(Foo.Bar),
    // Don't want this one... Foo.Baz,
    Magic_GetName(Foo.Quux),
     ...
};

How can I achieve that?
Thanks in advance!

c# c# 
Updated on 15-Apr-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By