Stringの名前とデフォルト値を持つ定数

投稿日 : 2008年7月5日 23:39

 Windows Live Writer SDK関連のための話しです。WLWのプラグイン作成時に IProperties というインタフェースが出てきます。これは次のようにプラグイン固有のまたはプラグインにより挿入したコンテンツ固有の情報を取得・設定するために用意されています。

Dim foo = Options.GetString("Foo", "foo")
Options.SetString("Foo", "bar")

 OptionsがIProperties型。情報を取得する場合、GetString(名前, デフォルト値)と呼び出します。設定する場合は、SetString(名前, 設定値)となります。基本的な型についてはそれぞれGetIntなどのメソッドが用意されてます。

 このIPropertiesを上のようにそのまま使用すると大量に文字列リテラルが出てきます。リテラルじゃないのは設定値となる"bar"のところだけ。単純に解決するには全部定数に置き換えます。

Const Foo As String = "Foo"
Const FooDefaultValue As String = "foo"

 するとこうなる。単純で良いのかもしれないけれど、Fooという名前にFooと値をいれてる、この例ではFooひとつだけだけど複数の設定項目が出た場合にグループとして扱えない、FooとFooDefaultValueは意味的にはペアとして扱いたいなど気になる点が出てきます。

 文字列の列挙体があればいいのだけど、それは言語的にできません。ただし、じゃんぬさんが昔に文字列の列挙体というエントリを投稿されてます。詳しくはそちらで。私もこれを参考にして、ペアの部分についても考えて出たコードが以下。

 まずは、値の設定・取得するところは次のようになって、

Dim foo = Options.GetString(FooPluginOptions.Foo.Name, FooPluginOptions.Foo.DefaultValue)
Options.SetString(FooPluginOptions.Foo.Name, "bar")

設定項目ごとに記述する部分は次のようになりました。

Public NotInheritable Class FooPluginOptions
    Public Shared ReadOnly Foo As New Item(Of String)("foo")
    Public Shared ReadOnly Bar As New Item(Of Integer)(100)

    ' (略 見てはいけない部分)

End Class

設定項目ごとに項目の型とデフォルト値を指定した、Itemというオブジェクトを作ってます。

 隠された部分は以下。FooPluginOptionsの中にあるコードです。うーむ。設定項目のグループごとに書くとなると、どんだけーって感じですね。Itemクラスは外に出しても問題ありませんが、コンストラクタ内のコードは必要になり親クラスとして切り出すと動作しなくなってしまいます。

 設定値の名前とそのデフォルト値のペアとしてItemクラスを作っています。プロパティとしてReadOnlyのNameとDefaultValueを持ってます。ToStringをオーバーライドしてNameを返すようにしているので Strict Offの場合はNameプロパティ要らず(私は常にStrict Onだけど)。Sharedなコンストラクタ内で自分自身のフィールドを取得してItemオブジェクトのNameプロパティにフィールド名そのままを設定してます。Private変数を書き変えてます。

Public NotInheritable Class Item(Of T)
    Private _name As String
    Public ReadOnly Property Name() As String
        Get
            Return _name
        End Get
    End Property

    Private _defaultValue As T
    Public ReadOnly Property DefaultValue() As T
        Get
            Return _defaultValue
        End Get
    End Property

    Public Sub New(ByVal defaultValue As T)
        _defaultValue = defaultValue
    End Sub

    Public Overrides Function ToString() As String
        Return _name
    End Function
End Class

Shared Sub New()
    Dim fieldsInfo = GetType(BasePropertyItems).GetFields( _
        BindingFlags.DeclaredOnly Or _
        BindingFlags.GetField Or _
        BindingFlags.Instance Or _
        BindingFlags.Public Or _
        BindingFlags.Static Or _
        BindingFlags.FlattenHierarchy)

    For Each info In fieldsInfo
        Dim item = info.GetValue(Nothing)
        item.GetType.InvokeMember( _
            "_name", _
            BindingFlags.SetField Or BindingFlags.NonPublic Or BindingFlags.Instance, _
            Nothing, _
            item, _
            New Object() {info.Name}, _
            Nothing, Nothing, Nothing)
    Next
End Sub

 どんな感じですかねぇ。ナイスな方法があれば教えてください。

フィードバック

# IPropertiesと拡張メソッド

2008/07/06 0:00 by katamari.wankuma.com
IPropertiesと拡張メソッド

# bYCGpflGaPvhvlVWCu

2011/09/28 16:40 by http://oemfinder.com
XvV2aD Author, keep doing in the same way..!

