Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2004

Tip: Looking for answers? Try searching our database.

hook with reflection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roman Pokrovskij - 17 Jun 2004 13:27 GMT
Hello ALL

Does any one know if there is the way "to hook" method in the object using
reflection?

I don't want to override class (construct new one) but what I want is simply
replace one method with another on run time. This method is virtual
(overridden from abstract) so i guess it is possible.. How ?

The problem concretization:

Replace Paint(...) method defined in the DataGridTextBoxColumn class (and
declared in DataGridColumnStyle class) on run-time.
Fabian Schmied - 17 Jun 2004 14:31 GMT
> Does any one know if there is the way "to hook" method in the object  
> using
> reflection?

No, there is none.

> I don't want to override class (construct new one) but what I want is  
> simply
> replace one method with another on run time. This method is virtual
> (overridden from abstract) so i guess it is possible.. How ?

If you really need to do it at runtime, you can use Reflection.Emit to  
create a dynamic assembly and class and override the method there. You  
cannot simply replace the method, unless you ressort to the unmanaged  
profiling API, which supports method replacement at load-time (and which I  
would not recommend for a real application).

Fabian
Roman Pokrovskij - 17 Jun 2004 22:03 GMT
Hello Fabian,

Thank you for your reply.

Other question:

May be it is possible to invoke my code exactly before this method  [ in my
case Paint(...) ] starts running? I just wonder
if there is the way to implement something like "artifical" event using the
reflection or may be other api?

In my concreate task I need to change
DataGridTextBoxColumn.DataGridTableStyle.BackBrush field value (at run-time)
just before Paint(..) starts?

(BTW: many thanks to Lutz Roeder for his .NET Reflector
http://www.aisto.com/roeder/dotnet/)

> > Does any one know if there is the way "to hook" method in the object
> > using
[quoted text clipped - 14 lines]
>
> Fabian
Fabian Schmied - 18 Jun 2004 15:31 GMT
> May be it is possible to invoke my code exactly before this method  [ in  
> my
> case Paint(...) ] starts running? I just wonder
> if there is the way to implement something like "artifical" event using  
> the
> reflection or may be other api?

I'm sorry, I don't think there is an easy way to do this.

> In my concreate task I need to change
> DataGridTextBoxColumn.DataGridTableStyle.BackBrush field value (at  
> run-time)
> just before Paint(..) starts?

Why do you need to this? Can't you do it some time beforehand? What's the  
reason you can't you derive your own class from the column class and  
override the Paint method?

Fabian
Roman Pokrovskij - 18 Jun 2004 17:01 GMT
> > In my concreate task I need to change
> > DataGridTextBoxColumn.DataGridTableStyle.BackBrush field value (at
> > run-time)
> > just before Paint(..) starts?

> Why do you need to this?

This is the development of ExtenderProvider idea. I wanted to create
something that can be named as "DataTableColumnStyle extender" :-) ...

> Can't you do it some time beforehand? What's the
> reason you can't you derive your own class from the column class and
> override the Paint method?

In my case this is possible. Here I'm looking for best design solution.

Thank you Fabian.
Fabian Schmied - 21 Jun 2004 09:12 GMT
> This is the development of ExtenderProvider idea. I wanted to create
> something that can be named as "DataTableColumnStyle extender" :-) ...

One thing I could think of is to create create an in-memory class derived  
from the DataGridTextBoxColumn class (using CodeDom or Reflection.Emit, as  
I said).

>> Can't you do it some time beforehand? What's the
>> reason you can't you derive your own class from the column class and
>> override the Paint method?
>
> In my case this is possible. Here I'm looking for best design solution.

I fear, there is not much you can do with the possibilities provided by  
Reflection (or .NET), apart from what I mentioned. I guess to get the best  
design solution, you would need to use aspects. But I don't know of any  
functional aspect-oriented programming tool for .NET you could use.

Fabian
Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Roman Pokrovskij - 17 Jun 2004 22:11 GMT
BTW other question:

If it is impossible to "override" abstract method at run-time, how it is
possible to add methods at run-time ? I have read something about it. What I
remember that Invoke (or InvokeBegin / InvokeEnd ?) methods  are
"constructed" (in some way, really I don't imagine what does it mean) at
run-time ...

> > Does any one know if there is the way "to hook" method in the object
> > using
[quoted text clipped - 14 lines]
>
> Fabian
Fabian Schmied - 18 Jun 2004 15:33 GMT
> If it is impossible to "override" abstract method at run-time, how it is
> possible to add methods at run-time ? I have read something about it.  
> What I
> remember that Invoke (or InvokeBegin / InvokeEnd ?) methods  are
> "constructed" (in some way, really I don't imagine what does it mean) at
> run-time ...

They are provided by the runtime, i.e. the CLR. If you use Rotor, you can  
change it's sources to allow your own methods to be runtime-provided, but  
with ordinary .NET, you can not employ this mechanism for your problem.

Fabian

Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.