本ブログは更新を停止しました。Aerieをよろしくお願いいたします。
投稿カレンダーはJavaScriptが有効でない環境では使用できません。
αετο? / aetos / あえとす
シャノン? 誰それ。
埼玉を馬鹿にする奴は俺が許さん。
基本的に知ったかぶり。興味を持った技術に手を出して、ちょっと齧りはするものの、それを応用して何か形にするまでは及ばずに飽きて放り出す人。
投稿日時 : 2008年6月3日 18:58
「親クラス」どころか,「自分が」明示的に実装しているインターフェイスのメソッドですら呼べません。 interface IFoo { void Bar(); } class Foo : IFoo { void IFoo.Bar() { } void Hoge () { Bar(); // Error CS0103 } }
キャストしたらおkなんだよね。 nterface IFoo { void Bar(); } class Foo : IFoo { void IFoo.Bar() { } void Hoge () { ((IFoo)this).Bar(); } }
自分は「((IFoo)this).Bar()」が面倒なので IFooインタフェースのヘルパークラスを作って、 次のような感じにします。 static class FooHelper { static void Bar(IFoo foo) { foo.Bar(); } } 呼び出すときは 「FooHelper.Bar(this)」 とします。
(this as IFoo).Bar(); はできますが、 (base as IFoo).Bar(); はコンパイルできないし ((this as Base) as IFoo).Bar(); は (this as Ifoo).Bar(); を呼び出しちゃいます。
勘違いでした……。 Reflection使えばできますが,C#でできるとはいいませんよね……。 一応,MethodInfo.DeclaringTypeで自分かそうでないかもわかりますが……。
これって、thisのクラスでもbaseのクラスでも、それぞれで 同じインターフェイスを明示的に実装してる場合の話ですか? リフレクション使うと何でもできそうって思って試行錯誤したけど できませんでした;; BindingFlags.DeclaredOnlyって、インターフェイスには使えなさそうだし。。 という内容をコメントする前にリフレッシュしたら、YuOさんのコメントが~ Reflectionでできるんですか!教えてください。。
親クラスがインターフェイスを明示的に実装している場合、子クラスはそのインターフェイス型にキャストできません。 で、もしそれを可能にするんであれば、子クラスでも明示的に実装するしかありません。 が、前述のように親クラスの実装を呼び出せないので、それを呼ぶ必要がある場合は、protected なヘルパーメソッドを設ける必要があるかもしれません。
>C# では親クラスが明示的に実装しているインターフェイスのメソッドの実装は呼べない? 呼べちゃうとインターフェースの意味が無くなっちゃうと思います。いわゆる多重継承ができちゃうんで。
↑意味がなくなるということはないか。ただ、多重継承の問題が勃発するというだけで。
> 多重継承の問題が勃発する よくわからないんですが、どのへんで?
あれれれ? ごめんなさい。何か勘違いしていたようです。何かへんなこと考えてたんだろうなぁ・・・
ベースクラスのGetType().GetMethodを, ・メソッド名にIFoo.Bar ・フラグにBindingFlags.Instance | BindingFlags.NonPublic を指定して呼び出すと, ベースクラスのIFoo.Barを呼び出す為のMethodInfoを取得することができます。
もしかして勘違いしてた? .NET 2.0の場合、Formクラスは、 / IContainerControl Form > ContainerControl - ScrollableContro > ... となってる。 / IContainerControl で、Form1 - Form のように明示的にIContainerControlを実装した場合、 親クラス側のIContainerControlのメソッド/プロパティが呼べないってこと? ScrollableContro | ContainerControl - IContainerControl (A) | Form | Form1 - IContainerControl (B) のようなクラス階層で、 ((IContainerControl)this).ActivateControl(..)実行すると、 Form1でIContainerControlを明示的実装してなければ、(A)が呼ばれるけど、 派生クラスと、基底クラスで同一のインターフェースを明示的に実装すると、(B)になっちゃうね。
/ IContainerControl の表示がずれてるけど、うまく汲み取ってください
面倒くせー http://www.atmarkit.co.jp/fdotnet/onepoint/onepoint01/onepoint01_02.html
> ((IContainerControl)this).ActivateControl(..)実行すると、 > Form1でIContainerControlを明示的実装してなければ、(A)が呼ばれるけど、 > 派生クラスと、基底クラスで同一のインターフェースを明示的に実装すると、(B)になっちゃうね。 親クラスが IContainerControl.ActivateControl() を明示的に実装していなければ、単に ActivateControl(); とした場合は親クラスが、(this as IContainerControl).ActivateControl(); とした場合は子クラスのが呼ばれますね。 親クラスが IContainerControl.ActivateControl() を明示的に実装していた場合、親クラスがそれを間接的に呼び出す方法を提供していない限り、子クラスから親クラスの ActivateControl を呼び出すことは出来ません、と。
間接的に呼び出す方法とか、YuOさんの方法とか、 いろいろ最初に試してたんですが、全部ダメでした。。 YuOさんはできると書かれてるし、私も絶対できそうって思ってます。 おかしいところを教えてください;; public interface IFoo { void Bar(); } public class Test1 : IFoo { void IFoo.Bar() { MessageBox.Show("a"); } protected void Nika() { ((IFoo)this).Bar(); } } public class Test2 : Test1, IFoo { void IFoo.Bar() { MessageBox.Show("b"); } public void Hoge() { MethodInfo mi1 = typeof(IFoo).GetMethod("Bar", BindingFlags.Instance | BindingFlags.Public); if (mi1 != null) mi1.Invoke(this, null); else MessageBox.Show("ダメ"); MethodInfo mi2 = this.GetType().BaseType.GetMethod("IFoo.Bar", BindingFlags.Instance | BindingFlags.NonPublic); if (mi2 != null) mi2.Invoke(this, null); else MessageBox.Show("ダメ"); MethodInfo mi3 = this.GetType().GetMethod("IFoo.Bar", BindingFlags.Instance | BindingFlags.NonPublic); if (mi3 != null) mi3.Invoke(this, null); else MessageBox.Show("ダメ"); Nika(); } } シャノンさんのリンク先ですけど、 >この点VB.NETはC#よりも高い柔軟性を持っている。VB.NETでは、Rockyクラスの1つ目の方式での実装が当然できる。 これってコンパイルエラーになりますよ? VBは明示的な実装しかできないです。 けど最近、こんな話がありました。 http://www.panopticoncentral.net/archive/2008/04/07/23114.aspx 長くてごめんなさい><
あ、全部ダメというのは、aが表示されないという意味で、 全部"ダメ"が表示されるという意味じゃないです。 それにしても長すぎでした。。ごめんなさい。
YuOさんのブログの方に書いていただいたのを見て、 私の間違ってるところがわかりました! 今頃でごめんなさいw ↑のコードが私のところでダメだったのは、 名前空間に入れて実行してたからでした。。 インターフェイス名をフルネームで指定すれば、ちゃんと取得できました。 ↓こんな感じで。そりゃそうですね~orz GetMethod("WindowsFormsApplication1.IFoo.Bar", フラグ) 覚えていないとハマると思いますよ!!w
Wonderful blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Appreciate it
Hi there! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your posts. Can you recommend any other blogs/websites/forums that cover the same subjects? Thanks a lot!
Its such as you learn my mind! You appear to know so much about this, like you wrote the book in it or something. I believe that you simply can do with a few percent to drive the message house a bit, however other than that, this is magnificent blog. A fantastic read. I'll definitely be back.
These are really fantastic ideas in about blogging. You have touched some good factors here. Any way keep up wrinting.
I do not even understand how I stopped up right here, however I thought this post was once good. I do not recognise who you might be but definitely you are going to a famous blogger should you aren't already. Cheers!
Wow, that's what I was seeking for, what a stuff! existing here at this weblog, thanks admin of this web page. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
If you want to grow your experience just keep visiting this web page and be updated with the most recent news posted here.
Thanks for some other excellent post. Where else may anyone get that kind of info in such a perfect approach of writing? I have a presentation next week, and I'm on the search for such info.
Hey! Someone in my Facebook group shared this website with us so I came to take a look. I'm definitely loving the information. I'm book-marking and will be tweeting this to my followers! Fantastic blog and fantastic design and style.
This is my first time visit at here and i am truly pleassant to read everthing at single place.
http://imrdsoacha.gov.co/silvitra-120mg-qrms
Powered by: Copyright © αετος / aetos