0

尝试使用此处显示的类作为 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.List1)`

出现错误的控制台应用程序(要求 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;
        }
    }
}
4

1 回答 1

3

您可能在某处有一个非公共构造函数。没有明确指定命名空间成员的可见性使其成为内部的,而不是公共的。这些修改使您的示例程序正常工作。请注意,动态方法位于动态生成的程序集中,因此无法访问内部成员。

于 2012-08-27T21:05:02.410 回答