473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Are Macros possible in C# ?

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
Nov 16 '05 #1
4 1210
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]
- mv*@spam.guard.caspershouse.com
"Jackson Houndmugger" <la*****@booooon.com> wrote in message
news:G9********************@giganews.com...
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
Nov 16 '05 #2
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
Nov 16 '05 #3
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.

--
I may not always have the best answers, but at least I'm not posting
questions in the wrong newsgroups...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2*****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"Jackson Houndmugger" <la*****@booooon.com> wrote in message
news:G9********************@giganews.com...
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

Nov 16 '05 #4
Great tip. I've never done that before. Works like a charm.

--
....Carl Frisk
Anger is a brief madness.
- Horace, 20 B.C.
http://www.carlfrisk.com
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message news:OP**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
25
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include...
8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
11
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm...
12
by: David Powell | last post by:
Because my work area won't have an Access programmer next year, I've been asked to rebuild their coded application as a set of modular tools. The idea is that a non-programmer will be able to...
2
by: Andrew Arro | last post by:
is it possible to make smth like a loop of macroses? i.e. i want some macros to be called X times, all that on the PREPROCESSOR lever i was trying smth like #define vv_0 100 #define vv_1...
35
by: Durgesh Sharma | last post by:
I want to use strrchar(source_string,last_char ) function from string.h header file,to find out the last occurrence of the NON SPACE Alphanumeric Character. Then i will put a NULL CHAR after...
13
by: Anthony de Almeida Lopes | last post by:
Hello, I am wondering why it is not possible to have a function-like macro like the following: #define __nothread(name) do { \ #ifdef _PTHREAD_H ...
1
by: avasilev | last post by:
Hi all, I am upgrading a kind of small logging framework. Until now, the interface for logging messages takes only a format string, and a variable argument list (in a similiar way as printf())....
2
by: Arto Viitanen | last post by:
Would it be possible to use C# 3.0 runtime macros to do following: If Apache's log4net is loaded, calls like log.DebugFormat("Hello {0}","world"); generate the logging, but if the dll is not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.