mardi 16 février 2016

How to get Protected Friend proprty from multilevel inherited class using reflection?

I have class structure somewhat like this,

 public class a
       Protected Friend Property EntityID() As Integer
        Get
            Return _entityId
        End Get
        Set(ByVal Value As Integer)
            _entityId = Value
        End Set
    End Property 
    //some other properties and methods
 End class


 public class b 
       Inherits a
        //some properties and methods
 End Class


 public class c 
       Inherits b
        //some properties and methods
 End Class

 public class d 
       Inherits c
        //some properties and methods

       Dim obj as D = new D();
       Dim data = obj.GetType().GetProperties(/*I have tried all binding flags here*/) 

 End Class

I want to access the EntityID property from class a from the object of Class D. Till now I googled and found lots of answeres from stack but any of those answers didn't give me what I want.

I get that property when I use code like,

 'obj.GetType().BaseType.BaseType.BaseType.GetProperties()'

But the use of the repetitive BaseType property doesn't look good and also it might cause problem if I add More inheritance in between in future. SO is there any way to avoid that and get what I want. If any confusion feel free to comment.

P.S- I have already tried lot of stack answers regarding this but not able to get what I want. If you know the C# way then also please suggest I will convert it to the VB.





Aucun commentaire:

Enregistrer un commentaire