Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attributes, method calls, and the detection thereof... 2

Status
Not open for further replies.

mmilan

Programmer
Jan 22, 2002
839
GB
Hi All,

I would like to apply an Attribute to particular calls to a particular method I've written, and then, inside the called method, detect whether or not the Attrubute was applied.

In terms of the background, I'm working on a library that publicly exposes a couple of functions A and B, but B is also called from A itself. I need to detect, in B, if the method was invoked from A or elsewhere.

I don't want to do this using optional parameters etc because this would be exposed in the public interface to my class.

Erm, help anyone? Just a quick pointer in the right direction would be a big help...

mmilan


 
Look at this article

It is in C# but that shouldn't be a problem ;-).

It catches the method looks for the attribute and changes the method. it can also see who called it by looking at the invocation. Take a look at The AOPAttributebase class.



Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
I can read C# as well - indeed I prefer to, but my employer insists on VB...

mmilan
 
Oops


Actually that was on purpose just to see if you were paying attention ;-).

Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
BeginInvoke? That's all greek as well :eek:(

I will bottom this one though - I'm determined...

I've considered using StackFrame to get at the methodinfo of the current method, and then using Attribute.GetCustomAttribute - but I don't want to use StackFrame either because it's really not designed for application use - rather MS's own stuff. I wouldn't want them thinking that meant they could just change it on a whim and screw up my code...

Thanks for your help so far though...

mmilan
 
I think I misunderstood you. And I actually think you just want the calling method name the only way to go there is to use stackframe like this

Code:
 StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
// Displays “WhatsmyName”
Console.WriteLine( " Parent Method Name {0} ", methodBase.Name );

Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Two options, use Chrissie's method of snagging the stack. Or put the code you actually want to execute into a private function with an extra parameter indicating what method called it. Keep your public method the same, but have it call the new private method with the extra parameter set to "external" or what ever. From Function B call the new private method with the extra parameter set to "B" or what ever.

Using the stack trace would be a more elligant, but using the private method is probably 'simpler'. Personally, I would go with the Stack.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Simpler? Really? Example please, my brain isn't getting it. To much exit points.

Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Code:
Public Function A(Parm1 as object, Parm2 as object) as object
  return MyInternalFunction(Parm1, Parm2, "A")
end function

Public Function B(Parm1 as object, Parm2 as object, Parm3 as object) as object
   ...
   x = MyInternalFunction(Parm1, Parm2, "B")
   ...
end function

Private Function MyInternalFunction(Parm1 as object, Parm2 as object, Sender as string) as object)
  ...
end function

Simpler in the meaning that it requires no knowledge to comprehend beyond basic OO design fundamentals. This would be the solution I would expect from a recent college grad with limited work experience. Chrissie's solution is what I would expect from someone with more hands on experience with knowledge of handy tools and functionality built into the framework.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
ThatGuyRick - I eventualy went down your line, but using a combination of public and friend...

Thanks to both of you though - very interesting.

We're about to embark on a massive vb6->DotNet project, so I'm all for getting into Attributes (which won't do what I tried to here by the way!), Delegates, Predicates et al...

Regular Expressions! At last! (I sometimes write in PHP, so I'd encountered them before...)

mmilan
 
Uhm. Regular exp..., I have no idea what you are talking about :).

Christiaan Baes
Belgium

"In a system where you can define a factor as part of a third factor, you need another layer to check the main layer in case the second layer is not the base unit." - jrbarnett
 
Regular expressions are basically a means of doing searching and replacing on strings at the speed of light squared... They're also very, VERY flexible. Have a read of it in Dotnet (System.Text.RegularExpressions namespace).

It's sort of like pattern matching on steroids!

mmilan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top