# IvcUfhWxiRhgFTUHmn

2011/10/21 22:04 by http://www.epotenzmittel.com/
Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!...

# JEEqvLpEvqo

2011/11/02 5:24 by http://www.pharmaciecambier.com/
Can be also this issue because the truth can be achieved only in a dispute :D

# QizBWlwtMWMrlLffV

2011/11/08 16:26 by http://www.disfuncion-erectil.org
Scribbler, give me a student's record-book!)))

# npByAQDENFqrMnuVzHL

2011/11/09 6:39 by http://www.farmaciaunica.com/
Yeah, now it's clear !... And firstly I did not understand very much where there was the link with the title itself !!...

# xQaftdoxbmOmfM

2011/11/16 4:39 by http://www.hooksandlattice.com/garden-table.html
Current blog, fresh information, I read it from time to time!!...

# Hi, I do think this is a great web site. I stumbledupon it ;) I'm going to come back yet again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide other people.

2018/10/06 23:05 by Hi, I do think this is a great web site. I stumble
Hi, I do think this is a great web site. I stumbledupon it ;) I'm going to come back yet
again since i have saved as a favorite it. Money
and freedom is the best way to change, may you be rich and continue to guide other people.

# I have been exploring for a little bit for any high-quality articles or weblog posts on this sort of house . Exploring in Yahoo I ultimately stumbled upon this web site. Reading this info So i am happy to show that I've a very good uncanny feeling I disc

2018/10/16 17:02 by I have been exploring for a little bit for any hig
I have been exploring for a little bit for any high-quality articles or weblog posts on this sort of house .
Exploring in Yahoo I ultimately stumbled upon this web site.
Reading this info So i am happy to show that I've a very good uncanny feeling I discovered exactly what I needed.
I such a lot unquestionably will make sure to don?t put out of your
mind this web site and give it a look on a continuing
basis.

# I'm truly enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work!

2018/11/11 5:30 by I'm truly enjoying the design and layout of your w
I'm truly enjoying the design and layout of your website. It's a very easy on the
eyes which makes it much more pleasant for me to come here
and visit more often. Did you hire out a designer to create your theme?
Superb work!

# Quality posts is the secret to interest the users to visit the website, that's what this site is providing.

2018/11/13 0:44 by Quality posts is the secret to interest the users
Quality posts is the secret to interest the users
to visit the website, that's what this site is providing.

# Hello there! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any suggestions?

2018/11/15 15:31 by Hello there! Do you know if they make any plugins
Hello there! Do you know if they make any plugins to safeguard against hackers?
I'm kinda paranoid about losing everything I've worked hard on. Any suggestions?

# Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I hope to give something back and aid others like you aided me.

2019/05/11 17:08 by Heya i am for the first time here. I found this bo
Heya i am for the first time here. I found this board and
I find It truly useful & it helped me out a lot. I hope to give something back and aid others like you aided me.

# This is really fascinating, You are a very skilled blogger. I've joined your rss feed and look forward to searching for more of your excellent post. Also, I've shared your website in my social networks

2019/08/19 19:53 by This is really fascinating, You are a very skilled
This is really fascinating, You are a very skilled blogger.

I've joined your rss feed and look forward to searching for more
of your excellent post. Also, I've shared your website in my social networks

# What's up mates, how is the whole thing, and what you wish for to say regarding this paragraph, in my view its in fact remarkable for me.

2021/08/23 22:22 by What's up mates, how is the whole thing, and what
What's up mates, how is the whole thing, and what you wish
for to say regarding this paragraph, in my view its in fact remarkable for me.

# This paragraph is in fact a pleasant one it helps new web viewers, who are wishing for blogging.

2021/08/26 4:10 by This paragraph is in fact a pleasant one it helps
This paragraph is in fact a pleasant one it helps new web viewers, who are
wishing for blogging.

# This paragraph is in fact a pleasant one it helps new web viewers, who are wishing for blogging.

2021/08/26 4:11 by This paragraph is in fact a pleasant one it helps
This paragraph is in fact a pleasant one it helps new web viewers, who are
wishing for blogging.

# I'm not positive the place you're getting your information, but great topic. I must spend a while finding out more or figuring out more. Thanks for magnificent info I used to be searching for this info for my mission.

2021/09/02 23:25 by I'm not positive the place you're getting your inf
I'm not positive the place you're getting your information, but
great topic. I must spend a while finding out more or figuring
out more. Thanks for magnificent info I used to be searching for this info for my mission.

