469,568 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Is there something like a "CurrentMethod" property on a class object ?

Hi,

I'm currently a method attribute which is used to check the "validity" of this method against a rule.
I wrote the isValid method, to be used inside the otriginal method:

For instance:
// Following is Csharp code

[ruleAttribute ("rule1")]
private void functionT12 ()
{
if (v.IsValid ()) {
...
}
}

isValid should be called with an method instance, taht is to say that "v" must reference the "functionT12" object.

So question is: how can I create a object reference on the current method ?
Is there something like a "CurrentMethod" property on a class object ?

Oriane
Nov 17 '05 #1
9 1281
Hi,

I am not sure what the IsValid method does and therefore what information on
the current method it needs.

You can possibly use the System.Diagnostics.StackFrame class and optionally
the System.Diagnostics.StackTrace class.
You can also probably use the System.Reflection.MethodInfo class.

Can you please describe the validity check you are trying to perform on a
method?
Also, consider Aspect-Oriented Programming techniques, they might be just
what you need for the validation.
In addition, consider custom Code Access Security permissions (on second
thought - hmm, not sure you can create those, please check with MSDN).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Oriane" <or****@guermantes.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
Hi,

I'm currently a method attribute which is used to check the "validity" of
this method against a rule.
I wrote the isValid method, to be used inside the otriginal method:

For instance:
// Following is Csharp code

[ruleAttribute ("rule1")]
private void functionT12 ()
{
if (v.IsValid ()) {
...
}
}

isValid should be called with an method instance, taht is to say that "v"
must reference the "functionT12" object.

So question is: how can I create a object reference on the current method ?
Is there something like a "CurrentMethod" property on a class object ?

Oriane
Nov 17 '05 #2
Hi Dmytro,
isValid is a method on my own using reflection and the "RuleAttribute" custom attribute.
I'm not searching for a CAS or security check, but just the "current" method.

I will try to find something in the System.Reflection.MethodInfo class.

Thanks

Oriane

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> a écrit dans le message de news: On**************@tk2msftngp13.phx.gbl...
Hi,

I am not sure what the IsValid method does and therefore what information on
the current method it needs.

You can possibly use the System.Diagnostics.StackFrame class and optionally
the System.Diagnostics.StackTrace class.
You can also probably use the System.Reflection.MethodInfo class.

Can you please describe the validity check you are trying to perform on a
method?
Also, consider Aspect-Oriented Programming techniques, they might be just
what you need for the validation.
In addition, consider custom Code Access Security permissions (on second
thought - hmm, not sure you can create those, please check with MSDN).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


"Oriane" <or****@guermantes.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
Hi,

I'm currently a method attribute which is used to check the "validity" of
this method against a rule.
I wrote the isValid method, to be used inside the otriginal method:

For instance:
// Following is Csharp code

[ruleAttribute ("rule1")]
private void functionT12 ()
{
if (v.IsValid ()) {
...
}
}

isValid should be called with an method instance, taht is to say that "v"
must reference the "functionT12" object.

So question is: how can I create a object reference on the current method ?
Is there something like a "CurrentMethod" property on a class object ?

Oriane

Nov 17 '05 #3
In this case, in the IsValid method, you can capture the stack trace with
the System.Diagnostics.StackTrace class, get the info on the immediate
caller and then utilize Reflection (in particular, the MethodInfo class) to
discover the attributes appplied to the calling method.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Oriane" <or****@guermantes.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Dmytro,
isValid is a method on my own using reflection and the "RuleAttribute"
custom attribute.
I'm not searching for a CAS or security check, but just the "current"
method.

I will try to find something in the System.Reflection.MethodInfo class.

Thanks

Oriane

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> a écrit dans le
message de news: On**************@tk2msftngp13.phx.gbl...
Hi,

I am not sure what the IsValid method does and therefore what information
on
the current method it needs.

You can possibly use the System.Diagnostics.StackFrame class and
optionally
the System.Diagnostics.StackTrace class.
You can also probably use the System.Reflection.MethodInfo class.

Can you please describe the validity check you are trying to perform on a
method?
Also, consider Aspect-Oriented Programming techniques, they might be just
what you need for the validation.
In addition, consider custom Code Access Security permissions (on second
thought - hmm, not sure you can create those, please check with MSDN).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Oriane" <or****@guermantes.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
Hi,

I'm currently a method attribute which is used to check the "validity" of
this method against a rule.
I wrote the isValid method, to be used inside the otriginal method:

For instance:
// Following is Csharp code

[ruleAttribute ("rule1")]
private void functionT12 ()
{
if (v.IsValid ()) {
...
}
}

