0

I'm working on optimizing some code. We have system where we can retrieve data from file though a function call that takes the object type and the data name, and gives back the constant in question. Something like:

float value = GetData(type, "StartValue");

It can also look like this, where type is implicit though class "A":

static class A
{
  static void some_function(self)
  {
    float value = self.GetData("StartValue");
  }
}

The value is constant in a specific release and the type is implicit though what objects defines the function (classes that sort of "inheres" from class A). What I want to try is replacing all GetData calls with the actual respective values. Then creating one function for every combination of some_function and class that can call it. That information is easy to get for me, as is the constant value that is stored in a json file. The actual function is linked in at runtime with reflection, so it's easy to map in the right one.

So something like

static class MyObject //A is "a part" of this class
{
  static void some_function_MyObject_version10(self)
  {
    float value = 5;
  }
}

I've managed to do this in the most simple case with Cecil, but when these GetData calls get nested in if statements and other similar thing, complexity quickly gets out of hand I think. So I'm looking into doing this with Roslyn/CodeAnalysis, and that is instead poorly documented, or all examples and tutorials are outdated. But of course I will dig though it and make it happen if that is the best approach, but what would be the best approach for this?

Mockarutan
  • 442
  • 4
  • 28
  • 2
    Why not just generate a `.cs` file in the `obj` folder where you inline all of constants, use a proper property accessor or even a constant field that's easy for the compiler to inline during compile time and leave it at that? This is relatively easy to do from a MsBuild target. Then inject the generated class in the `compile` itemGroup for it to be compiled along with the rest of the code... – jessehouwing Sep 30 '18 at 18:04
  • Well, if that accomplishes the same thing, sure! What you described is new to me though, so could you explain it a little bit further? – Mockarutan Sep 30 '18 at 18:22
  • 2
    Check out T4 templates at compile time docs here: https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2017 – jessehouwing Sep 30 '18 at 18:35

0 Answers0