Question

est ci-dessous une version coupé très bas de certains code que j'ai

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)
    };
}

(Il existe de nombreux types, et ne vous inquiétez pas pas tout est vraiment public!)

Cela fonctionne et est OK dans la mesure où il va, mais le fait que je dois continuer à répéter new DataInfo<DataType1> me dérange un peu.

J'ai essayé de créer une verion d'aide non générique de Datainfo pour créer les objets pour moi comme si

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)
    };
}

Mais cela ne fonctionne pas comme il le compilateur ne peut pas résoudre FieldA & FieldB comme il ne peut pas déduire le type de données.

Toutes les idées comment je peux me débarrasser de l'info de type dupliquée? Cela ne me dérange pas de faire des changements, aussi longtemps que je me retrouve avec une liste de DataInfos

Était-ce utile?

La solution

Je crée une classe de constructeur:

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");
    }
}

Utilisez ensuite comme:

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

Je ne l'ai pas testé, mais je pense que cela devrait fonctionner. Vous pouvez faire un type non générique au sein Datainfo, auquel cas vous pouvez utiliser:

static List<DataInfo<DataType1>> data1 = new DataInfo<DataType1>.Builder
{ ... }.Build();

Autres conseils

Vous pouvez éventuellement hériter de la liste> et de fournir une méthode d'ajout spécialisée:

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

Ensuite, vous pouvez l'utiliser comme ceci:

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;
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top