LoginSignup
8
8

More than 5 years have passed since last update.

入れ子になったクラスで内部クラスから外部クラスの情報を取得する

Posted at

以下の様なクラスがあった場合に

public class OuterClass
{
    public class InnerClassA
    {
    }
    public class InnerClassB
    {
    }
    public class InnerClassC
    {
    }
}

InnerClassAからOuterClassの情報を取得したい。

var innerTypeA = typeof(OuterClass.InnerClassA);
var outerType = innerTypeA.DeclaringType;

とすれば以下のように取得できる。
image

逆に外部クラスから内部クラスの情報を取得するには

var outerType = typeof(OuterClass);
var innerTypes = outerType.GetNestedTypes();

とすれば取得できる。
image

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8