none
given a class how do I execute its protected properties

    Question

  • Hi

    I am new to CSharp. I wanted to know as to how to get to execute => CanRaiseEvents property of a button. This is a protected property. In short I want to know given a class how do I execute its protected properties

    Sample Code is welcome

    regards

    Manoj Gokhale

    Tuesday, April 11, 2017 1:37 PM

Answers

  • Firstly you don't execute a property. A property can be read or written just like fields or variables.

    Secondly the CanRaiseEvents property isn't really for your usage. It is used by the component to manage the complexities of controlling at what point during the component lifetime it should or shouldn't raise events. Outside of components I don't know that I've ever seen code implement anything like this.

    Can you be more specific about what you want to do with this property? There really shouldn't be any scenario where you do anything with this property in your code. If you want to handle events from a component you hook up an event handler. If you want to trigger an event then do whatever interaction causes it. If you're writing your own type with events then you'd simply invoke the event directly. Some types provide protected methods to raise events.

    Protected members can only be access by derived types so if you want to access a protected member (property, method, etc) then you need to derive from the type in question. Protected members are for supporting derived types, not for general use.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Tuesday, April 11, 2017 1:58 PM
    Moderator
  • You can read a protected property by using Reflection. I am not recommending that you do this; as mentioned in previous replies, there is probably a good reason why it was protected. But if you really want to access it, you can do it like this:

    using System.Reflection;
    ...
    Button b = ... // Your button
    Type t = typeof(Button);
    PropertyInfo pi = t.GetProperty("CanRaiseEvents", BindingFlags.Instance | BindingFlags.NonPublic);
    bool canRaiseEvents = pi.GetValue(b);
    

    Wednesday, April 12, 2017 6:16 AM
    Moderator

All replies

  • I think you need to supply more information on what you are actually trying to do.

    Because, generally, you shouldn't need to access that property outside of the class itself, which is why it is protected in the first place.

    It is intended for specific situations for controls that you create yourself, where you may need to override that property ("protected" means it can be access only in the class itself or descendent classes).

    Tuesday, April 11, 2017 1:41 PM
  • Firstly you don't execute a property. A property can be read or written just like fields or variables.

    Secondly the CanRaiseEvents property isn't really for your usage. It is used by the component to manage the complexities of controlling at what point during the component lifetime it should or shouldn't raise events. Outside of components I don't know that I've ever seen code implement anything like this.

    Can you be more specific about what you want to do with this property? There really shouldn't be any scenario where you do anything with this property in your code. If you want to handle events from a component you hook up an event handler. If you want to trigger an event then do whatever interaction causes it. If you're writing your own type with events then you'd simply invoke the event directly. Some types provide protected methods to raise events.

    Protected members can only be access by derived types so if you want to access a protected member (property, method, etc) then you need to derive from the type in question. Protected members are for supporting derived types, not for general use.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Tuesday, April 11, 2017 1:58 PM
    Moderator
  • Hi Manoj,

    One way you could assign private value from another by wrapping you private property into public access specifier. Below is a sample code, you need to change it as per your requirement. Hope this helps you.

    private static string _pubName="";
    public static string pubName(){ return _pubName; }
    
    public static void assignValue(string currentName)
    {
       _pubName=currentName;
    }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, April 11, 2017 2:43 PM
    Moderator
  • You can read a protected property by using Reflection. I am not recommending that you do this; as mentioned in previous replies, there is probably a good reason why it was protected. But if you really want to access it, you can do it like this:

    using System.Reflection;
    ...
    Button b = ... // Your button
    Type t = typeof(Button);
    PropertyInfo pi = t.GetProperty("CanRaiseEvents", BindingFlags.Instance | BindingFlags.NonPublic);
    bool canRaiseEvents = pi.GetValue(b);
    

    Wednesday, April 12, 2017 6:16 AM
    Moderator
  • Hi Manoj,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, May 12, 2017 1:03 PM
    Moderator