开发者

Issue With Casting MdiChildren

开发者 https://www.devze.com 2023-03-22 22:57 出处:网络
I am having a c开发者_Go百科asting issue with MdiChildren. This works: MyForm mf = (MyForm)this.ActiveMdiChild;

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>();
0

精彩评论

暂无评论...
验证码 换一张
取 消