使用 lambda 参数时如何减少类型声明?

发布于 2024-07-24 06:59:36 字数 1546 浏览 6 评论 0原文

下面是我拥有的一些代码的大幅削减版本

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

(有很多类型,不用担心并非所有内容都是公开的!)

这是有效的,并且就目前而言还可以,但事实上我必须保留重复 new DataInfo 让我有点烦恼。

我尝试创建 DataInfo 的非通用帮助程序版本来为我创建对象,

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

但这不起作用,因为编译器无法解析 fieldA & fieldB,因为它无法推断数据的类型。

有什么想法可以摆脱重复的类型信息吗? 我不介意进行更改,只要我最终得到 DataInfos 列表

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-07-31 06:59:36

我会创建一个构建器类:

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

然后将其用作:

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

我还没有测试过它,但我认为这应该可行。 您可以将其设为 DataInfo 中的非泛型类型,在这种情况下您可以使用:

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

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();
少女净妖师 2024-07-31 06:59:36

您可以从 List> 继承 并提供专门的添加方法:

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

然后,您可以像这样使用它:

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

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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文