- Home /
Make the compiler evaluate a string as a chunk of code to be compiled?
I want to create a method which will take ANY variable type as an argument. And by any I mean user-defined types, so it could be a very flexible method that can be reused anytime. Kind of:
static internal class ArrayFill {
public int Fill (**PARSED_STRING_TYPE** arr) {
*whatever code goes in here*
}
}
So the first idea I came up with is parsing a string, since in my last job I worked with a language that would let me to parse a string during runtime, but from my research it doesn't seem that exists here. So I thought maybe it could be done in some way prior to compilation, in Editor scripting somehow?
I thought a workaround could be overloading the method, but I would like to avoid overloading for each type.
The best solution is going to depend somewhat on what you're trying to do with the variable. But you might want to look into generics. Using generics you can define a function that takes any type of variable, like this..
int Fill<T>( T var)
{
}
But your code snippet suggests that maybe all you're doing is trying to manipulate an array of an arbitrary type, in which case you may be trying to reinvent the wheel. The generic List class already does that.
This seems like a convoluted way of making a Dictionary<string, object>
, which could easily handle custom vars of literally any kind.
Your answer
