모든 하위 클래스에서 정렬 된 컬렉션을 반환하는 데 사용할 수있는 포함 된 목록이있는 기본 클래스가 있습니다.내부 클래스에서 IL을 사용 하시겠습니까?
나는 Activator.CreateInstance()
을 사용하고 있었지만 이것은 간단한 new() 함수에 비해 천천히 느린 것입니다.
Emit IL을 사용하여 new()처럼 빠르게 처리하는 방법을 찾았지만 수업이 공개되지 않으면 MethodAccessException
오류가 발생합니다. 이것은 일반적인 것으로 보인다.
이 방법이 있습니까?
코드는 여기에 클래스를 생성합니다 :
public static List<T> SortedCollection<T>(SPListItemCollection items, ListSortType sortType, List<Vote> votes) where T : IVotable
{
var returnlist = new List<T>();
var functionCreator = FastActivator.GenerateFunc<Func<SPListItem, List<Vote>, T>>();
for (int i = 0; i < items.Count; i++) { returnlist.Add(functionCreator(items[i], votes)); }
//Old code here
//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;
}
//Usage
//Post : BaseClass which has above method
return Post.SortedCollection<Post>(listItems,sortType,votes);