Search code examples
c#xnaoverridingc-preprocessorusing-directives

Global "using SpriteBatch = MySpriteBatch" for all file's, not per file


In my XNA game, I have extended SpriteBatch to override some of the functions to do what I want, and added a few of my own.

I wanted force other members to use this class without having to change all instances of "SpriteBatch" to "MySpriteBatch"

In files, you can do something like:

using SpriteBatch = MySpriteBatch; // Overrides the Microsoft Spritebatch

However, this is for every file I must include this. It gets even more annoying if we did this for more than just SpriteBatch, or if we wanted to define some verbose type (Project.Class.ThisEnum)

Is there a way to define these names globally, once, for the whole project?

I've seen something like this before in a C++ project where they had a Precompiled header included to every file, and inside was all the renames, such as:

#define INT int //INT is now the same as int

Solution

  • There is no "global" way to do this in C#.

    That being said, depending on what you're doing, you may be able to accomplish your goals by using extension methods. This can provide the illusion of "adding methods" to the type without actually creating your own type.