先日ふとWPFでも触ってみようかなっとVisual Studio 2008 のWPFアプリケーションプロジェクトテンプレートでプロジェクト作ったら、何にもしていないのにビルドエラーになったことが若干のトラウマになっています、とりこびとです。夜分遅くに失礼いたしますw
そんなことはさておき、今日はなんとなく My のお話。Visual Basic 2005 から実装された Visual Basic のアレ。My.Settings とか My.Resource とかいろいろありますよね♪でもさ!でもさ!
My.Little.Lover とか作ってみたくない?w(そうでもない?w)
というわけで、オリジナルの My とかの作り方~w
方法はいたって簡単♪
My.Settings をまねっこ!(ニヤ
で、実際の My.Settings の雛形コードがこれ↓Visual Basic の(たとえばWindows アプリケーション)プロジェクト作ってソリューションエクスプローラですべてのファイルを表示したときに出てくる「Settings.Designer.vb」ですね。
Option Strict On
Option Explicit On
Namespace My
<Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(), _
Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0"), _
Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Partial Friend NotInheritable Class MySettings
Inherits Global.System.Configuration.ApplicationSettingsBase
Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings), MySettings)
#Region "My.Settings Auto-Save Functionality"
#If _MyType = "WindowsForms" Then
Private Shared addedHandler As Boolean
Private Shared addedHandlerLockObject As New Object
<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
Private Shared Sub AutoSaveSettings(ByVal sender As Global.System.Object, ByVal e As Global.System.EventArgs)
If My.Application.SaveMySettingsOnExit Then
My.Settings.Save()
End If
End Sub
#End If
#End Region
Public Shared ReadOnly Property [Default]() As MySettings
Get
#If _MyType = "WindowsForms" Then
If Not addedHandler Then
SyncLock addedHandlerLockObject
If Not addedHandler Then
AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings
addedHandler = True
End If
End SyncLock
End If
#End If
Return defaultInstance
End Get
End Property
End Class
End Namespace
Namespace My
<Global.Microsoft.VisualBasic.HideModuleNameAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()> _
Friend Module MySettingsProperty
<Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")> _
Friend ReadOnly Property Settings() As Global.WindowsApplication1.My.MySettings
Get
Return Global.WindowsApplication1.My.MySettings.Default
End Get
End Property
End Module
End Namespace
つらつら眺めているとどうやらMy.Settings は実際にはMy.MySettingsProperty.Settings(Global.WindowsApplication1.My.MySettings型)っぽいですよ。それがどうしてMy.Settingsでアクセスできるのかしら~?
しかけはモジュールというものとこれらの属性っぽいです♪
MSDN:EditorBrowsableAttribute クラス
プロパティまたはメソッドをエディタから参照できるかどうかを指定します。このクラスは継承できません。
MSDN:HideModuleNameAttribute クラス
HideModuleNameAttribute 属性がモジュールに適用された場合は、モジュールに必要な修飾だけを使用してモジュール メンバにアクセスできます。
これらの属性を使うことで、IntelliSense への表示をコントロールすることで、My.MySettingsProperty.SettingsがMy.Settingsになるんですね。
じゃ、さっそく
My.Little.Lover とか作ってみる♪
一つ目は Lover というプロパティを持った MyLittle という名前のクラスを作ります。実装はシングルトンパターンですね。
Option Strict On
Option Explicit On
Namespace My
<System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)> _
Friend NotInheritable Class MyLittle
Private Shared _defaultInstance As MyLittle
Public Shared ReadOnly Property DefaultInstance() As MyLittle
Get
Return _defaultInstance
End Get
End Property
Private _lover As Object
Public Property Lover() As Object
Get
Return _lover
End Get
Set(ByVal value As Object)
_lover = value
End Set
End Property
End Class
End Namespace
んでもってお次は Little という名前のプロパティを持ったMyLittlePropertyという名前のモジュールです。
Namespace My
<Microsoft.VisualBasic.HideModuleNameAttribute()> _
Friend Module MyLittleProperty
Friend ReadOnly Property Little() As WindowsApplication1.My.MyLittle
Get
Return MyLittle.DefaultInstance
End Get
End Property
End Module
End Namespace
これでMy.MyLittleProperty.Little.Loverの出来上がり・・・って、違う違うwここでMyLittleProperty モジュールの属性 HideModuleNameAttribute がMyLittlePropertyというモジュール名を書かなくてもメンバにアクセスできるようにします。よって、あっという間に

できましたっと♪