MethodBase Without Reflection

One of my friends early this morning (Too early if you ask me. It takes me a lil bit to get my engines going) asked me about getting presently executing method information. Well, that is pretty easy, right? We have System.Reflection that allows us to look into the current codebase for information like that, like getting the methods name as a string.
We just simply do a:
[csharp]
System.Reflection.MethodBase.GetCurrentMethod()
[/csharp]

Bam, we have the current method name. But, he wanted to do this sans Reflection.
Well, we have to make that one line of code a little bit longer taking into account that constraint, but it really isn’t that complex. We know that for each method that is executed in an arbitrary thread we get one of those StackFrame objects that is pushed onto the base dynamic stack data structure (i.e. We push a new StackFrame object onto the callstack). Those StackFrame objects contain all sorts of fun information, and that information can be extracted and used for whatever scheming purpose you have in mind. But remember, a lot of method information is contained in the debug file, so when you switch to release builds you might elude some data that you are interrogating for.
So firstly, let’s produce a new StackFrame object:

[csharp]
StackFrame stackFrame = new StackFrame();
[/csharp]

Now we have a StackFrame object, and we know that the StackFrame contains all sorts of MethodBase information that can be employed to garnish information from.
So, let’s do some MethodBase hydrating:
[csharp]

StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
[/csharp]

Now that you got those to ready to go, you can start getting all the fun information!
For example:

[csharp]
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
string methodName = methodBase.Name;
bool isStaticMethod = methodBase.IsStatic;
bool isPublicMethod = methodBase.IsPublic;
[/csharp]

Ready to go! :)

Share

ToolPart Class With System.Reflection

If you are still working with some of the SharePoint base classes, you probably are familiar with the ToolPart class. The ToolPart class allows you optionally expose a bunch of neat little custom pieces you decide on regarding your WebPart in a natively expandable menu within the WebPart ToolPane. This is nice when doing something like displaying the copyright or whatever of your WebPart. The basic code for a ToolPart class looks like this:

[csharp]

using Microsoft.SharePoint.WebPartPages;
using System;
using System.Web.UI;

namespace buenz.Webparts
{
public class BasicToolpart : ToolPart
{
public BasicToolpartMS()
{
base.Title = “My Name”;
}

public override void ApplyChanges()
{
}

public override void CancelChanges()
{
}

protected override void RenderToolPart(HtmlTextWriter output)
{
output.Write(“Adam Buenz”);
}

public override void SyncChanges()
{
}
}
}

[/csharp]
But, when you are displaying anything like version of WebParts etc. it is often a little bit eaiser to use .NET Reflection as opposed to having to go through a bunch of string values in the WebPart class file to display the version of the WebPart. Instead you can just pull it from the assignments made in the assembly info file using the System.Reflection namespace. To take advantage of this, there are just a couple things that you have to do. The first is to make a reference to System.Reflection and then initialize three strings to hold the data that is being pulled from the executing assembly, in this case the WebPart version, the Company name that built the webpart, and of course, the WebPart product name. The other is to get the information from the WebPart assembly file using GetExecutingAssembly() to get the location to the WebPart binary, and then get the relevant information.
[csharp]

using Microsoft.SharePoint.WebPartPages;
using System;
using System.Web.UI;
using System.Reflection;

namespace buenz.WebParts
{
public class ReflectingToolPart : ToolPart
{
private string labelVersion;
private string labelCompanyName;
private string labelProductName;

public ReflectingToolPart()
{
this.labelProductName = AssemblyProduct;
this.labelCompanyName = AssemblyCompany;
this.labelVersion = String.Format(“Version {0}”, AssemblyVersion);
base.Title = “My WebPart”;
}

public override void ApplyChanges()
{
}

public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}

public string AssemblyProduct
{
get
{
// Get all Product attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
// If there aren’t any Product attributes, return an empty string
if (attributes.Length == 0)
return “”;
// If there is a Product attribute, return its value
return ((AssemblyProductAttribute)attributes[0]).Product;
}
}

public string AssemblyCompany
{
get
{
// Get all Company attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
// If there aren’t any Company attributes, return an empty string
if (attributes.Length == 0)
return “”;
// If there is a Company attribute, return its value
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}

public override void CancelChanges()
{
}

protected override void RenderToolPart(HtmlTextWriter output)
{
output.Write(”

“);
output.Write(”
“);
output.Write(labelProductName);
output.Write(“”);
output.Write(labelVersion);
output.Write(“”);
output.Write(labelCompanyName);
}public override void SyncChanges()
{
}
}
}

[/csharp]

Just make sure that the attributes that you are pulling from the WebPart don’t contain null values.

Share