简体   繁体   中英

Get From properties using reflection

I'm accessing form properties on this way:

System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetEntryAssembly();
Type[] Types = myAssembly.GetTypes();
foreach (Type myType in Types)
{
    if (myType.BaseType == null) continue;
    
    if (myType.BaseType == typeof(Form))
    {                    
        var emptyCtor = myType.GetConstructor(Type.EmptyTypes);
        if (emptyCtor != null)                    
        {
            var f = (Form)emptyCtor.Invoke(new object[] { });
            string FormText = f.Text;
            string FormName = f.Name;
            string btype = myType.BaseType.FullName;        

        }
    }
}

But each time when Form is accessed the Constructor is called and everything inside constructor is executed. How to avoid this?

If you want to get the names of the properties for that specific type, then you should just iterate through the types. There is a good example of that here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM