Creating object instances is faster than reflection in Windows CE

So, having looked at the article describing How to Create Instances of Objects Faster Than Reflection ... I was very worried because in my code I currently have quite a bit of reflection. Unfortunately, DynamicMethod and ILGenerator not supported on Windows CE. EDIT : Activator Supported in Windows CE

I was wondering if anyone knows about the possibility of instantiating an object faster than reflection in CE. If not, maybe someone can explain why Windows CE does not support this feature, and if there is any problem to get this feature in CE. Even if I had to program my own DynamicMethod and ILGenerator , it might be worth it :)

+7
source share
2 answers

Firstly, the activator is supported. Check out the docs here .

However, this is not the fastest thing on the planet, especially if you intend to create more than one instance of this type. What I did in the OpenNETCF.IoC framework after many tests in different ways to create an object was ConstructorInfo caching for each type (especially in the ObjectFactory class ) and is used to create the object. Yes, you must use reflection to get the CI for the first time, but subsequent calls are very fast, since you already have a delegate.

+2
source

Depending on your design, you can create a set of instances (instances of the compile-time instance) (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 prefill the factory with the appropriate types.


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

+1
source

All Articles