Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easiest way to access hierarchy / nonpublic member

0.00/5 (No votes)
15 Oct 2012 1  
A tip to access hierarchy / nonpublic member

Introduction

In a task, we need to get the private members of the object, call private property for private or inherited base class function. In order to facilitate this type of operation, we have built a c # extension methods, so that we can be the elegant private field, properties, methods of operation of any object. Download nonpubacess.zip.

Using the code    

This function is very easy to use, you can call directly through smart tips.  Potential naming conflicts and the existing methods of the object in order not to occur, I give these methods a unified increased the AllAccess suffix. In the IDE by entering AllAccess these extension methods. You can open such a consistent way of operating object functions, fields and properties.   

The sample code is as follows:  

private void button1_Click(object sender, EventArgs e)
{
    System.Console.WriteLine(this.Text);
    //userWindowText = "Form1"
    this.SetField_AllAccess("userWindowText", "test");
    var x = this.GetField_AllAccess("userWindowText");
    System.Console.WriteLine(x);
    this.Invoke_AllAccess("Hide");//Hide th
    System.Threading.Thread.Sleep(1000);
    this.Invoke_AllAccess("Show");
    var x2 = "abc".GetProperty_AllAccess("Chars", 1);
    System.Console.WriteLine(x2 + " " + this.GetProperty_AllAccess("Text"));
}

Source code

Directly copy the following source code to your project, to all objects support a private member operation. 

public static class extAllAccess
{
    static System.Collections.Generic.Dictionary<Type, 
      System.Collections.Generic.Dictionary<string, 
      System.Reflection.MemberInfo>> dtsm = new Dictionary<Type, 
      Dictionary<string, System.Reflection.MemberInfo>>();
    static void InitType(Type t, Type tDump)
    {
        if (tDump == null)
        {
            dtsm[t] = new Dictionary<string, System.Reflection.MemberInfo>();
            tDump = t;
        }
        foreach (var mi in tDump.GetMembers(
            System.Reflection.BindingFlags.DeclaredOnly |
            System.Reflection.BindingFlags.Instance |
            System.Reflection.BindingFlags.Static |
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Public))
            dtsm[t][mi.Name] = mi;
        if (tDump.BaseType != null)
            InitType(t, tDump.BaseType);
    }
    static void CheckType(Type t)
    {
        if (t == null)
            return;
        if (!dtsm.ContainsKey(t))
        {
            lock (dtsm)
            {
                if (!dtsm.ContainsKey(t))
                    InitType(t, null);
            }
        }
    }
    public static object GetField_AllAccess(this object obj, string fieldName)
    {
        if (obj == null)
            return null;
        var t = obj.GetType();
        CheckType(t);
        return ((System.Reflection.FieldInfo)dtsm[t][fieldName]).GetValue(obj);
    }
    public static void SetField_AllAccess(this object obj, string fieldName, object value)
    {
        if (obj == null)
            return;
        var t = obj.GetType();
        CheckType(t);
        ((System.Reflection.FieldInfo)dtsm[t][fieldName]).SetValue(obj, value);
    }
    public static object Invoke_AllAccess(this object obj, string methodName, params object[] paras)
    {
        if (obj == null)
            return null;
        var t = obj.GetType();
        CheckType(t);
        return ((System.Reflection.MethodInfo)dtsm[t][methodName]).Invoke(obj, paras);
    }
    public static object GetProperty_AllAccess(this object obj, string propertyName, params object[] index)
    {
        if (obj == null)
            return null;
        var t = obj.GetType();
        CheckType(t);
        return ((System.Reflection.PropertyInfo)
          dtsm[t][propertyName]).GetValue(obj, index.Length == 0 ? null : index);
    }
    public static void SetProperty_AllAccess(this object obj, 
      string propertyName, object value, params object[] index)
    {
        if (obj == null)
            return;
        var t = obj.GetType();
        CheckType(t);
        ((System.Reflection.PropertyInfo)dtsm[t][propertyName]).SetValue(obj, 
          value, index.Length == 0 ? null : index);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here