How to access the inner class using Reflection

How to access the inner class of the assembly? Say I want to access System.ComponentModel.Design.DesignerHost. Here, DesignerHost is an inner and private class.

How to write code to load assembly and type?

+50
reflection c # class internal
Aug 11 '09 at 9:11
source share
2 answers

In general, you should not do this - if the type has been marked as internal, it means that you are not going to use it outside the assembly. It can be deleted, changed, etc. In a later version.

However, reflection allows you to access types and members that are not publicly accessible - just find the overloads that accept the BindingFlags argument and include BindingFlags.NonPublic in the flags you pass.

If you have a full type name (including assembly information), then you only need to work with a call to Type.GetType(string) . If you know the assembly in advance and know the public type inside this assembly, then using typeof(TheOtherType).Assembly to get a reference to the assembly is usually simpler, you can call Assembly.GetType(string) .

+75
Aug 11 '09 at 9:14
source share

To load the assembly and enter it in your example:

 Assembly design = Assembly.LoadFile(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Design.dll"); Type designHost = design.GetType("System.ComponentModel.Design.DesignerHost"); 
+9
Aug 11 '09 at 9:24
source share



All Articles