Click here to Skip to main content
11,496,146 members (30,607 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C# reflection
eg.I have an instance of System.Windows.Forms.Control.
System.Windows.Forms.Control _control=new System.Windows.Forms.Control()
I can create a Click event dynamically using delegate,
_control.Click+=new EventHandle(control_Click);
Now,there is a protect virtual property in Control Class:
protected virtual bool CanEnableIme { get; }
What I want to do is to override the "CanEnableIme" property through the instance reference:_control.How to implement that? I have no idea about it.
Then for example,there is public virtual function:public virtual bool PreProcessMessage(ref Message msg),How can I override the function through "_control".

Thanks.As you can see that,I'm a Chinese,my ultimate goals is that:
I want to write some Chinese char on a draw board, such as Form,Panel,PictureBox ,...,which is usually a Type Inherit from Control.
Now I can write some English char on the board using DrawString function in GDI+ ,but I can't write Chinese char correctly, the board failed to get the focus of IME. I found that if I inherit a Type from Control, and override the "CanEnableIme" property,and let it return True(the property is readonly), the new Type could get the focus.
And then override the "CanEnableIme" property, I found that when I input one char using the Chinese IME ,the GDI+ draw the char twice,not only the Chinese char, also the English char.The reason of the error is that:when you open the IME and input something,the windows send two message "WM_CHAR" and "WM_IME_CHAR", the KeyPress event Triggered twice, so each char draw twice. That is why I want to override the PreProcessMessage function, for filtering the unnecessary msg.
That is all.
Posted 19-Jun-12 5:05am
Edited 19-Jun-12 6:19am
Sergey Alexandrovich Kryukov at 19-Jun-12 10:22am
The question makes no sense. What would be your idea on the purpose of such thing?
The term concept or virtual and overridden method/properly is related to a type (a class), not an instance of a class.

Therefore, the question makes no sense and just demonstrates that you did not get how OOP works in principle, well, not just yet; you need to understand it thoroughly, otherwise the further programming activity would be pretty much useless.

I am not sure you can understand that, but: I'm not saying you cannot create an overridden method/property during run time, but it is possible only if you create a brand new assembly during run time, by compilation some source code (it can be done via CodeDOM) or by creating one from scratch using System.Reflection.Emit. I don't think explaining further detail would be appropriate — these are way more advanced issues compared with "classic" OOP.

At the same time, I think your attention was to achieve some useful functionality. That's why it is important to start your question with explanation of your ultimate goals, before sharing your considerations on how you want to achieve them, as such considerations could be "infected" with some ideas you were preoccupied with, which could be reasonable or wrong. In this case, I cannot guess what would be your intentions, so there is nothing I can add to help you.

I fully agree with Sergey about the quality of your question and his other explainations - listen to him!

I'm just guessing: you want to switch between base class behavior and some overriden behavior (this could then be dynamic...)? Maybe you don't need a realy dynamic solution - so a "poor man's" solution could be the usage of a "prepared" override with a "switch" condition like this.

    class Program
        static void Main(string[] args)
            Child child = new Child();
            child.UseOverride = true;

    class Base
        public virtual void DoIt()
            Console.WriteLine("DoIt was called");
    class Child : Base
        public bool UseOverride { get; set; }
        public override void DoIt()
            if (UseOverride) // prepared override
                Console.WriteLine("DoIt override was called");
            else // call base class method
Sometimes a very frustrating aspect of programming wanting to do something, and not being able to do it. Sealed classes can sometimes be in your way, and sometimes it is that a method in a control is not marked as virtual. You can use the New keyword to replace a method, but if the class is viewed as the base class, like for controls, then your method will not be visible, and the original method will be invoked. Microsoft has generally been pretty good about allowing methods (properties) to be overridden, but there are cases where they do not allow overriding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 CHill60 248
2 RyanDev 230
3 Sascha Lefèvre 205
4 OriginalGriff 155
0 Sergey Alexandrovich Kryukov 10,372
1 OriginalGriff 8,871
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 19 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100