Reputation: 744
Is there a possibility to execute some code during compile time?
For example, I would like to check if the requested methods of a dynamic object exist in the parameter-type of a generic class.
// This code has no actual purpose, just as an example
public class Sample<T>
{
public Sample<T>(T instance)
{
foo = Value = instance;
/* adding some extra code(e.g. logging) to the methods of T, by
inserting a "M" in front of the method names of T */
}
public T Value { get; }
public dynamic foo { get; }
}
How it would/could be used
var foo = new Sample<string>("hey");
foo.MSubstring(0,0);
Now I want to know if there's a possibility to execute code at compile time, e.g., to throw an exception before runtime that foo.MgetSize()
doesn't exist in T.
(This question is only about how to execute code at compile time, and this sample isn't a real problem.)
I don't have a plan how to do that. Maybe using those #if
- things?
Upvotes: 4
Views: 11308
Reputation: 8999
The scenario where a certain function might exist and might not exist is a little sketchy. It is better to include a Convert method in your interface and the object itself can tell you whether it implements the functionality, and return an object with the appropriate interface, or null if it does not.
Although, it could be done with preprocessor, I wouldn't recommend it. The scenario you are describing would mean that you are including one header that does define that method for the object, or one that does not. You don't get real dynamics. So in the header that does, use a
#define HASMETHOD
and in your executor use:
#ifdef HASMETHOD
foo.MSubstring(0, 0);
#endif
But again, I strongly recommend against this. Use a method, you call at runtime to determine, that returns the interface to call.
Upvotes: 0
Reputation: 14231
Theoretically, you can use T4 for compile-time checking.
Another way is to use languages that support compile-time macros. For example, Nemerle or Boo.
Maybe you can use Roslyn. But only in C# 6.
Upvotes: 4
Reputation: 23714
In C#, you could run some code that you just compiled in the "Post Build" step of Visual Studio.
You might be able to use a tool like Post#, to do what you want by creating attributes that do the checking you have in mind. See: https://www.postsharp.net/product/how-it-works
In other languages, like Jai, you can do this easily. See "Arbitrary compile-time code execution".
Upvotes: 2
Reputation: 29213
Yes, it is possible to execute code at build time. After all, the tools that build your program are, themselves, code.
What you're asking is how to customize that build process and execute some additional logic during it. How you would do this depends on the actual tools you're using - for example, for Visual Studio and the MSBuild system, you can refer to the MSDN documentation on custom build steps and build events.
What your custom build tool would be like depends on what part of the process it will fire up in. If it works with source code, it needs to be able to parse C# source code (and probably Visual Studio project files as well). If it works by verifying the emitted assembly (the binary that's produced at the end of the typical build process), it could simply use reflection to detect the errors you want. I'd say the latter is more convenient.
Upvotes: 4
Reputation: 678
You cannot execute code at compile time by definition. You can however execute code at load time (actually before the first instance is created or any static members are referenced) using static constructors:
Upvotes: -3