Reputation: 7419
I know that attributes
are not supported inside the method body but i wanted to know maybe someone has work around this. What i am interested in is a technique that allows you to enter custom Attribute
inside the method body and onto an two things.
Foo foo = new Foo()
)foo.bar()
)later on I plan to use the metadata
of the entered attributes
using reflection
. I came along a group of people that actually calims that they have extented c#
to do so but because of no downloadable I am unable to verify it.
Here is the link to their research paper Freely Annotating c# Please advise me the solution
UPDATE
further with reflection I already know the name of class Foo and its method bar. what the purpose of this is to know that foo called bar() in some method.
for Example
static void Main()
{
[p1inst]
ConcretePrototype1 p1 = new ConcretePrototype1("I");
[p1call]
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
Console.WriteLine("Cloned: {0}", c1.Id);
// Wait for user
Console.Read();
}
above is example that illustrate the outcome. At present it can not be done because compiler gives error. but the authors of above mentioned research paper have claimed that they have done this to allow attributes inside the method body
The whole point is to identify that in some method an object of type FOO
exists and that object has called bar()
or not
Upvotes: 0
Views: 1353
Reputation: 4180
you could add a dictionary with as keys the method names to the class...in a method by reflection you can find out the method name and that way lookup the matching value in the dictionary which would contain your attributes.
I think you don't even need to pre-add the method names in the dictionary as keys; you can put them there the first time the function runs.
Upvotes: 0
Reputation: 101052
I think you are on the wrong track. You can't have attributes inside a method. You either didn't read the paper entirely or missed some points.
It speaks of a source-to-source compiler that just translates
public void m() {
Console.WriteLine("Parallelable code sample");
[Parallel("Begin of a parallel block")] {
Console.WriteLine("Code exec by the main thread");
[Process("First process")] { /∗ Computation here ∗/ }
[Process] { /∗ Computation here ∗/ }
}
Console.WriteLine("Here is sequential");
}
into this C# code:
[Parallel("Parallelable code sample", ACSIndex=1)]
[Process("First process", ACSIndex=2)]
[Process(ACSIndex=3)]
public void m() {
Console.WriteLine("Parallelable code sample");
Annotation.Begin(1); { // [Parallel]
Console.WriteLine("Code exec by the main thread");
Annotation.Begin(2); /∗ [Process("First process")] ∗/ { · · · }
Annotation.End(2);
Annotation.Begin(3); /∗ [Process] ∗/ { · · · }
Annotation.End(3);
}
Annotation.End(1);
}
So when the method Begin(1)
is called on the Annotation
class, it will just lookup the method attribute with ASCIndex=1
.
To solve your actual problem, you should look into the StackTrace class.
Example:
void Main()
{
var f = new FooClass();
f.Foo();
}
class BarClass
{
public static void Bar()
{
var st = new StackTrace();
var frame = st.GetFrame(1);
var m = frame.GetMethod();
Console.WriteLine(String.Format("'{0}' called me from '{1}'", m.DeclaringType.Name, m));
}
}
class FooClass
{
public void Foo()
{
BarClass.Bar();
}
}
Output:
'FooClass' called me from 'Void Foo()'
Upvotes: 1