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

Get Class and property name form an instance property
cgraus
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
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
soundboy72
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
Monarghel