My Reflection

The other day I had some trouble with ASP.Net. I was saving a class I’d written into the ViewState (I did this a long time ago, I know now that that might not be the best idea) and it was giving me errors saying that the ViewState was corrupted somwhere around saving it into or loading it from the ViewState.

I had to fix this, but I still needed all the information inside the class. Even though I knew that it was not the right thing to do really and in the end proved to be seriously useless since another measure I took apparently removed the need of saving it in the ViewState altogether.

I did find a solution that worked before the other thing started to take over. I wrote 2 functions, one to save the info in the object into the ViewState and one to load it again.

using System;
using System.Reflection;

namespace My.NameSpace
{
class MyClass
{
private void savetoViewState(MyObj saveobject)
{
Type t = saveobject.GetType();
foreach (PropertyInfo info in t.GetProperties())
{
object value = info.GetValue(saveobject, null);
if (value != null && test.GetType().Namespace == "System")
ViewSate["myobj_" + info.Name] = value;
}
}

private MyObj loadfromViewState()
{
MyObj myObj = new MyObj();
Type t = myObj.GetType();

foreach (PropertyInfo info in t.GetProperties())
{
object obj = ViewState["myobj_" + info.Name];
if (obj != null)
info.SetValue(myObj, obj, null);
}

return myObj;
}
}

class MyObj
{
public string Name
{
get;
set;
}

public int Number
{
get;
set;
}
}
}

In the save function we get the type of the object we’re working with, because the type class has some very nice reflection functions, among which are GetProperties(), GetMethods(), GetContructors and many more.
We use the GetProperties method to get all the properties and we loop through them in a foreach loop. Now we can examine each property seperately.

We get the value of the property by calling the PropertyInfo.GetValue() method which requires the object from which to get the property (our argument in this case) and an object array of index values, so that you can, I’m guessing, get a or multiple items in an array. The index must be null of you’re getting non-indexed properties.

Then we check if our value is null, because if it is we don’t need to save it, and we check to see if the namespace of the property’s type is System, which in my case made sure that I was only saving properties that were of type System.String or System.Int32 etc and not Some.NameSpace.In.My.Project.

Finally if our property’s value gets through these checks, we save it in the ViewState with a prefix to identify it as part of our object.

In the load function we do something very similar. First we instantiate a new instance of our class and we again get the type of it.
Then we loop through all it’s properties, much like we did in the save function.
Now, though, we get a value from the ViewState, which should correspond to what we saved in there before. Now if there is a property that we didn’t save then getting it from the ViewState will of course result in null, so we check if the value if null. If it’s not then we call the PropertyInfo.SetValue() method which needs the instance of the object it must place the value in, then the value that it must place and again like with GetValue() an object array of indexes.

Once we’ve looped through all the properties, it should again be filled with all the relevant information and we’re good to get to work on it again.

This is my first little bit of Reflection that I’ve written to date. I know it’s not much, but since I didn’t know if MyObj was going to stay the way it was and I wanted it to work regardless. I also didn’t feel like going ViewState["myobj_name"] = myObj.Name; over and over, since the object I was working with had many more properties than this example.

In the line with if (value != null && test.GetType().Namespace == "System") I had just gotten the value of the property, if it had one at all, and then I’m checking to see if the type of that property’s namespace equals “System”, this is because in the object I was working with also had a few properties that were connected to lazy-loaded database entities, which in turn also had these, and if I wanted to save these then it would have been a serious disaster (Damn how did I ever think that writing an object like that in the ViewState would just work…).

Anyway, it’s fun to see how easy a simple reflection function can be. I’d read about it when I studied for my certificate exams and I saw some of it at the Microsoft Dev Days this year, but it always seemed complicated and strange to me. All the more reason to try it out somewhere.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s