I have the following problem: I need to test a list of algorithms (~300) with maximum speed performance.
Since every one is unique, I create them as static classes and made a execute() function like bellow.
Each one does have some fixed parameters (the same amount) that eventually, I can make as consts;
I was able to get a list of execute() methods, make a delegate and run it.
Now in C I would make some function pointers and that's it.
Make a array of function pointers.
How can I get a delegate to entire static object, not only to particular method?
Actually I need a list or array of them.
I would prefer to do some heavy lifting in initialization() like reflection, so I can have max. performance at runtime at execute();
Now I am not sure this is the best approach, I'm not expert in C#.
Thanks for suggestions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public static class algorithms
{
public static void initialize()
{
List<Type> types = typeof(algorithms).GetNestedTypes(BindingFlags.Public | BindingFlags.Static).ToList();
foreach ( Type t in types )
{
var method = t.GetMethod("Execute");
var execute = (Func<int, int>)Delegate.CreateDelegate(typeof(Func<int, int>), null, method);
int reply = execute(0x12345678); // I was able to obtain *ptr to execute() for each one
// how can I obtain a *ptr to entire object in order to access it's members too ?
}
}
// list of ~300 algorithms, unique (so no need to have instances)
public static class alg1
{
public static string Name; // each share the same parameters
public static string Alias;
public static int Execute(int data) // the same execute function
{
// but different processing for each algorithm
return 1;
}
}
public static class alg2
{
public static string Name;
public static string Alias;
public static int Execute(int data)
{
return 2;
}
}
public static class alg3
{
public static string Name;
public static string Alias;
public static int Execute(int data)
{
return 3;
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire