views:

205

answers:

2

Below is a heavily cut down version of some code I have

public class DataInfo<T>
{
    public DataInfo(string description, Func<T, object> funcToGetValue)
    {
        this.description = description;
        this.funcToGetValue= funcToGetValue;
    }

    public readonly string description;
    public readonly Func<T, object> funcToGetValue;
}

public class DataType1
{
    public int fieldA { get; set; }
    public string fieldB { get; set; }
}

public class CurrentUse
{
    static List<DataInfo<DataType1>> data1 = new List<DataInfo<DataType1>>()
    {
        new DataInfo<DataType1>("someStuff", data => data.fieldA),
        new DataInfo<DataType1>("someOtherStuff", data => data.fieldB)
    };
}

(There are many types, and don't worry not everything is public really!)

This is working and is OK as far as it goes, but the fact that I have to keep repeating new DataInfo<DataType1> bothers me a bit.

I tried creating a non generic helper verion of DataInfo to create the objects for me as so

public class DataInfo
{
    public static DataInfo<T> Create<T>(string description, Func<T, object> func)
    {
        return new DataInfo<T>(description, func);
    }
}
public class DesiredUse
{
    static List<DataInfo<DataType1>> data1 = new List<DataInfo<DataType1>>()
    {
        DataInfo.Create("someStuff", data => data.fieldA),
        DataInfo.Create("someOtherStuff", data => data.fieldB)
    };
}

But that doesn't work as it the compiler cannot resolve fieldA & fieldB as it cannot infer the type of data.

Any ideas how I can get rid of the duplicated type info? I don't mind making changes, as long as I end up with a list of DataInfos

+3  A: 

I'd create a builder class:

public sealed class DataInfoListBuilder<T> : IEnumerable
{
    private readonly List<DataInfo<T>> list = new List<DataInfo<T>>();

    public void Add(string description, Func<T, object> function)
    {
         list.Add(DataInfo.Create<T>(description, function));
    }

    public List<DataInfo<T>> Build()
    {
        return list;
    }

    public IEnumerator GetEnumerator()
    {
        throw new InvalidOperationException
            ("IEnumerator only implemented for the benefit of the C# compiler");
    }
}

Then use it as:

static List<DataInfo<DataType1>> data1 = new DataInfoListBuilder<DataType1>
{
    { "someStuff", data => data.fieldA },
    { "someOtherStuff", data => data.fieldB }
}.Build();

I haven't tested it, but I think that should work. You could make it a non-generic type within DataInfo, in which case you'd use:

static List<DataInfo<DataType1>> data1 = new DataInfo<DataType1>.Builder
{ ... }.Build();
Jon Skeet
Thanks Jon, that works just great!
Argos
A: 

You can possibly inherit from List> and provide a specialized add method:

public class SpecialList<T> : List<DataInfo<T>>
{
    public void Add(string description, Func<T, object> func)
    {
        base.Add(new DataInfo<T>(description, func));
    }
}

Then, you can use it like this:

public class CurrentUse
{
    public static SpecialList<DataType1> Data1
    {
        get
        {
            SpecialList<DataType1> list = new SpecialList<DataType1>();
            list.Add("someStuff", data => data.fieldA);
            list.Add("someOtherStuff", data => data.fieldB);

            return list;
        }
    }
BFree