12 Interface Public Interface ICalc Add In Function
コードで書いてみた(1/2) • Interface Public Interface ICalc. Add. In Function Calc(By. Val x As Integer, _ By. Val y As Integer) As Integer End Interface • Add-in Public Class Adder Implements Wankuma. ICalc. Add. In Public Function Calc(By. Val x As Integer, _ By. Val y As Integer) As Integer _ Implements ICalc. Add. In. Calc Return x + y End Function End Class わんくま同盟 大阪勉強会 #23
コードで書いてみた(2/2) Host application Imports System. IO Imports System. Reflection Module Main. Module Sub Main() Dim files = Directory. Get. Files( _ Path. Get. Directory. Name(Assembly. Get. Executing. Assembly. Location), "*. dll") For Each file In files For Each t In Assembly. Load. From(file). Get. Types If t. Is. Class And. Also _ t. Is. Public And. Also _ Not t. Is. Abstract And. Also _ t. Get. Interface("Wankuma. ICalc. Add. In") Is. Nothing Then Dim addin. Assembly = Assembly. Load. From(file) Dim addin = Direct. Cast(addin. Assembly. Create. Instance(t. Full. Name), _ Wankuma. ICalc. Add. In) Console. Write. Line(t. Full. Name) ' アドイン呼び出し Console. Write. Line("Calc({0}, {1}) = {2}", 160, 2, addin. Calc(160, 2)) End If Next Console. Read. Line() End Sub End Module わんくま同盟 大阪勉強会 #23
セグメントの関係 Host View Host-side Adapter 参照 実装 実装 Interface/抽象Class ※ 7個のアセンブリ Contract 分離境界 Add-in side Adapter (矢先の)コンストラクタへ渡される Add-in View Add-in 実装 Interface/抽象Class • HostとAdd-inは別App. Domain • Contractは両Domainに読み込まれる わんくま同盟 大阪勉強会 #23
実装(Contract) Imports System. Add. In. Contract Imports System. Add. In. Pipeline <Add. In. Contract()> _ Public Interface ICalc. Add. In. Contract Inherits IContract Function Calc(By. Val x As Integer, By. Val y As Integer) As Integer End Interface • シリアル化可能な型(基本的なものに限 る)とコントラクト、列挙値のみ受け渡 し可能 わんくま同盟 大阪勉強会 #23
実装(View) Imports System. Add. In. Pipeline <Add. In. Base()> _ Public Interface ICalc. Add. In Function Calc(By. Val x As Integer, By. Val y As Integer) As Integer End Interface • Add-in側はAdd. In. Base属性 • 基本的にはHost/Add-inとも同じ わんくま同盟 大阪勉強会 #23
実装(Add-in Side Adapter) Imports System. Add. In. Pipeline <Add. In. Adapter()> _ Public Class View. To. Contract. Add. In. Side. Adapter Inherits Contract. Base Implements ICalc. Add. In. Contract Private _view As Wankuma. ICalc. Add. In ' Add. In View Public Sub New(By. Val view As Wankuma. ICalc. Add. In) My. Base. New() _view = view End Sub Public Function Calc(By. Val x As Integer, By. Val y As Integer) As Integer Implements ICalc. Add. In. Contract. Calc Return _view. Calc(x, y) End Function End Class わんくま同盟 大阪勉強会 #23
実装(Host Side Adapter) Imports System. Add. In. Pipeline <Host. Adapter. Attribute()> _ Public Class Contract. To. View. Host. Side. Adapter Implements Wankuma. ICalc. Add. In ' Host. View Private _contract As ICalc. Add. In. Contract Private _handle As Contract. Handle Public Sub New(By. Val contract As ICalc. Add. In. Contract) My. Base. New() _contract = contract _handle = New Contract. Handle(contract) End Sub Public Function Caluclate(By. Val x As Integer, By. Val y As Integer) As Integer Implements ICalc. Add. In. Calc Return _contract. Calc(x, y) End Function End Class わんくま同盟 大阪勉強会 #23
実装(Add-in) Imports System. Add. In <Add. In("Adder Add. In", Version: ="1. 0. 0. 0")> _ Public Class Adder. Add. In Implements Wankuma. ICalc. Add. In ' Add-in View Public Function Calc(By. Val x As Integer, _ By. Val y As Integer) As Integer _ Implements Wankuma. ICalc. Add. In. Calc Return x + y End Function End Class • Name(必須), Description, Publisher, Versionの記述が可能 わんくま同盟 大阪勉強会 #23
アドイン探索 ' (Imports System. Add. In. Hosting) Dim add. In. Root = Environment. Current. Directory ' パイプラインセグメントキャッシュの更新 Dim warnings = Add. In. Store. Update(add. In. Root) For Each warning In warnings Console. Write. Line(warning) Next ' アドインの検出 Dim tokens = Add. In. Store. Find. Add. Ins(Get. Type(ICalc. Add. In), add. In. Root) • Add. In. Store Class: パイプラインとアド イン情報を格納・探索 わんくま同盟 大阪勉強会 #23
アドインのアクティブ化 ' ひとつめのアドイン選択 Dim token = tokens. First ' Add. In. Token Class ' アドインのアクティブ化 Dim calc = token. Activate(Of ICalc. Add. In)(Add. In. Security. Level. Internet) ' アドイン実行 Console. Write. Line(token. Name) Console. Write. Line("Calc({0}, {1}) = {2}", 160, 2, calc. Calc(160, 2)) • Add. In. Token Class: アクティブにできる アドインを表す わんくま同盟 大阪勉強会 #23 でと きり まあ しえ たず
参考Webサイト • アドインおよび拡張機能 – http: //msdn. microsoft. com/ja-jp/library/bb 384241. aspx • アプリケーションの機能拡張 – http: //msdn. microsoft. com/ja-jp/library/bb 909809. aspx • CLR 徹底解剖. NETアプリケーションの拡張性 – http: //msdn. microsoft. com/ja-jp/magazine/cc 163476. aspx – http: //msdn. microsoft. com/ja-jp/magazine/cc 163460. aspx – http: //msdn. microsoft. com/ja-jp/magazine/cc 700355. aspx • CLR Add-In Team Blog – http: //blogs. msdn. com/clraddins/default. aspx – http: //blogs. msdn. com/clraddins/pages/info. aspx • System. Add. In Tools and Samples – http: //codeplex. com/clraddins わんくま同盟 大阪勉強会 #23
- Slides: 24