2

给定 aCollection<T>其类型T仅在运行时(而不是在编译时)已知,我想生成一个ImmutableList<T>.

我想创建的方法可能像:

var immutableList = CreateImmutableList(originalList, type);

其中 originalList 是IEnumerable,type 是T生成的ImmutableList<T>.

如何?!

(我正在使用 NET .Core)

编辑:感谢评论,我找到了一个可行的解决方案。它使用 AddRange 方法。

namespace Sample.Tests
{
    using System;
    using System.Collections;
    using System.Collections.Immutable;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Reflection;
    using Xunit;

    public class ImmutabilityTests
    {
        [Fact]
        public void CollectionCanBeConvertedToImmutable()
        {
            var original = new Collection<object>() { 1, 2, 3, 4, };
            var result = original.AsImmutable(typeof(int));

            Assert.NotEmpty(result);
            Assert.IsAssignableFrom<ImmutableList<int>>(result);
        }
    }

    public static class ReflectionExtensions
    {
        public static IEnumerable AsImmutable(this IEnumerable collection, Type elementType)
        {
            var immutableType = typeof(ImmutableList<>).MakeGenericType(elementType);
            var addRangeMethod = immutableType.GetMethod("AddRange");
            var typedCollection = ToTyped(collection, elementType);

            var emptyImmutableList = immutableType.GetField("Empty").GetValue(null);
            emptyImmutableList = addRangeMethod.Invoke(emptyImmutableList, new[] { typedCollection });
            return (IEnumerable)emptyImmutableList;
        }

        private static object ToTyped(IEnumerable original, Type type)
        {
            var method = typeof(Enumerable).GetMethod("Cast", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type);
            return method.Invoke(original, new object[] { original });
        }
    }
}
4

3 回答 3

3

您可以使用反射来做到这一点:

  1. Type为_ImmutableList<T>
  2. 用数据填充它
  3. 把它返还

这是一个演示的LINQPad程序。我假设“不可变列表”是指System.Collections.Immutable.ImmutableList<T>通过 Nuget 可用:

void Main()
{
    object il = CreateImmutableList(new[] { 1, 2, 3, 4, 5 }, typeof(int));
    il.GetType().Dump();
    il.Dump();
}

public static object CreateImmutableList(IEnumerable collection, Type elementType)
{
    // TODO: guard clauses for parameters == null
    var resultType = typeof(ImmutableList<>).MakeGenericType(elementType);
    var result = resultType.GetField("Empty").GetValue(null);
    var add = resultType.GetMethod("Add");
    foreach (var element in collection)
        result = add.Invoke(result, new object[] { element });
    return result;
}

输出:

System.Collections.Immutable.ImmutableList`1[System.Int32]

1
2
3
4
5
于 2016-06-16T12:06:13.143 回答
0

如果你准备好装箱/拆箱,那么你可以做这样的事情

           var objectCollection = new Collection<object>();
            objectCollection.Add(3);
            objectCollection.Add(4);
            var immutableList = objectCollection.ToImmutableList();

这里元素类型是int,我们将值添加到对象集合中。如果我们想取回输入的值,我们可以这样做:

    foreach (var obj in immutableList)
                {
                    int myVal = (int) Convert.ChangeType(obj, typeof(int));
                    Console.WriteLine(myVal);
                }

注意:如果您的列表很大并且元素类型是属于装箱/拆箱的值类型,它可能会对性能产生影响

于 2016-06-16T11:56:46.703 回答
0

您可以使用CreateRange. 要最小化反射,请使用辅助方法:

private static ImmutableList<T> CreateImmutableList<T>(IEnumerable<object> source)
  => ImmutableList.CreateRange(source.Cast<T>());

private MethodInfo _createImmutableList = typeof(ReflectionExtensions)
  .GetMethod(nameof(CreateImmutableList), BindingFlags.NonPublic | BindingFlags.Static);

// then: 
return _createImmutableList 
  .MakeGenericMethod(elementType)
  .Invoke(null, new object [] { collection });

于 2021-03-20T14:00:28.030 回答