isValid should be called with an method instance, taht is to say that "v"
must reference the "functionT12" object.

So question is: how can I create a object reference on the current method
?
Is there something like a "CurrentMethod" property on a class object ?

Oriane


Nov 17 '05 #4
using System.Reflection;
....
MethodBase methodBase = MethodBase.GetCurrentMethod();

Gabriel Lozano-Morán
Nov 17 '05 #5
Thank you Dmytro,

now you can perhaps ask another question: is there a way to avoid the calling of the "isValid" in the method?
I mean, to "trigger" the call to "isValid" automatically when entering in the method code ?

I think this is not possible, but it could have made my code prettier !!

What I would try to do is to emulate the [PrincipalPermissionAttribute ] behaviour...

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> a écrit dans le message de news: O$*************@TK2MSFTNGP12.phx.gbl...
In this case, in the IsValid method, you can capture the stack trace with
the System.Diagnostics.StackTrace class, get the info on the immediate
caller and then utilize Reflection (in particular, the MethodInfo class) to
discover the attributes appplied to the calling method.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


"Oriane" <or****@guermantes.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Dmytro,


isValid is a method on my own using reflection and the "RuleAttribute"
custom attribute.
I'm not searching for a CAS or security check, but just the "current"
method.

I will try to find something in the System.Reflection.MethodInfo class.

Thanks

Oriane

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> a écrit dans le
message de news: On**************@tk2msftngp13.phx.gbl...
Hi,

I am not sure what the IsValid method does and therefore what information
on
the current method it needs.

You can possibly use the System.Diagnostics.StackFrame class and
optionally
the System.Diagnostics.StackTrace class.
You can also probably use the System.Reflection.MethodInfo class.

Can you please describe the validity check you are trying to perform on a
method?
Also, consider Aspect-Oriented Programming techniques, they might be just
what you need for the validation.
In addition, consider custom Code Access Security permissions (on second
thought - hmm, not sure you can create those, please check with MSDN).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Oriane" <or****@guermantes.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
Hi,

I'm currently a method attribute which is used to check the "validity" of
this method against a rule.
I wrote the isValid method, to be used inside the otriginal method:

For instance:
// Following is Csharp code

[ruleAttribute ("rule1")]
private void functionT12 ()
{
if (v.IsValid ()) {
...
}
}

isValid should be called with an method instance, taht is to say that "v"
must reference the "functionT12" object.

So question is: how can I create a object reference on the current method
?
Is there something like a "CurrentMethod" property on a class object ?

Oriane


Nov 17 '05 #6
OK.

Thanks a lot
"LOZANO-MORÁN, Gabriel" <ga************@no-spam.com> a écrit dans le message de news: ej**************@TK2MSFTNGP12.phx.gbl...
using System.Reflection;
...
MethodBase methodBase = MethodBase.GetCurrentMethod();

Gabriel Lozano-Morán

Nov 17 '05 #7
Oriane wrote:
Thank you Dmytro,

now you can perhaps ask another question: is there a way to avoid the
calling of the "isValid" in the method?
I mean, to "trigger" the call to "isValid" automatically when
entering in the method code ?

I think this is not possible, but it could have made my code prettier
!!

What I would try to do is to emulate the
[PrincipalPermissionAttribute ] behaviour...


Yes, and it would eliminate the need to do the "try to find the current
method" dance you were asking about.

Look at this article:

http://msdn.microsoft.com/msdnmag/is...contextsinnet/

It's tricky to do this, but I have done it for reasons similar to yours.

There's a few other articles, besided that one, that talk about this topic
on the Net. You might want to look for them too.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Nov 17 '05 #8
Still, read Reginald's post carefully. I believe what this poster suggests
is just another name for Aspect-Oriented Programming I suggested earlier in
this thread. And this would be the best way to implement your validity
check.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Oriane" <or****@guermantes.com> wrote in message
news:OK**************@TK2MSFTNGP09.phx.gbl...
OK.

Thanks a lot
"LOZANO-MORÁN, Gabriel" <ga************@no-spam.com> a écrit dans le message
de news: ej**************@TK2MSFTNGP12.phx.gbl...
using System.Reflection;
...
MethodBase methodBase = MethodBase.GetCurrentMethod();

Gabriel Lozano-Morán


Nov 17 '05 #9
"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Still, read Reginald's post carefully. I believe what this poster suggests
is just another name for Aspect-Oriented Programming I suggested earlier in this thread. And this would be the best way to implement your validity
check.


Ok. I will follow your advice.

Sincerely
Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.