通用类型扩展方法 ToMultiDimensionalArray

发布于 2024-11-14 06:01:58 字数 995 浏览 2 评论 0原文

我目前正在尝试使用泛型类型的扩展方法将 IEnumerable 转换为 T2 类型的二维数组。您还应该能够选择要包含到该数组中的 T 的哪些属性。

这是我到目前为止得到的信息:

public static T2[][] ToMultidimensionalArray<T, T2>(this IEnumerable<T> enumerable, int count, params string[] propNames)
    {
        IEnumerator<T> enumerator = enumerable.GetEnumerator();
        T2[][] resultArray = new T2[count][];
        int i = 0;
        int arrLength = propNames.Length;
        while (enumerator.MoveNext())
        {
            resultArray[i] = new T2[arrLength];
            int j = 0;
            foreach(string prop in propNames)
            {
                resultArray[i][j] = ((T)enumerator.Current).//How do I access the properties?
                j++;
            }
            i++;
        }
        return resultArray;
    }

我在访问 foreach-Loop 中的 enumerator.Current 属性时遇到问题。

我正在使用 .NET-Framework 4.0。

任何意见将不胜感激。

谢谢,

丹尼斯

I'm currently trying to convert an IEnumerable<T> to a 2-dimensional array of type T2 using an extension method with generic types. You should also be able to choose which properties of T you want to include into that array.

Here's what I got so far:

public static T2[][] ToMultidimensionalArray<T, T2>(this IEnumerable<T> enumerable, int count, params string[] propNames)
    {
        IEnumerator<T> enumerator = enumerable.GetEnumerator();
        T2[][] resultArray = new T2[count][];
        int i = 0;
        int arrLength = propNames.Length;
        while (enumerator.MoveNext())
        {
            resultArray[i] = new T2[arrLength];
            int j = 0;
            foreach(string prop in propNames)
            {
                resultArray[i][j] = ((T)enumerator.Current).//How do I access the properties?
                j++;
            }
            i++;
        }
        return resultArray;
    }

I'm having a problem accessing the properties of enumerator.Current within the foreach-Loop.

I'm using .NET-Framework 4.0.

Any input would be greatly appreciated.

Thanks,

Dennis

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

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

发布评论

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

评论(2

违心° 2024-11-21 06:01:58

一般来说,这个问题可以使用反射来解决:

public static T2[][] ToMultidimensionalArray<T, T2>(
                                                this IEnumerable<T> enumerable,
                                                int count,
                                                params string[] propNames)
{
    T2[][] resultArray = new T2[count][];
    int i = 0;
    int arrLength = propNames.Length;
    foreach (var item in enumerable)
    {
        resultArray[i] = new T2[arrLength];
        int j = 0;
        foreach (string prop in propNames)
        {
            // Get the required property info using reflection
            var propertyInfo = typeof(T).GetProperty(prop);
            // Extract the getter method of the property
            var getter = propertyInfo.GetGetMethod();
            // Invoke the getter and get the property value
            var value = getter.Invoke(item, null);
            // Cast the value to T2 and store in the array
            resultArray[i][j] = (T2) value;
            j++;
        }
        i++;
    }
    return resultArray;
}

我将问题理解为有一个 T 集合,其中这些对象具有 T2 类型的属性。目标是获取每个对象的属性并将它们放入多维数组中。如果我错了请纠正我。

In general, this problem can be solved using reflection:

public static T2[][] ToMultidimensionalArray<T, T2>(
                                                this IEnumerable<T> enumerable,
                                                int count,
                                                params string[] propNames)
{
    T2[][] resultArray = new T2[count][];
    int i = 0;
    int arrLength = propNames.Length;
    foreach (var item in enumerable)
    {
        resultArray[i] = new T2[arrLength];
        int j = 0;
        foreach (string prop in propNames)
        {
            // Get the required property info using reflection
            var propertyInfo = typeof(T).GetProperty(prop);
            // Extract the getter method of the property
            var getter = propertyInfo.GetGetMethod();
            // Invoke the getter and get the property value
            var value = getter.Invoke(item, null);
            // Cast the value to T2 and store in the array
            resultArray[i][j] = (T2) value;
            j++;
        }
        i++;
    }
    return resultArray;
}

I understood the problem as having a Ts collection where these objects have properties of T2 type. The goal is to take the properties of each object and place them in a multidimensional array. Correct me if I'm wrong.

微凉徒眸意 2024-11-21 06:01:58

你的意思是
(T2)typeof(T).GetProperty(prop).GetValue(enumerator.Current, null);

但我无法理解你想要什么。我认为这个方法行不通。

Do you mean
(T2)typeof(T).GetProperty(prop).GetValue(enumerator.Current, null);

But I can't understand what you want. I don't think this method can work.

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