Ognacの雑感

木漏れ日々

目次

Blog 利用状況

書庫

ギャラリ

文字列によるインスタンス化ではまりました

---GACの動作の知識不足が原因だったのですが、派生的に勉強になりました。---
文字列によるインスタンス化は
    asm.CreateInstance(型名)
    Activator.CreateInstance(type);
    type.InvokeMember(Nothing,     BindingFlags.CreateInstance,....)
    AppDomain.CreateInstance
などがあります。

定石として
        With Me  'GAC.アセンブリを読み込む
            Dim a As [Assembly] = [Assembly].LoadWithPartialName("System.Drawing")
            Dim name As String = "System.Drawing.Size"
            Dim o As Object = a.CreateInstance(name)
            Dim bl As Boolean = TypeOf o Is Size
            Try
                Dim sz As Size = CType(o, Size)
                MessageBox.Show(String.Format(" {0},{1} ", sz.Width, sz.Height)) '>= 成功する
            Catch ex As Exception
                MessageBox.Show("GAC cast 失敗 : b1 = " + bl.ToString)
            End Try
        End With
があります..が LoadWithPartialName() は初回のコストが高い..そこで
直接 アセンブリ.DLLを読み込んでみましたら,コストが低くなりました. BUT!!!

        Const ipath As String = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\"
        '実際はipathは固定でなく GetCORSystemDirectory()で取得してます。
        'APIコール使用に引っかかるものがあります. API以外に取得する方法ありませんか?(ついでの質問)


        With Me 
            Dim a As [Assembly] = System.Reflection.Assembly.LoadFile(ipath + "\System.Drawing.dll")
            Dim name As String = "System.Drawing.Size"
            Dim o As Object = a.CreateInstance(name)
            Dim bl As Boolean = TypeOf o Is Size
            Try
                Dim sz As Size = CType(o, Size)
                MessageBox.Show(String.Format(" {0},{1} ", sz.Width, sz.Height))
            Catch ex As Exception
                MessageBox.Show("cast 失敗 : b1 = " + bl.ToString)           '>= これが失敗するのでハマッテしまった。
            End Try
        End With
キャストができない?????
    違いは..GAC経由か否か.
     生成されたassemblyをみてみると
    GlobalAssemblyCache: TrueかFalseの差とCodeBaseなどに差がある.
    ということは, cast動作は 厳密名を考慮してキャストチェックしているんですね.

初回のコスト抑制を考えてみました
        With Me 'LoadWithPartialNameは初回のコストが高いので改善
            Dim t1 As Type = Type.GetType("System.Drawing.Size") ' これは失敗する
            Dim t As Type = GetType(Size)  ' (assembly取得のために Size.classを明記するので少しイヤ)
            Dim a As [Assembly] = T.GetType.Assembly ' '  GetType(Size).Assembly としてもSizeを明記することになる...

            Dim name As String = "System.Drawing.Size"
            Dim o As Object = a.CreateInstance(name)
            Dim bl As Boolean = TypeOf o Is Size
            Try
                Dim sz As Size = CType(o, Size)
                MessageBox.Show(String.Format(" {0},{1} ", sz.Width, sz.Height))
            Catch ex As Exception
                MessageBox.Show("cast 失敗 : b1 = " + bl.ToString)
            End Try
        End With
コストは抑えられましたが, New()で初期設定ができないのと, assembyを引用する元のTypeの取得に getType(Size)を使っているのがすこしイヤです。

初期化付きNewを可能に改善
        With Me
            Dim t As Type = Size.GetType     'なんらかの形で Sizeの型が要る
            Dim o As Object = t.InvokeMember(Nothing, BindingFlags.CreateInstance, Nothing, Nothing, New Object() {100, 300})
            Try
                Dim sz As Size = CType(o, Size)
                MessageBox.Show(String.Format(" {0},{1} ", sz.Width, sz.Height))
            Catch ex As Exception
                MessageBox.Show(" cast 失敗 ")
            End Try

        End With

