使用FastActivator代替Activator.CreateInstance() [英] Using FastActivator in place of Activator.CreateInstance()

查看:132
本文介绍了使用FastActivator代替Activator.CreateInstance()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用此处显示的Class作为Activator.CreateInstance()的示例 http://codeblocks.codeplex.com/wikipage?title=FasterActivator%20Sample

Trying to use Class shown here as a sample for 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:

Error is in the for loop, getting 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;
        }
    }
}

推荐答案

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

You probably have a non-public constructor somewhere. Not explicitly specifying a visibility on a namespace member makes it internal, not public. These modifications made your example program work correctly. Note that the dynamic method is in a dynamically generated assembly and as such doesn't have access to internal members.

这篇关于使用FastActivator代替Activator.CreateInstance()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