简体   繁体   中英

How to find out the private/public classes, properties, and methods being loaded/executed with C# during runtime

I am trying to use reflection to use a Proxy with WebView for Windows 8 Store App. The problem is, I am unable to find out if WebView has a private method or private property that is used for the default proxy.

I know android has a way of using reflection to update the WebView: WebView android proxy

However, since Android is open source, it is easier to see what methods/objects it is using in the backend.

Would there be any way to do this for the .NET Windows Store APIs? Maybe using reflection to get the classes loaded at runtime.

Visual Studio can show all the members declared in a class. Put mouse pointer above the class name in yoru code, right-click the name, and select the "Go to defeniton" command. Visual studio will create a temporary file and open it in the code editor. This file will contain only members definitions, though.

It is even possible to browse the implementation, but Visual Studio cannot do this. You can use the Reflector tool for this purpose. I do not know a freeware analog for Reflector, except for ildasm .

You can obtain all members declared in a class using code, as well. The Type class has the GetMembers method, allowing you to obtain all members of a class. Use BindingFlags to specify that you need to obtain private members:

MemberInfo[] privateMembers = typeof(MyClass).GetMembers(BindingFlags.Instance | BindingFlags.NotPublic);

Setting Binding Flags is not possible with Windows Store APIs. However I did find a workaround, you would need to create a new project: Portable Class Library, which is targeted to .NET 4+ and Windows Store APIs. Place this method in an empty class:

public void ReflectionMagic(Object obj)
    {
        BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static;

        Type webview_type = obj.GetType();

        ConstructorInfo[] constructorinfo_arr = webview_type.GetConstructors(flags);
        MemberInfo[] memberinfo_arr = webview_type.GetDefaultMembers();
        EventInfo[] eventinfo_arr = webview_type.GetEvents(flags);
        FieldInfo[] fieldinfo_arr = webview_type.GetFields(flags);
        Type[] interfaces_arr = webview_type.GetInterfaces();
        MemberInfo[] membersinfo_arr = webview_type.GetMembers(flags);
        MethodInfo[] methodinfo_arr = webview_type.GetMethods(flags);
        Type[] nestedtypes_arr = webview_type.GetNestedTypes(flags);
        PropertyInfo[] propertyinfo_arr = webview_type.GetProperties(flags);

        Type webview_interface_type = obj.GetType().GetInterfaces()[5].GetType();

        ConstructorInfo[] constructorinfo_arr2 = webview_interface_type.GetConstructors(flags);
        MemberInfo[] memberinfo_arr2 = webview_interface_type.GetDefaultMembers();
        EventInfo[] eventinfo_arr2 = webview_interface_type.GetEvents(flags);
        FieldInfo[] fieldinfo_arr2 = webview_interface_type.GetFields(flags);
        Type[] interfaces_arr2 = webview_interface_type.GetInterfaces();
        MemberInfo[] membersinfo_arr2 = webview_interface_type.GetMembers(flags);
        MethodInfo[] methodinfo_arr2 = webview_interface_type.GetMethods(flags);
        Type[] nestedtypes_arr2 = webview_interface_type.GetNestedTypes(flags);
        PropertyInfo[] propertyinfo_arr2 = webview_interface_type.GetProperties(flags);

    }

And from your original project, add a reference to this portable class library, instantiate the class and call the method: ReflectionMagic with the object you want.

You should be able to step through your Portable Class Library code by stepping into the line of code that calls ReflectionMagic. (hint: F11 - step into)

Note: Visual Studio 2012 Express is not able to create portable class libraries. I used the trial version of Visual Studio 2012 Ultimate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM