Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:53:34+00:00 2026-06-10T09:53:34+00:00

I have a base class which has an embedded List that can be used

  • 0

I have a base class which has an embedded List that can be used by all child classes to return a sorted collection.

I had been using Activator.CreateInstance(), but this is TERRBILY slow compared to a simple new() function.

I found a way to use Emit IL to do this nearly as fast as new(), but if my classes aren’t public, I get a MethodAccessException error. This seems to be common.

Is there a way around this?

Code to generate classes here:
http://codeblocks.codeplex.com/wikipage?title=FasterActivator%20Sample

Code being used with the above here:

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:53:36+00:00Added an answer on June 10, 2026 at 9:53 am

    You can use the DynamicMethod Constructor (String, Type, Type[], Type) method to generate a method associated with the class who’s members you want access to.

    The generated method will have full access to all members in the type you associate it with, and all internal methods and members the class would have access to in its module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class that inherits List<> which has multiple classes derived from
I have a base class line which has a child class arc (note that
I have a base class which has two child classes derived from it. class
So I have a base class Animal which has 2 classes inheriting from it,
I have a base class Character which has several classes deriving from it. The
I have a base class called Component which has a number of classes derived
I have a base class, B, which has two constructors, one with no paremeters
I have a base class with a property called Name, which has an XmlText
I have written a base class from which I wish to derive several child
So, I have a base class which has a private locking object like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.