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 8440249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:14:22+00:00 2026-06-10T08:14:22+00:00

I have a class implementing an interface, which has a multi-parameter constructor, and a

  • 0

I have a class implementing an interface, which has a multi-parameter constructor, and a static sorted collection. This class is a Base Class which has many inherited classes.

internal class SCO : IVotable
{
    public SCO(SPListItem item, List<Vote> votes)
    {
        //Initialize Object
    }

    public static List<T> SortedCollection<T>(SPListItemCollection items, ListSortType sortType, List<Vote> votes) where T : IVotable
    {
        var returnlist = new List<T>();
        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;
    }
}

This allows me to do the following with any Child Class:

List<ChildClass> sortedClassList = ChildClass.SortedCollection<ChildClass>(listItems, sortType, votes);

My current reliance on Activator.CreateInstance worries me, as this is about 100 times slower than using Emit IL directly. I’ve been reading a few articles about Emit IL, and it seems fantastic for this solution.

I cannot seem to get it to work, however. When I try to instantiate ILGenerator gen = it tells me “Cannot access non-static field ‘method’ in static context”

My class isn’t static, neither are my constructors, and the static list show below isn’t interacting with Emit yet. How do I make this work?

Code thus far:

internal class SCO : IVotable
{
    //Properties emittied
    static ConstructorInfo ctor = typeof(SCO).GetConstructors()[1];
    delegate SCO SCOCtor(SPListItem item, List<Vote> votes);
    static SCOCtor SCOCtorDelegate;

    DynamicMethod method = new DynamicMethod("CreateInstance", typeof (SCO),
                             new Type[] {typeof (SPListItem), typeof (List<Vote>)});

    ILGenerator gen = method.GetILGenerator(); //Error here
    //"Cannot access non-static field 'method' in static context"

    private static SCO CreateInstance(SPListItem item, List<Vote> votes)
    {
        return SCOCtorDelegate(item, votes);
    }
}

Blog for reference:
http://ayende.com/blog/3167/creating-objects-perf-implications

  • 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-10T08:14:24+00:00Added an answer on June 10, 2026 at 8:14 am

    I have a drop-in replacement for Activator that uses IL generation hosted here on CodePlex. You can also get it via Nuget here (a single source file include, no assemblies).

    The source code for FasterActivator is here.

    Usage is something like what’s outlined below.

    private static readonly Dictionary<Type, DynamicCreationDelegate> _cachedCreationDelegates = new Dictionary<Type, DynamicCreationDelegate>();
    
    private static DynamicCreationDelegate CreateOrGet(Type typeToCreate)
    {
        DynamicCreationDelegate result = null;
    
        if (!_cachedCreationDelegates.TryGetValue(typeToCreate, out result))
        {
            result = FastActivator.GenerateDelegate(typeToCreate, 
            /* List of types that make up the constructor signature of the type being constructed */
            typeof(SPListItem), typeof(List<Vote>));
            _cachedCreationDelegates.Add(result);
        }
    
        return result;
    }
    
    // Usage 
    for (int i = 0; i < items.Count; i++) 
    { 
        var creationDelegate = CreateOrGet(typeof(genericType));
        returnlist.Add((T)creationDelegate(new object[] { items[i], votes })); 
    }
    

    Oh, and here is a generic version that ought to be faster.

    private static readonly Func<SPListItem, List<T>, T> _creationFunc;
    private static Func<SPListItem, List<T>, T> CreateOrGetFunc()
    {
        if (!_creationFunc == null)
            _creationFunc = FastActivator.GenerateFunc<Func<SPListItem, List<T>, T>>(/* IL generator knows all type info from generic params now */);
    
        return _creationFunc;
    }
    
    // Usage
    for (int i = 0; i < items.Count; i++) 
    { 
        var creationFunc = CreateOrGetFunc();
        returnlist.Add(creationFunc(items[i], votes )); 
    }
    

    Hope this helps!

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

Sidebar

Related Questions

I have an interface with several events I have base class implementing the interface
I have an interface and a class implementing the interface like this: public interface
I have a Collection<T> . I have a class TManager implementing an interface UManager
I have Class and Student objects. Both have collection of another as property. Which
I have a generic class DirectorySource<T> which depends on an interface IDirectorySearch<T> . Both
I have an interface (say Employee) which has 2 classes that implements it (say
The issue appears is when I have a class implementing an interface, and extending
I have a WCF service which exposes a Generic interface (and the service has
I've got a project which has in it a protocol, a class implementing that
I have a project where I have an interface, an Abstract class implementing the

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.