Re: C# Reflection--anybody use it lately?



Example...

public class SomeClass
{
[Log]
public void DoSomething()
{
...
}

[Log]
public void DoSomethingElse()
{
...
}

public void DoSomethingElseButDontLog()
{
...
}
}

In this example when you compile your project PostSharp will use a post-compile hook in the IDE to reflect over the classes/members etc. It sees the [Log] attribute which you yourself have written and changes the code from this

{
...
}

to this

{
System.Diagnostics.Debug.WriteLine("Entering method DoSomething");
try
{
...
}
finally
{
System.Diagnostics.Debug.WriteLine("Leaving method DoSomething");
}
}


You can write what code you like in your PostSharp attribute ("Log" in this case), the attribute may then be applied to assemblies, classes, or individual members. This is one good example of how .NET attributes are used in combination with Reflection (post-compile) to alter the code you have written. This AOP approach only uses reflection during compile time and not runtime, additionally I will say that if you think AOP is only related to security then either that URL is no good or you misunderstood it.


In short, much ado about nothing. I'll cross it off my list of things
to study.

Tut tut. With this kind of attitude towards learning there's no wonder why you don't understand the benefits of such a simple thing such as Reflection.



Pete


.



Relevant Pages

  • Re: C# typeof() and Generics
    ... DoSomething, so no point to make the initial call generic. ... If you're already using reflection, boxing should be the least of your performance worries. ... public void StartHere ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: finalize()
    ... public void doSomething() throws Exception { ... File file = null; ... You failed to catch the Exception that doSomethingmight throw. ...
    (comp.lang.java.programmer)
  • Re: extending class that is not in a packed
    ... Now I am using reflect to extend this class ... >> public void DoSomething(){ ... >> and class Class2 that is ... >> public void DoSomething2(){ ...
    (comp.lang.java)
  • Re: finalize()
    ... abstract class DoSomething { ... public void doSomething() throws Exception { ...
    (comp.lang.java.programmer)
  • Re: runtime stack
    ... The stack would be relevant if you were concerned about the number of variables you are using within a method. ... public void FirstTry(string varone){if ... dosomething();} ...
    (microsoft.public.dotnet.languages.csharp)