いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

特定のアセンブリが参照しているアセンブリを取得する。(Assembly, System.Reflection, GetReferencedAssemblies)


以下、メモ書きです。


特定のアセンブリが参照しているアセンブリを実行時に動的に取得するには、以下のメソッドを使用します。

System.Reflection.Assembly.GetReferencedAssemblies()


戻り値は、AsssemblyNameオブジェクトの配列となります。
以下サンプルです。

// vim:set ts=4 sw=4 et ws is nowrap ft=cs:
using System;

namespace Gsf.Samples.CSharp {

    public partial class MainForm : DevExpress.XtraEditors.XtraForm {

        public MainForm() {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e) {

            _listBox.Items.Clear();
            _listBox.Items.AddRange(GetType().Assembly.GetReferencedAssemblies());
            
            if(_listBox.Items.Count != 0){
                _listBox.SelectedIndex = 0;
            }

        }
    }
}


実行すると、以下のように表示されます。