user3318908
user3318908

Reputation: 33

Get struct within a struct using reflection

I'm having problems accessing A struct within a struct

namespace Some.NameSpace.ToAccess
{
    public struct HowTo
    {
        public const string Some_Static_Strings = "redudantString";
        public const string SomeOtherStatic_Strings = "someOtherRedundantString";
        public const string Option3 = SomeOption";

        public struct AccessMe
        {
            public static readonly string OPTION1 = 1.ToString;
            public static readonly string OPTION2 = 2.ToString;
            public static readonly string OPTION3 = 4.ToString;
            public static readonly string OPTION0 = 0.ToString;

            static AccessMe()
            {
            }
        }
    }
}

I have looked at other similar questions however mine differs in that I'm also loading the assembly at runtime rather than just using reflection to get the contents of a specific struct at runtime. So to reitterate I have no reference to the library I'm itterating over prior to runtime.

this is very similar to my issue Get struct within struct using reflection, however I can't do

FieldInfo FI = typeof(HowTo).GetType().GetField("Collection", BindingFlags.Public | BindingFlags.Instance);

because I need to get the type first, however this also doesnt work

var result = _someClass.PreLoadedAssembly.GetType("Some.NameSpace.ToAccess.HowTo").GetField("AccessMe", BindingFlags.Public | BindingFlags.Instance);

(PreLoadedAssembly being the Assembly I've loaded at runtime and stored in _someClass)

Any help would be appreciated as I'm not getting very far. Thanks

Upvotes: 3

Views: 200

Answers (1)

Alberto
Alberto

Reputation: 15941

You should use the GetNestedType method:

_someClass.PreLoadedAssembly
          .GetType("Some.NameSpace.ToAccess.HowTo")
          .GetNestedType("AccessMe");

Upvotes: 2

Related Questions