Are Macros possible in C# ?

  • Thread starter Jackson Houndmugger
  • Start date
J

Jackson Houndmugger

Hi,

Let's say that I need to design 50 methods, each one of which look like this:

try{
if(Shutdown.Instance.IsShuttingDown){
return;
}
DisposeLock.AcquireReaderLock(Timeout.Infinite);
if(IsDisposed){
throw new AppShutdownException();
}

/* Custom Code Here !*/
}
catch(AppShutdownException){}
finally{
DisposeLock.ReleaseReaderLock();
}

So basically, each method looks like the one above. The only thing that differentiates one method from another is the custom code which will be placed in the area which I've designated with the comment block.

I'm wondering if C# supports some sort of "macro" that would allow me to substitute an abbreviation representing the repetative wrapper code that is placed before and after the custom code. MFC allowed this. C# does something similar via it's "using" directive which encapsulates try/finally/dispose logic, but I'm not certain if C# allows developers to create their own macros.

Any ideas?

Thanks,

Jackson
 
N

Nicholas Paldino [.NET/C# MVP]

Jackson,

No, it is not possible to create macros. However, for what you want to
do, you can create your shell, and then pass a delegate to the method to be
called in the custom portion of the code.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi,

Let's say that I need to design 50 methods, each one of which look like
this:

try{
if(Shutdown.Instance.IsShuttingDown){
return;
}
DisposeLock.AcquireReaderLock(Timeout.Infinite);
if(IsDisposed){
throw new AppShutdownException();
}

/* Custom Code Here !*/
}
catch(AppShutdownException){}
finally{
DisposeLock.ReleaseReaderLock();
}

So basically, each method looks like the one above. The only thing that
differentiates one method from another is the custom code which will be
placed in the area which I've designated with the comment block.

I'm wondering if C# supports some sort of "macro" that would allow me to
substitute an abbreviation representing the repetative wrapper code that is
placed before and after the custom code. MFC allowed this. C# does something
similar via it's "using" directive which encapsulates try/finally/dispose
logic, but I'm not certain if C# allows developers to create their own
macros.

Any ideas?

Thanks,

Jackson
 
M

Mythran

Create your "shell", select what you want to duplicate, and then drag-n-drop
onto the ToolBox window under the section you want. Then you can
drag-n-drop into place wherever you want it.

Does this help?

Mythran
 
M

Marcos Stefanakopolus

One method that takes a delegate would definitely be the easiest way to go.
Also provides you with more run-time flexibility. If that doesn't work for
you for some reason, you could use the Reflection.Emit classes to generate
these methods dynamically and emit a .DLL that contains them or something.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top