3

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

2
  • 4
    The nested struct is not a field. It's a type. Commented Feb 17, 2014 at 11:52
  • As @Ondrej pointed out, its Type..... Commented Feb 17, 2014 at 11:56

1 Answer 1

2

You should use the GetNestedType method:

_someClass.PreLoadedAssembly
          .GetType("Some.NameSpace.ToAccess.HowTo")
          .GetNestedType("AccessMe");
Sign up to request clarification or add additional context in comments.

1 Comment

A better match might be .GetNestedType("AccessMe")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.