Sizeの型情報は何からの形で必要になるので, どの手法も長短がありそうです。

 


 

投稿日時 : 2006年11月29日 11:45

Feedback

# over the counter uti meds 2023/01/27 7:45 Jamessop

https://over-the-counter-drug.com/# over the counter diuretics

# drug information and news for professionals and consumers. Everything what you want to know about pills.
https://canadianfast.com/
Everything what you want to know about pills. Cautions. 2023/02/20 5:25 CanadaBest

drug information and news for professionals and consumers. Everything what you want to know about pills.
https://canadianfast.com/
Everything what you want to know about pills. Cautions.

# doxycycline tablets - https://doxycyclinesale.pro/# 2023/04/22 4:07 Doxycycline

doxycycline tablets - https://doxycyclinesale.pro/#

# buy prednisone tablets online - https://prednisonesale.pro/# 2023/04/22 15:14 Prednisone

buy prednisone tablets online - https://prednisonesale.pro/#

# cytotec buy online usa - https://cytotecsale.pro/# 2023/04/29 4:44 Cytotec

cytotec buy online usa - https://cytotecsale.pro/#

# over the counter ed medication https://overthecounter.pro/# 2023/05/08 22:49 OtcJikoliuj

over the counter ed medication https://overthecounter.pro/#

# over the counter asthma inhalers 2023/05/11 4:59 TommyAmora

https://overthecounter.pro/# what can you give a dog for pain relief over the counter?

# ed medications 2023/05/13 21:44 MichaelExcup

https://edpills.pro/# medication for ed

# buy erection pills 2023/05/14 14:45 MichaelExcup

https://pillswithoutprescription.pro/# price prescriptions

# ed pills gnc: https://edpills.pro/# 2023/05/16 3:24 EdPillsPro

ed pills gnc: https://edpills.pro/#

# purchase prednisone https://prednisonepills.pro/# - prescription prednisone cost 2023/06/05 5:22 Prednisone

purchase prednisone https://prednisonepills.pro/# - prescription prednisone cost

# sildenafil 100mg tablets buy online 2023/06/27 12:07 DavidPaync

http://sildenafilpills.pro/# buying sildenafil citrate online

# what is the best ed pill https://edpill.pro/# - best male ed pills 2023/06/27 14:40 EdPills

what is the best ed pill https://edpill.pro/# - best male ed pills

# sildenafil citrate 50mg tab 2023/06/29 9:23 DavidPaync

http://edpill.pro/# erection pills online

# ed treatments 2023/06/29 23:53 Jamesweeva

http://sildenafilpills.pro/# buy sildenafil 100mg online price

# best erectile dysfunction pills 2023/07/01 1:54 Jamesweeva

https://edpill.pro/# best male ed pills

# paxlovid buy https://paxlovid.store/
paxlovid covid 2023/07/13 21:45 Paxlovid

paxlovid buy https://paxlovid.store/
paxlovid covid

# ed pills that really work https://edpills.ink/# - over the counter erectile dysfunction pills 2023/07/27 0:55 EdPills

ed pills that really work https://edpills.ink/# - over the counter erectile dysfunction pills

# buy misoprostol over the counter https://cytotec.ink/# - buy cytotec pills 2023/07/27 1:17 PillsFree

buy misoprostol over the counter https://cytotec.ink/# - buy cytotec pills

# new treatments for ed https://edpillsotc.store/# - gnc ed pills 2023/10/08 1:15 EdPills

new treatments for ed https://edpillsotc.store/# - gnc ed pills

# valtrex prescription https://valtrex.auction/ buy valtrex australia 2023/10/24 22:12 Valtrex

valtrex prescription https://valtrex.auction/ buy valtrex australia

# buy ed pills https://edpills.tech/# new ed pills 2023/12/23 8:13 EdPills

buy ed pills https://edpills.tech/# new ed pills

タイトル
名前
Url
コメント