.NET 中的类型字段与嵌套类型

Type fields versus nested types in .NET

对于类型,有命令 GetFields() 和命令 GetNestedTypes()。 GetFields() 不 return 嵌套类型。 我对字段和嵌套类型之间的区别感到困惑。 示例将非常有帮助! 谢谢

A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type. (https://msdn.microsoft.com/en-us/library/ms173118.aspx)

A type defined within a class or struct is called a nested type. (https://msdn.microsoft.com/en-us/library/ms173120.aspx)

例如,在 class

class Foo
{
    private int a;

    public class Bar
    {
        // ...
    }
}

a 是字段,Bar 是嵌套类型。

class Foo {
    private String _aField;

    private class ANestedClass {
        private String _aFieldInANestedClass;
    }
}

像这样:

public void Blargh() {
    Foo foo = new Foo();
    FieldInfo aField = foo.GetType().GetField("_aField");
    String aFieldValue = aField.GetValue( foo );

    Type[] nestedTypes = foo.GetType().GetNestedTypes();
    Type aNestedClass = nestedTypes.Single( t => t.Name == "ANestedClass" );
}