For demonstration purpose I've created a class that contains only private methods,fields and properties
/// <summary>
/// Holds blog information
/// </summary>
public class Blog
{
/// <summary>
/// Gets the posts count.
/// </summary>
private static int PostsCount
{
get
{
return 10000;
}
}
/// <summary>
/// Gets the description.
/// </summary>
/// <returns></returns>
private string GetDescription()
{
return @"A weblog dedicated to obsessively profiling
reviewing new Internet products and companies";
}
/// <summary>
/// Changes the name of the blog.
/// </summary>
/// <param name="newName">The new name.</param>
private string ChangeBlogName(string newName)
{
name = newName;
return name;
}
string url = "http://techcrunch.com";
string name = "Tech Crunch";
}
To access private fields I used the System.Reflection.FieldInfo class .For private properties I used System.Reflection.PropertyInfo .Note if you want to work with private members you should use System.Reflection.BindingFlags enum as shown in the example below .So for non-static class members the flags should looks like BindingFlags.NonPublic | BindingFlags.Instance .For static don't forget to add BindingFlags.Static .
class Program
{
static void Main(string[] args)
{
Blog blog = new Blog();
Type type = typeof(Blog);
//create reflection bindings - will be used to retrive private fields,methods or properties
BindingFlags privateBindings = BindingFlags.NonPublic | BindingFlags.Instance;
// retrive private fields from our class
FieldInfo[] fieldInfos = type.GetFields(privateBindings);
// retrive private fields metadata
foreach (FieldInfo fieldInfo in fieldInfos)
{
Console.WriteLine(fieldInfo.Name +" "+ fieldInfo.GetValue(blog));
}
PropertyInfo [] propertyInfos = type.GetProperties(privateBindings|BindingFlags.Static);
// retrive private static properties metadata
foreach (PropertyInfo propertyInfo in propertyInfos)
{
// note that no instance need for static property to retrive it's value
Console.WriteLine(propertyInfo.Name + " " + propertyInfo.GetValue(null,null) );
}
// call method using MethodInfo object
MethodInfo miGetDescription = type.GetMethod("GetDescription", privateBindings);
object retObj = miGetDescription.Invoke(blog, new object[]{});
Console.WriteLine(retObj);
// call method using MethodInfo object with input parameters
MethodInfo miChangeBlogName = type.GetMethod("ChangeBlogName", privateBindings);
retObj = miChangeBlogName.Invoke(blog, new object[] { "Yahoo blog" });
Console.WriteLine(retObj);
Console.ReadLine();
}
The console output fallows
1 comment:
This comes with a warning:
Reflecting private or protected members will only work in a full trust environment.
This is particularly an issue in a shared hosting environment, where medium trust is the norm.
Too many times I've run into libraries that just won't work once I get them onto the webserver, for this very reason.
Remember that we have the ability to declare private and protected variables for a *reason*. By using reflection in this way you break the intended access model of the original programmer, so this is rarely a good thing to do.
A situation where you might want this is if, for instance, you're writing a 3rd-party tool for analysing assemblies. "Reflector" is a good example of a situation where this is done effectively.
If you're doing this simply as a helper or shortcut to workaround an existing object model which is lacking the interface you require, then you should question what you're doing and consider finding another way around.
Post a Comment