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

O

Oriane

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
 
D

Dmytro Lapshyn [MVP]

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]


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
 
O

Oriane

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
 
D

Dmytro Lapshyn [MVP]

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]


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
 
L

LOZANO-MORÁN, Gabriel

using System.Reflection;
....
MethodBase methodBase = MethodBase.GetCurrentMethod();

Gabriel Lozano-Morán
 
O

Oriane

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 said:
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]


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 said:
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]


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
 
R

Reginald Blue

Oriane said:
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/issues/03/03/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]
 
D

Dmytro Lapshyn [MVP]

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]


OK.

Thanks a lot
 
O

Oriane

Dmytro Lapshyn said:
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
 

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