Skip Navigation Links / Posts / Post

How to get around internal and private members :-)

Categories

As I read Scott's reply to Paul's post I was thinking about the ingenuity of developers and how they will always find a way around such trivial things as access modifiers when I remembered a concrete example of it smile_wink

Fredrik Normen (http://fredrik.nsquared2.com) was one of the real pioneers with the ASP.NET Web Part stuff and I have him to thank for a lot of the stuff that I learned leading up to my book on the subject. This blog post is in no way a slight on Fredrik but instead serves to highlight his ingenuity smile_wink

One of his most popular creations was his Templated Web Part Chrome:

     http://fredrik.nsquared2.com/viewpost.aspx?PostID=248

I wanted to learn how he had managed to recreate the verbs on web parts because rendering verbs is a really tricky exercise. You have to take into account server side verbs, client side verbs, and also different browser types. As I had started to roll my own logic for custom rendering of verbs I just gave up - because it's way too hard.  Spelunking the code through Reflector told me that I was up for many hundreds of lines of code.  So I lolled over to Fredrik's site to see what he had done.  I looked at the tutorial here:

    http://fredrik.nsquared2.com/viewpost.aspx?PostID=...

And then downloaded the code project from here:

    http://www.gotdotnet.com/workspaces/workspace.aspx...

I really had to laugh - mostly because I no longer felt so silly smile_teeth when I saw how Fredrik had implemented it:

 

 

private string RenderVerbs(WebPart webPart) {
    TextWriter writer = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);

    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table);

    typeof(WebPartChrome).GetMethod(
        "RenderVerbsInTitleBar",
        System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance).Invoke(this, new object[]         { htmlWriter, webPart, 1 });

    htmlWriter.RenderEndTag();

    TextReader stringReader = new StringReader(writer.ToString());
    return stringReader.ReadToEnd();
}

posted 10/23/2006 9:38:52 PM

 

Comments:

# access modifiers? what access modifiers?
posted by lb on 10/24/2006 6:59:26 AM :

love it!!

 

Comments are currently disabled for this post.