Mark Hatcher
Mark Hatcher

Reputation: 23

How to create a list of the static objects from a class

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

Answers (3)

Alberto Monteiro
Alberto Monteiro

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

D Stanley
D Stanley

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:

  1. Your static fields are writable - so a client could replace HackersGuide with a different Publication object.
  2. Your class is mutable, so even if the field was read-only, a client could change one of the properties.

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

James Gaunt
James Gaunt

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

Related Questions