Reputation: 23
I'm setting up data for testing by creating a few dozen static objects of a custom type. I want to have a list of these objects so that I can make dynamic assertions during testing. Here's the class and the objects:
public class Publication
{
public string Name { get; set; }
public string DropdownText { get; set; }
public string DropdownValue { get; set; }
public string BaseURL { get; set; }
public static Publication MotocrossWeekly = new Publication {
Name = "Motocross Weekly",
DropdownText = "Motocross Weekly",
DropdownValue = "18",
};
public static Publication ExtremeWelding = new Publication {
Name = "Extreme Welding",
DropdownText = "Extreme Welding",
DropdownValue = "6",
};
public static Publication HackersGuide = new Publication {
Name = "Hacker's Guide to Security",
DropdownText = "Hacker's Guide",
DropdownValue = "36",
};
...
public static IList<Publication> Publications = ???;
The goal is to have a static list of Publications that would contain all of the Publication objects within the Publication class. This is to avoid having to manually write out each object in the list and editing the list every time one is added or removed from the system.
I think this can be accomplished with reflection, but I can't find specifics for what I'm trying to do.
Upvotes: 2
Views: 4357
Reputation: 6219
You can use reflection to do that
public class Publication
{
public string Name { get; set; }
public string DropdownText { get; set; }
public string DropdownValue { get; set; }
public string BaseURL { get; set; }
public static Publication MotocrossWeekly = new Publication
{
Name = "Motocross Weekly",
DropdownText = "Motocross Weekly",
DropdownValue = "18",
};
public static Publication ExtremeWelding = new Publication
{
Name = "Extreme Welding",
DropdownText = "Extreme Welding",
DropdownValue = "6",
};
public static Publication HackersGuide = new Publication
{
Name = "Hacker's Guide to Security",
DropdownText = "Hacker's Guide",
DropdownValue = "36",
};
public static IList<Publication> Publications
{
get
{
return typeof(Publication).GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(f => (Publication)f.GetValue(null))
.ToList();
}
}
}
Upvotes: 2
Reputation: 152521
Well, to get the list of public, static fields you can do:
typeof(Publication).GetFields(BindingFlags.Static|BindingFlags.Public)
and loop or use Linq to find those that are of type Publication
.
but there are a few other things to consider in your class design:
HackersGuide
with a different Publication
object.A better design would be to have get-only static properties:
private static Publication _MotocrossWeekly = new Publication {
Name = "Motocross Weekly",
DropdownText = "Motocross Weekly",
DropdownValue = "18",
};
public static MotocrossWeekly {get {return _MotocrossWeekly; } }
// etc.
Upvotes: 1
Reputation: 14783
Something like this should work:
FieldInfo[] fields = typeof(Publication).GetFields(BindingFlags.Public | BindingFlags.Static)
FieldInfo
should tell you all you need to know about the field:
https://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo(v=vs.110).aspx
Upvotes: 0