簡體   English   中英

使用FastActivator代替Activator.CreateInstance()

[英]Using FastActivator in place of Activator.CreateInstance()

嘗試使用此處顯示的Class作為Activator.CreateInstance()的示例http://codeblocks.codeplex.com/wikipage?title=FasterActivator%20Sample

    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)); }
        }
        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;
    }

錯誤在for循環中,獲得MethodAccessException:

.Post__041c49eec0a6466da11894b0455b1162(Microsoft.SharePoint.SPListItem, System.Collections.Generic.List 1)`

出現錯誤的控制台應用程序(要求FastActivator類位於同一名稱空間中):

namespace testcase
{
    class Program
    {
        static void Main(string[] args)
        {
            var vote1 = new Vote() {ItemID=1};
            List<Vote> votes = new List<Vote>();
            votes.Add(vote1);

            List<string> strings = new List<string>();
            strings.Add("test");
            List<Post> posts = Post.SortedCollection<Post>(strings, ListSortType.Hot, votes);
        }
    }

    internal interface IVotable
    {
        double HotScore { get; set; }
        double VoteTotal { get; set; }
        DateTime CreatedDate { get; set; }
    }
    internal class SCO : IVotable
    {
        public double HotScore { get; set; }
        public double VoteTotal { get; set; }
        public DateTime CreatedDate { get; set; }

        public SCO(string item, List<Vote> votes)
        {
            VoteTotal = 10;
            HotScore = 10;
            CreatedDate = DateTime.Now;
        }

        public static List<T> SortedCollection<T>(List<string> items, ListSortType sortType, List<Vote> votes) where T : IVotable
        {
            var returnlist = new List<T>();
            Type genericType = typeof(T);
            var functionCreator = FastActivator.GenerateFunc<Func<string, List<Vote>, T>>();

            for (int i = 0; i < items.Count; i++) { returnlist.Add(functionCreator(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;
        }
    }

    class Vote
    {
        public double ItemID { get; set; }
    }
    class VoteMeta
    {
        public double UpVotes { get; set; }
        public double DownVotes { get; set; }
        public string CurrentUserVoteClass { get; set; }
    }
    internal enum ListSortType { Hot, Top, Recent };

    class Post : SCO
    {
        public string Summary { get; set; }
        public Uri Link { get; set; }

        public Post(string item, List<Vote> votes)
            : base(item, votes)
        {
            Summary = "Summary";
            Link = new UriBuilder("http://www.google.com").Uri;
        }
    }
}

您可能在某個地方有一個非公共構造函數。 如果未明確指定名稱空間成員的可見性 ,則會使其內部可見 ,而不是公共的。 這些修改使示例程序可以正常工作。 請注意,動態方法位於動態生成的程序集中,因此無法訪問內部成員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM