に失敗し、ここに私のコードは次のとおりです。取得コンポーネント情報は、私がボタンとは、openDialogとのWinフォームを持っているのC#
[Form1.csの]:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1 obj=new Class1();
obj.get_info(this);
}
}
}
[Class1.csの]:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
class Class1
{
private IEnumerable<Component> EnumerateComponents(Form frm)
{
return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
where typeof(Component).IsAssignableFrom(field.FieldType)
let component = (Component)field.GetValue(frm)
where component != null
select component;
}
public void get_info(Form frm)
{
foreach (Component c in EnumerateComponents(frm))
{
if (c.GetType() == typeof(OpenFileDialog))
{
MessageBox.Show("Detected OpenFileDialog");
}
}
}
}
}
なぜ機能しないのですか?
私は、以下のこれらのリンクを訪問してきたが、私は私の問題を解決するためにそれらを使用しませんでした:あなたはあなたの答えを提供するために、間違った型を求めている
Access form component from another class Accessing Form's Controls from another class How to access a visual component from another form in c#
おかげ
LINQブロックを、結果を変数に割り当てる別々のステートメントに分割し、それらの変数を検査します。 – CodeCaster
@CodeCaster:例を挙げたり、回答を投稿したりできますか?私はスーパー初心者です:) –
"それは動作しません"とはどういう意味ですか?戻り値が悪いですか?例外がスローされましたか?アプリクラッシュ? – DonBoitnott