Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Getting to Properties in derived class from base
Message
De
01/11/2010 16:48:51
 
 
À
01/11/2010 16:36:05
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
Divers
Thread ID:
01487780
Message ID:
01487800
Vues:
33
I guess reflection can work as the 'poor-mans interface' :-}
 PropertyInfo pi = this.GetType().GetProperty("EnableSomething");
if (pi!=null && pi.Propertytype==typeof(bool)
{
//behaviour supported.......
}
only downside is the reflection overhead (which for most practical usage is not a factor)....

>>Mabe you could rethink the class hierarchy. If the base class expects specific properties child classes then abstract methods/properties would be the way to go. Failing that, as Paul suggested, implementing interfaces is the next best option....
>>
>
>Ideally, I wouldn't be trying to touch properties that are created down the chain in the heirarchy, but I am adding functionality and trying to avoid changing the base class as it is part of the framework. Unless I can get what I need added to the framework, it is open to being broke with the next release. With the ideas I have been given, I will find a way even if not as elegant as I would like.
>Thanks for the help
>Tim
>
>
>>>I have several properties I added and was hoping for something a bit more steamlined but this may be as good as it gets unless I architect something more elegant.
>>>Thanks
>>>Tim
>>>
>>>>Hi,
>>>>Reflection? Given that it's a property:
public void MyMethod()
>>>>     {
>>>>         PropertyInfo pi = this.GetType().GetProperty("EnableSomething");
>>>>         bool b = (bool)  pi.GetValue(this, null);
>>>>     }
(with a sprinkling of checking for nulls, etc :-}
>>>>
>>>>>If I have a base class, and App level class, and several inherited classes but have added a property in the App Level classe, how can I get to it from the base? I have a reference to the class in the base
>>>>>
>>>>>
>>>>>
>>>>>public class MyBase()
>>>>>{
>>>>>     
>>>>>     public method MyMethod()
>>>>>     {
>>>>>          // I  want to access the EnableSomething property of the specific class here
>>>>>          // There is a reference to the class in a property
>>>>>          this.RunningClass.  // property not available.  How to get it?
>>>>>     }
>>>>>}
>>>>>
>>>>>public class MyAppLevelBase() : MyBase
>>>>>{
>>>>>     //Property added here
>>>>>     public bool EnableSomething
>>>>>     {
>>>>>          get {return this._enableSomething; }
>>>>>          set {this._enableSomething = value; }
>>>>>     }
>>>>>}
>>>>>
>>>>>// bunch of classes like this one
>>>>>public class MyClass1() : MyAppLevelBase
>>>>>{
>>>>>    public MyClass1()
>>>>>    {
>>>>>        this.EnableSomething = true;
>>>>>     }
>>>>>}
>>>>>
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform