C# #define macro equivalent.
-
I wonder if there is an equivalent of C# to the #define prepropcessor directive of C++. The one of C# is not very useful since you can not define macros. Can i use something else, which would perform the same functions. This will really clean my code.
-
I wonder if there is an equivalent of C# to the #define prepropcessor directive of C++. The one of C# is not very useful since you can not define macros. Can i use something else, which would perform the same functions. This will really clean my code.
Simple answer: no, there isn't equivalant (but you can define symbols for compiler) Complex answer: Some solutions: 1) Develope own preprocessor that handles macros (shouldn't been too hard to do - but will make your code uncompatible) 2) Use #if (compiler symbols) to "select implementation". For example:
public abstract class DumpableObject : IDumpableObject { #if DEBUG private DumpContext m_contextDump; private int m_iDumpLevel = DumpContext.DEFAULT_DUMP_LEVEL; protected int DumpLevel { get { return m_iDumpLevel; } set { m_iDumpLevel = value; } } public void DumpAll() { DumpContext context = new DumpContext( this, m_iDumpLevel, DumpContext.DumpDetails.ALL ); } public void Dump( DumpContext context ) { m_contextDump = context; OnDump(); m_contextDump = null; } protected virtual void OnDump() { } protected void Dump( string strName, object obj ) { m_contextDump.Dump( strName, obj ); } #else protected int DumpLevel { get { return 0; } set { } } protected void DumpAll() { } public void Dump( DumpContext context ) { } protected virtual void OnDump() { } protected void Dump( string strName, object obj ) { } #endif }
(Yes I know - it doesn't implement all macro tricks - but at least some). Basicly you really should be able to do almost anything without macros (just use sub methods).
-
Simple answer: no, there isn't equivalant (but you can define symbols for compiler) Complex answer: Some solutions: 1) Develope own preprocessor that handles macros (shouldn't been too hard to do - but will make your code uncompatible) 2) Use #if (compiler symbols) to "select implementation". For example:
public abstract class DumpableObject : IDumpableObject { #if DEBUG private DumpContext m_contextDump; private int m_iDumpLevel = DumpContext.DEFAULT_DUMP_LEVEL; protected int DumpLevel { get { return m_iDumpLevel; } set { m_iDumpLevel = value; } } public void DumpAll() { DumpContext context = new DumpContext( this, m_iDumpLevel, DumpContext.DumpDetails.ALL ); } public void Dump( DumpContext context ) { m_contextDump = context; OnDump(); m_contextDump = null; } protected virtual void OnDump() { } protected void Dump( string strName, object obj ) { m_contextDump.Dump( strName, obj ); } #else protected int DumpLevel { get { return 0; } set { } } protected void DumpAll() { } public void Dump( DumpContext context ) { } protected virtual void OnDump() { } protected void Dump( string strName, object obj ) { } #endif }
(Yes I know - it doesn't implement all macro tricks - but at least some). Basicly you really should be able to do almost anything without macros (just use sub methods).
The problem with this method is that my code size actually Increases very rapidely. I wonder how come there is no cheap tricks. Maybe I can Use attributes for that ? But i will probably have to create a preprocessor directive myself :(( X|
-
The problem with this method is that my code size actually Increases very rapidely. I wonder how come there is no cheap tricks. Maybe I can Use attributes for that ? But i will probably have to create a preprocessor directive myself :(( X|
HAHAHA_NEXT wrote: But i will probably have to create a preprocessor directive myself I itch for that too :) I use it (#if) alot. To simulate the enviroment, eg:
#define HELLO
bool Blah(string foo)
{
#if HELLO
return true;
#elseif
return false;
#endif
}