プログラミング

[C#]任意の型のインスタンスを作成

Mohmongar
c#で任意の型TがType型の変数tで与えられている場合のインスタンスを作る場合の方法。またはその配列のインスタンスを作る方法のメモ // 任意型tのインスタンスの作成 Type t = typeof(***); var tInst = Activator.CreateInstace(t); // 型tの配列t[]のインスタンスの作成 Type t = typeof(***); Type ta = Type.GetType(t.toString()+"[]"); Int num; var tInst = Activator.CreateInstace(ta, num); // または Type t; Int num; var tInst = Array.CreateInstace(t, num); // 追加 var tInst = Activator.CreateInstace(t.MakeArrayType(1), num);

[WPF]Drawing.BitmapをBitmapSourceへ変換

Mohmongar
昔のDLLがBitmapしか対応してないので、System.DrawingのBitmapをWPFのBitmapSourceへ変換する。 [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); public BitmapSource BmpToWPFBmp(System.Drawing.Bitmap bitmap) { IntPtr hBitmap = bitmap.GetHbitmap(); BitmapSource source; source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DeleteObject(hBitmap); return source; }