C# Reflection: Is it possible to find an instance of an object at runtime?

前端 未结 7 1113
说谎
说谎 2020-12-07 01:53

I\'m wondering if it\'s possible to use reflection to locate an object at runtime? This is more of an experiment than a practical requirement.

I\'ve used the .GetTyp

7条回答
  •  被撕碎了的回忆
    2020-12-07 02:42

    Jon Skeet is correct that it isn't possible for just any arbitrary type.

    For your concrete example, it is possible. And you don't need to p/invoke EnumWindows or use Form.FromHandle()

    Form f = Application.OpenForms.Where(x => x.GetType().Name == "FormIWant").FirstOrDefault();
    

提交回复
热议问题