views:

165

answers:

2

I need to access some members marked internal that are declared in a third party assembly.

I would like to return a value from a particular internal property in a class. Then I'd like to retrieve a value from a property on that returned value. However, these properties return types that are also internal and declared in this third party assembly.

The examples of doing this I've seen are simple and just show returning int or bool. Can someone please give some example code that handles this more complex case?

+1  A: 

You can always retrieve it as an object and use reflection on the returned type to invoke its methods and access its properties.

tvanfosson
+3  A: 

You just keep digging on the returned value (or the PropertyType of the PropertyInfo):

u

sing System;
using System.Reflection;
public class Foo
{
    public Foo() {Bar = new Bar { Name = "abc"};}
    internal Bar Bar {get;set;}
}
public class Bar
{
    internal string Name {get;set;}
}
static class Program
{
    static void Main()
    {
        object foo = new Foo();
        PropertyInfo prop = foo.GetType().GetProperty(
            "Bar", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        object bar = prop.GetValue(foo, null);
        prop = bar.GetType().GetProperty(
            "Name", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        object name = prop.GetValue(bar, null);

        Console.WriteLine(name);
    }
}
Marc Gravell