Saturday, February 2, 2008

Reflection and Medium Trust

Up to today I thought if you host a website in a Medium Trust environment for .NET, reflection is basically turned off or not allowed. It was rather frustrating to not be able to use some of the nice abilities that reflection provides because the website is hosted in a Medium Trust environment. However, reflection IS still allowed, but whatever you are looking for has to be marked as public. For example, I had a class that looked something like the following:

internal class MyClass
{
private bool _someProperty;

public bool SomeProperty
{
get { return _someProperty; }
set { _someProperty = value; }
}
}

Because the class is marked "internal", it is not accessible using reflection in a Medium Trust environment. Just by changing "internal" to "public", I was able to query the classes properties using reflection:

public class MyClass
{
...
}

I hope someone else finds this useful.

No comments: