关于编译时的类型一无所知。
object TransformObject(object oldObject, Type newType, Func<object, object> transform)
{
if(obj.GetType().ImplementsGenericInterface(typeof(IList<>))
&& newType.ImplementsGenericInterface(typeof(IList<>)))
{
object newCollection = Activator.CreateInstance(newType);
// This is where it gets tricky:
// 1. How to iterate over the old collection?
// 2. How to add each element in the new collection?
object oldCollection = oldObject as IEnumerable;
foreach(var oldItem in oldCollection)
{
object newItem = transform(oldItem);
newCollection.Add(newItem);
}
// 3. How to ensure that the order in the new collection is preserved?
return newCollection;
}
return null;
}
自然,这是行不通的。
在运行时已知的是:
oldCollection.GetType()
实现type IList<AT>
。newType
实施类型 IList<BT>
object transform(object oldObject)
负责将类型AT
的对象转换为类型的新对象BT
。或将对象映射到不同类型的对象。映射是未知的编译时。试试这个解决方案:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Test
{
static bool ImplementsGenericInterface(Type type, Type genericInterface)
{
return type.GetInterfaces().Any(i => i.GetGenericTypeDefinition() == genericInterface);
}
static object TransformObject(object oldObject, Type newType, Func<object, object> transform)
{
if (ImplementsGenericInterface(oldObject.GetType(), typeof(IList<>))
&& ImplementsGenericInterface(newType, typeof(IList<>)))
{
object newCollection = Activator.CreateInstance(newType);
var method = newType.GetMethod("Add");
foreach (var item in (IEnumerable)oldObject)
{
var newItem = transform(item);
method.Invoke(newCollection, new object[] { newItem });
}
return newCollection;
}
return null;
}
public static void Main()
{
var list1 = new List<int>() { 1, 2, 3 };
var list2 = (List<string>)TransformObject(list1, typeof(List<string>), o => o.ToString());
foreach (var item in list2)
Console.WriteLine(item);
Console.ReadKey();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句