7

So after looking at an article describing how to Create Object Instances Faster Than Reflection I got really excited since in my code I currently have quite a bit of reflection. Unfortunately DynamicMethod and ILGenerator are not supported in Windows CE. EDIT: Activator is supported in Windows CE

I was wondering if anyone knew of any way to create object instances faster than reflection in CE. If not, maybe someone could explain why Windows CE does not support this feature and if there are any work arounds to get this feature in CE. Even if I had to code my own DynamicMethod and ILGenerator classes it might be worth it :)

Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
  • It depends. Please provide more detail. – SLaks Feb 28 '11 at 21:32
  • Basically from any given System.Type, I would like to be able to create an instance of that Type as fast as possible. – Ian Dallas Feb 28 '11 at 21:37
  • How many object instances are you creating? FYI: I've written about the same technique (http://www.matlus.com/high-performance-class-factory/) and use it at a framework level too, but frankly, unless you're creating hundreds of thousands of objects in a tight loop you won't see any performance improvement. Note also hat if you're only using the default constructor then that is highly optimized by the framework. – Shiv Kumar Mar 01 '11 at 04:37

2 Answers2

2

First, Activator is supported. Take a look at the docs here.

That said, it's not the fastest thing on the planet, especially if you intend to create more than one instance of the given type. What I did in the OpenNETCF.IoC framework after lots of testing of different way to build up the object was to cache the ConstructorInfo on a per-type basis (specifically in the ObjectFactory class) and used that for object creation. Yes, you've got to use reflection to get the CI the first time, but subsequent calls are very fast since you've already got the delegate.

ctacke
  • 66,480
  • 18
  • 94
  • 155
1

Depending on your design, you might be able to create a set of (compile-time) instantiating delegates (you can store in a static class).

For example:

static class Factory<T> {
    public Func<T> Creator { get; set; }
}

var instance = Factory<TSomething>.Creator();

//Elsewhere
Factory<SomeClass>.Creator = () => new SomeClass();

This will only help if you can populate the factory in advance with the relevant types.


If all you have is a Type (as opposed to a generic parameter), you can store delegates in a Dictionary<Type, Func<object>>, although it will be less efficient due to casting.
You'd still need to populate the dictionary.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • This is a possibility but it seems like it would get unwieldy in a large application/framework which is unfortunately what I am dealing with. The size of the Dictionary would become far too large and new Types would not be easy to support. – Ian Dallas Feb 28 '11 at 21:39
  • @tkeE: Can you store only the most frequently-used types? – SLaks Feb 28 '11 at 21:43
  • That is a possibility but not entirely desirable for the reasons listed above. It hard to know exactly what will be the most frequently used since this is a framework which will be used by many systems. – Ian Dallas Feb 28 '11 at 21:48
  • I'm not sure what other options you have; I've never worked with .Net CF. – SLaks Feb 28 '11 at 21:48