简体   繁体   中英

Access property on dynamic form

How would one access properties on a dynamic form:

Type type = Type.GetType("GMD.Chart_Forms." + chart);
Form form = (Form)Activator.CreateInstance(type); 
form.PROPERTY????? = ...;
form.Show();

I know it's logical that I cannot access it directly becease the information (name of form) will only be available at runtime. But is there a way to access the properties on the dynamic form?

You could use dynamic :

dynamic form = Activator.CreateInstance(type);
form.Property = value;

But you might get a runtime exception if the property doesn't exist.

To avoid this you could add a check to see if the property exists:

How to detect if a property exists on an ExpandoObject?

它很乱,但你可以检查表单的子控件,直到你达到你想要的控件,然后将它CAST到你知道它的类型,然后访问该属性。

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