"Marc Rohloff [TeamB]" <"on request">a écrit dans le message de news:
XXXX@XXXXX.COM...
| I use class types all the times to decide what class to instantiate. I
| can not do this in c# without going through the overhead of reflection.
You can negate some of the time penalty of reflection by using a combination
of LCG (lightweight code generation) and delegates. I have measured this
approach and found it to be something like 40 times faster than using
Activator.CreateInstance.
Here is an example of the syntax that my Class Reference system allows :
public class Base
{
#region optional if you need parameterised "virtual" constructors
public class ClassReference : ClassReference<Base>
{
public Base Create<derivedT>(int value) where derivedT : Base
{
return base.Create<derivedT>(new object[] { value });
}
}
public static ClassReference ClassRef
{
get { return new ClassReference(); }
}
#endregion
private int value;
public Base(int value)
{
this.value = value;
}
}
public class Derived : Base
{
public Derived() : this(0) { }
public Derived(int value) : base(value) { }
}
class Program
{
[STAThread]
static void Main(string[] args)
{
Base.ClassReference cr = Base.ClassRef;
Base b = cr.Create<Derived>();
// parameterised
//Base b = cr.Create<Derived>(123);
Console.WriteLine(b.GetType().ToString());
Console.ReadKey();
}
}
| I also often call virtual class methods via a class reference. Again
| this can be done using reflection which is slow as well as not
| offering any compiler checking.
You can use a similar technique for static methods
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer