I've got a feeling that what you are trying to do is have a single list that is shared between all instances of Foo
, but is populated only once. Perhaps a static constructor is what you want:
static Foo()
{
// note that properties needs to be static too!
foreach(var prop in properties)
{
ValidatedProperties.Add(prop.Name);
Console.WriteLine(prop.Name);
}
}
public Foo()
{
// regular constructor for creating an instance of Foo
}
Although, unless properties
is coming from some external source (e.g. a database, webservice, file), it would be a lot easier to just populate ValidatedProperties
when you declare it:
static readonly List<string> ValidatedProperties = new List<string>() { "foo", "bar" }
You could even use a ReadOnlyCollection<string>
to prevent anybody adding to it later:
static readonly ReadOnlyCollection<string> ValidatedProperties =
new ReadOnlyCollection<string>(new string[] {"foo", "bar" });