我有一个实现接口(interface)的类,它有一个多参数构造函数和一个静态排序集合。这个类是一个有很多继承类的基类。
internal class SCO : IVotable
{
public SCO(SPListItem item, List<Vote> votes)
{
//Initialize Object
}
public static List<T> SortedCollection<T>(SPListItemCollection items, ListSortType sortType, List<Vote> votes) where T : IVotable
{
var returnlist = new List<T>();
Type genericType = typeof(T);
for (int i = 0; i < items.Count; i++) { returnlist.Add((T)Activator.CreateInstance(genericType, new object[] { items[i], votes })); }
switch (sortType)
{
case ListSortType.Hot:
returnlist.Sort((p1, p2) => p2.HotScore.CompareTo(p1.HotScore));
break;
case ListSortType.Top:
returnlist.Sort((p1, p2) => p2.VoteTotal.CompareTo(p1.VoteTotal));
break;
case ListSortType.Recent:
returnlist.Sort((p1, p2) => p2.CreatedDate.CompareTo(p1.CreatedDate));
break;
}
return returnlist;
}
}
这允许我对任何子类执行以下操作:
List<ChildClass> sortedClassList = ChildClass.SortedCollection<ChildClass>(listItems, sortType, votes);
我目前对 Activator.CreateInstance 的依赖让我很担心,因为这比直接使用 Emit IL 慢大约 100 倍。我一直在阅读一些关于 Emit IL 的文章,这个解决方案似乎很棒。
但是,我似乎无法让它工作。当我尝试实例化 ILGenerator gen = 时,它告诉我“无法在静态上下文中访问非静态字段‘方法’”
我的类不是静态的,我的构造函数也不是静态的,下面显示的静态列表还没有与 Emit 交互。我该如何进行这项工作?
到目前为止的代码:
internal class SCO : IVotable
{
//Properties emittied
static ConstructorInfo ctor = typeof(SCO).GetConstructors()[1];
delegate SCO SCOCtor(SPListItem item, List<Vote> votes);
static SCOCtor SCOCtorDelegate;
DynamicMethod method = new DynamicMethod("CreateInstance", typeof (SCO),
new Type[] {typeof (SPListItem), typeof (List<Vote>)});
ILGenerator gen = method.GetILGenerator(); //Error here
//"Cannot access non-static field 'method' in static context"
private static SCO CreateInstance(SPListItem item, List<Vote> votes)
{
return SCOCtorDelegate(item, votes);
}
}
引用博客: http://ayende.com/blog/3167/creating-objects-perf-implications
请您参考如下方法:
我有一个使用 IL 生成托管的 Activator 的替代品 here在 CodePlex 上。您也可以通过 Nuget 获取它 here (单个源文件包含,没有程序集)。
FasterActivator 的源代码是 here .
用法类似于下面概述的内容。
private static readonly Dictionary<Type, DynamicCreationDelegate> _cachedCreationDelegates = new Dictionary<Type, DynamicCreationDelegate>();
private static DynamicCreationDelegate CreateOrGet(Type typeToCreate)
{
DynamicCreationDelegate result = null;
if (!_cachedCreationDelegates.TryGetValue(typeToCreate, out result))
{
result = FastActivator.GenerateDelegate(typeToCreate,
/* List of types that make up the constructor signature of the type being constructed */
typeof(SPListItem), typeof(List<Vote>));
_cachedCreationDelegates.Add(result);
}
return result;
}
// Usage
for (int i = 0; i < items.Count; i++)
{
var creationDelegate = CreateOrGet(typeof(genericType));
returnlist.Add((T)creationDelegate(new object[] { items[i], votes }));
}
哦,这是一个应该更快的通用版本。
private static readonly Func<SPListItem, List<T>, T> _creationFunc;
private static Func<SPListItem, List<T>, T> CreateOrGetFunc()
{
if (!_creationFunc == null)
_creationFunc = FastActivator.GenerateFunc<Func<SPListItem, List<T>, T>>(/* IL generator knows all type info from generic params now */);
return _creationFunc;
}
// Usage
for (int i = 0; i < items.Count; i++)
{
var creationFunc = CreateOrGetFunc();
returnlist.Add(creationFunc(items[i], votes ));
}
希望这对您有所帮助!
