Get Class and property name form an instance property

Hello,

It is possible to get Class and property name like it is on the code sample
Take note on the comment inside the getNames method.




Public Class x
  Public Property y()

  Public Property z()
End Class


Public Class UseClass
  Dim ClassToGetNames As x

  Sub New()
    Dim ClassName, PropertyName As String
    getNames(ClassToGetNames.y, ClassName, PropertyName)
  End Sub

  Sub getNames(ByVal MyInstance As Object, ByRef ClassName As String, ByRef PropertyName As String)

    'Here i want to get the Name of the class and the name of the property in MyInstance

  End Sub
End Class


 


I expect the folowing results:
ClassName:"x"
PropertyName:"y"

Thanks


Answer this question

Get Class and property name form an instance property

  • cgraus

    Interesting idea and definitely possible, but beware that Reflection is slower than other methods of binding. (System.Reflection has a lot of work to do to dynamically figure out what a compiler could hardcode with more information.) A good, though advanced read, on how to make your Reflection-based apps hum is Joel Pobar's Reflection: Dodge Common Performance Pitfalls to Craft Speey Applications:

    http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx

    Your best bet would be to dynamically instantiate your instances, but manipulate them through the UserControl base class. For instance:



    Imports System.Reflection

    Module Module1
     
    Sub Main()
       
    Dim listOfUserControls As New ArrayList
       
    Dim assm As [Assembly] = [Assembly].LoadFrom("MyUserControls.dll")
       
    Dim types As Type() = assm.GetTypes()

       
    For Each t As Type In types
         
    If t.IsSubclassOf(GetType(System.Windows.Forms.UserControl)) And Not t.IsAbstract Then
           
    Dim ctrl As System.Windows.Forms.UserControl
            ctrl = Activator.CreateInstance(t)
            ctrl.Name = t.FullName
            listOfUserControls.Add(ctrl)
         
    End If
       
    Next

       
    For Each uc As System.Windows.Forms.UserControl In listOfUserControls
          Console.WriteLine(uc.Name)
       
    Next
    End Sub
    End
    Module

     


  • caaanerud

    You can definitely accomplish this in .NET. You'll want to read up System.Reflection. Here's some starting points:

    http://msdn.microsoft.com/msdnmag/issues/05/08/BasicInstincts/default.aspx

    http://samples.gotdotnet.com/QuickStart/howto/default.aspx url=/quickstart/howto/doc/GetTypes.aspx

    http://samples.gotdotnet.com/QuickStart/howto/default.aspx url=/quickstart/howto/doc/ListMembers.aspx

    You'll want to do something like:

    Shared Sub Main()
       Dim obj As X = New X
      
    Dim className As String
       className = GetClassName(obj)
       Console.WriteLine(className)
    End Sub

    Shared Function GetClassName(ByVal MyInstance As Object) As String
       Dim t As Type = MyInstance.GetType()
       Return t.Name
    End Function

    What you're trying to do with your technique for getting the property name would be exceedingly difficult if not impossible. (i.e. Off the top of my head, I can't think of a way to get the property name.) You'll want to take a look at Type.GetProperties() to get a list of properties for a type. Given obj.Y, you can't figure out the property name as in the called function you only have its value. Maybe some creative thinking on your part in the context of your application, you can figure out another way to tackle the problem.

  • PJFINTRAX

    .NET is an easy addiction to succumb to. I've been working with .NET for about 5-6 years now (i.e. since the betas of .NET Framework 1.0) and am still finding new and interesting areas to explore. And Microsoft keeps heaping on the goodness. .NET Framework 2.0 has some fantastic new features. And we still have Windows Communication Foundation (i.e. Indigo) and Windows Presentation Foundation (i.e. Avalon) coming our way. Enjoy the ride! I know that I am! :)

  • soundboy72

    Hello guys,

    I would like to go further. In my case, I have developped an UserControl library. What I would like to do is to create nodes of a treeview, the node name being the name of each usercontrols in this library. Clicking a node, I would like to create an instance of the corresponding usercontrol and add it as a child of a container control (could be a panel, a dialog window...).
    All will be done automaticly and without any hardcodage, if possible. I think that reflection can help me, but I do not really know where begin. Does anybody have some kind of "hook code" or links to article to drop me on the good path

    Thanks,

    Monarghel

  • jefflipe

    Thank you for your help Smile. In fact the speed concern should not be a problem, as I will load my control list once and call only the one I need when I need it (i.e. when the user click on the a node of the treeview). I am slowly discovering the power of the .Net framework and I am getting addicted... Tongue Tied.

    Monarghel

  • Get Class and property name form an instance property