views:

62

answers:

2

So, I have been trying to research this all morning, and have had no luck. I am trying to find a way to dynamically create a method/delegate/lambda that returns a new instance of a certain class (not known until runtime) that inherits from a certain base class.

I can guarantee the following about the unknown/dynamic class

  1. It will always inherit from one known Class (Row)
  2. It will have atleast 2 constructors (one accepting a long, and one accepting an IDataRecord)

I plan on doign the following:

  1. Finding all classes that have a certain attribute on them
  2. Creating a delegate/method/lambda/whatever that creates a new instance of the class
  3. Storing the delegate/whatever along with some properties in a struct/class
  4. Insert the struct into a hashtable
  5. When needed, pull the info out of the hashtable and calling the delegate/whatever to get a new instance of the class and returning it/adding it to a list/etc.

I need help only with #2 above!!! I have no idea where to start. I really just need some reference material to get me started, or some keywords to throw into google.

This is for a compact/simple to use ORM for our office here. I understand the above is not simple, but once working, should make maintaining the code incredibly simple.

Please let me know if you need any more info! And thanks in advance! :)

+3  A: 

what about Activator.CreateInstance ?

sample:

string typeName = ...;
Activator.CreateInstance(Type.GetType(typeName), params);
Andrey
I believe there is an overload of CreateInstance that lets you specify constructor arguments. This would be your best bet.
Tejs
This will work fine, unless he has to do it thousands of times per second, in which case a compiled delegate would be much better.
Gabe
I'm looking for a compiled delegate :-\ This will be run in a website, and I would like to have any performance degradations from reflection to occur once at load if possible.
TJMonk15
Worth noting that for the compact framework you can't use this method. Then you need to go deep into reflection to do the same thing.. at least to call an ctor with params.
Stormenet
+3  A: 

You can use LINQ expressions to construct the lambda (long p) => new XXX(p) as Row at runtime:

Type type = // ...
ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(long) });

var p = Expression.Parameter(typeof(long), "p");
var expr = Expression.Lambda<Func<long, Row>>(
               Expression.TypeAs(
                   Expression.New(ctor, p),
                   typeof(Row)),
               p);

Func<long, Row> rowCreator = expr.Compile();

Row row = rowCreator(10);
dtb
Never really saw a good use of expressions, but you opened my eyes a bit :)
Stormenet
i like the style, but does it compiles to real IL and is performance as good as of new long();
Andrey
@Andrey: `expr.Compile()` compiles the expression to real IL. The performance is the same as if the lambda expression was compiled at compile-time, so it's as good as it can get :-)
dtb
so this answer should be accepted!
Andrey
although type.GetConstructor should not be called more then once, otherwise it will kill performance.
Andrey
@Andrey: The compiled lambda returned by `expr.Compile()` can be cached and reused many times later. There is no need to construct the expression more than once.
dtb
Omg! That is perfect! Thank you so much! :)
TJMonk15