P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

Generic Reflection of static types with code reuse

I have a method that iterates the fields of a class, returning their values as a CSV. I need a way to give classes access to this method in a generic fashion.

For some reason, Statics must derive from object or you get a compile error. In this case, deriving from a different base class does increase code re-useability for me. Is there another way to accomplish my goal?

I believe the only choice I have is to make my static class an instance class.

//a data container used for Mocking in inversion of control
public class FieldContainer : ReflectionHelper<FieldContainer>
{
    public static string Field1 = "Some Data";
    public static string Field2 = "Persons Name";
    public static string Field3 = "3030 Plane Ave.";
}

    public class ReflectionHelper<T>
    {
    public static string ToCSV()
    {
        StringBuilder fieldCollector = new StringBuilder();

        Type type = typeof(T);
        FieldInfo[] fields = type.GetFields();
        foreach (FieldInfo f in fields)
        {
            fieldCollector.Append(f.GetValue(null) + ",");
        }

        return fieldCollector.ToString();
    }
    }

Upvotes: 0

Views: 236

Answers (4)

Grant Thomas
Grant Thomas

Reputation: 45083

You could form it as an extension method, as such:

public static class ReflectionHelperExtensions
{
    public static string ToCSV<T>(this T instance)
    {
        var type = instance.GetType();
        var fields = type.GetFields();
        var fieldCollector = new StringBuilder();        
        foreach (FieldInfo f in fields)
        {
            fieldCollector.Append(f.GetValue(null) + ",");
        }
        return fieldCollector.ToString();
    }
}

This way, your field container classes don't need to derive from any given type, as this applies to all derivatives of object.

Upvotes: 1

gislikonrad
gislikonrad

Reputation: 3581

It's fine if you have the class as an instanced type.

public abstract class ReflectionHelper<T>
{
    protected ReflectionHelper()
    { }


    public static string ToCsv(string delimiter = ",")
    {
        var fieldCollector = new StringBuilder();

        var type = typeof(T);
        var fields = type.GetFields();
        foreach (var f in fields)
        {
            fieldCollector.Append(f.GetValue(null) + delimiter);
        }

        return fieldCollector.ToString();
    }
}

public class Something : ReflectionHelper<Something>
{
    protected Something() : base()
    {

    }
    public static string Field1 = "Some Data";
    public static string Field2 = "Persons Name";
    public static string Field3 = "3030 Plane Ave.";
}

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

Your code is perfectly valid (at least technically). Your class FieldContainer is not a static class and therefore it can derive from ReflectionHelper<T>.

However, you normally would not implement the method ToCSV in a base class, because it can basically work on ANY class. Because you want to work on static members, an extension method isn't the best way either. The simplest and cleanest way to do it, will be to have a static helper class that implements this method:

public static class ReflectionHelper
{
    public static string ToCSV<T>()
    {
        StringBuilder fieldCollector = new StringBuilder();

        Type type = typeof(T);
        FieldInfo[] fields = type.GetFields();
        foreach (FieldInfo f in fields)
        {
            fieldCollector.Append(f.GetValue(null) + ",");
        }

        return fieldCollector.ToString();
    }
}

You can use it like this:

var csv = ReflectionHelper.ToCSV<FieldContainer>();

However, I fail to see, why you would want to implement something like that at all. It doesn't seem to make too much sense.

Upvotes: 1

havardhu
havardhu

Reputation: 3616

Have you concidered using an extension method?

public static class ReflectionExtensions
{
     public static string ToCSV(this object input)
     { 
         StringBuilder fieldCollector = new StringBuilder();

         Type type = input.GetType();
         FieldInfo[] fields = type.GetFields();
         foreach (FieldInfo f in fields)
         {
             fieldCollector.Append(f.GetValue(null) + ",");
         }

         return fieldCollector.ToString();
    }
}

Then you could simply call the following on any object:

FieldContainer c = new FieldContainer();
string result = c.ToCSV();

Upvotes: 0

Related Questions