# I'm not positive the place you're getting your information, but great topic. I must spend a while finding out more or figuring out more. Thanks for magnificent info I used to be searching for this info for my mission.

2021/09/02 23:26 by I'm not positive the place you're getting your inf
I'm not positive the place you're getting your information, but
great topic. I must spend a while finding out more or figuring
out more. Thanks for magnificent info I used to be searching for this info for my mission.

# I'm not positive the place you're getting your information, but great topic. I must spend a while finding out more or figuring out more. Thanks for magnificent info I used to be searching for this info for my mission.

2021/09/02 23:27 by I'm not positive the place you're getting your inf
I'm not positive the place you're getting your information, but
great topic. I must spend a while finding out more or figuring
out more. Thanks for magnificent info I used to be searching for this info for my mission.

# I'm not positive the place you're getting your information, but great topic. I must spend a while finding out more or figuring out more. Thanks for magnificent info I used to be searching for this info for my mission.

2021/09/02 23:28 by I'm not positive the place you're getting your inf
I'm not positive the place you're getting your information, but
great topic. I must spend a while finding out more or figuring
out more. Thanks for magnificent info I used to be searching for this info for my mission.

# It's impressive that you are getting thoughts from this post as well as from our discussion made here.

2021/09/03 0:55 by It's impressive that you are getting thoughts from
It's impressive that you are getting thoughts from this post as well as from our discussion made here.

# It's impressive that you are getting thoughts from this post as well as from our discussion made here.

2021/09/03 0:56 by It's impressive that you are getting thoughts from
It's impressive that you are getting thoughts from this post as well as from our discussion made here.

# It's impressive that you are getting thoughts from this post as well as from our discussion made here.

2021/09/03 0:57 by It's impressive that you are getting thoughts from
It's impressive that you are getting thoughts from this post as well as from our discussion made here.

# It's impressive that you are getting thoughts from this post as well as from our discussion made here.

2021/09/03 0:58 by It's impressive that you are getting thoughts from
It's impressive that you are getting thoughts from this post as well as from our discussion made here.

# Hello there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!

2021/09/03 2:16 by Hello there! I know this is kinda off topic but I
Hello there! I know this is kinda off topic but I was wondering if you knew where
I could locate a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having difficulty finding
one? Thanks a lot!

# Hello there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!

2021/09/03 2:17 by Hello there! I know this is kinda off topic but I
Hello there! I know this is kinda off topic but I was wondering if you knew where
I could locate a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having difficulty finding
one? Thanks a lot!

# Hello there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!

2021/09/03 2:18 by Hello there! I know this is kinda off topic but I
Hello there! I know this is kinda off topic but I was wondering if you knew where
I could locate a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having difficulty finding
one? Thanks a lot!

# Hello there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!

2021/09/03 2:19 by Hello there! I know this is kinda off topic but I
Hello there! I know this is kinda off topic but I was wondering if you knew where
I could locate a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having difficulty finding
one? Thanks a lot!

# I am actually delighted to read this web site posts which contains plenty of valuable facts, thanks for providing these data. ps4 https://j.mp/3z5HwTp ps4 games

2021/09/15 8:31 by I am actually delighted to read this web site post
I am actually delighted to read this web site posts
which contains plenty of valuable facts, thanks for providing these data.
ps4 https://j.mp/3z5HwTp ps4 games

# I am actually delighted to read this web site posts which contains plenty of valuable facts, thanks for providing these data. ps4 https://j.mp/3z5HwTp ps4 games

2021/09/15 8:32 by I am actually delighted to read this web site post
I am actually delighted to read this web site posts
which contains plenty of valuable facts, thanks for providing these data.
ps4 https://j.mp/3z5HwTp ps4 games

# I am actually delighted to read this web site posts which contains plenty of valuable facts, thanks for providing these data. ps4 https://j.mp/3z5HwTp ps4 games

2021/09/15 8:33 by I am actually delighted to read this web site post
I am actually delighted to read this web site posts
which contains plenty of valuable facts, thanks for providing these data.
ps4 https://j.mp/3z5HwTp ps4 games

# I am actually delighted to read this web site posts which contains plenty of valuable facts, thanks for providing these data. ps4 https://j.mp/3z5HwTp ps4 games

2021/09/15 8:34 by I am actually delighted to read this web site post
I am actually delighted to read this web site posts
which contains plenty of valuable facts, thanks for providing these data.
ps4 https://j.mp/3z5HwTp ps4 games
コメントの入力
タイトル
 
名前
 
Url
コメント