Issue With Casting MdiChildren
I am having a c开发者_Go百科asting issue with MdiChildren
.
This works:
MyForm mf = (MyForm)this.ActiveMdiChild;
this does not work:
MyForm[] mfs = (MyForm[])this.MdiChildren;
All forms in the parent form are instances of MyForm
, But I cannot successfully cast them. This means that I cannot use any of the methods that MyForm
provides because they are only Form objects. What can I do to rectify this issue?
The MdiChildren property returns an array of Form
instances, which is not the same thing as (and is not directly convertible to) an array of MyForm
instances.
You can use LINQ's Cast() method to perform an explicit conversion:
MyForm[] mfs = this.MdiChildren.Cast<MyForm>().ToArray();
Here is how you can do it;
IEnumerable<MyForm> mfs = this.MdiChildren.Cast<MyForm>();
精彩评论