Reputation: 2023
I have started using reflection and am a bit confused about getting PropertyInfo
.
I do something like this and it works:
Dim x as New MyClass
Dim prop as PropertyInfo = x.GetType.GetProperty("Name")
I don't understand why I must have an instance of the class in order to get a property from it. If GetType
returns a Type object, why couldn't I just reference the type itself?
Dim prop as PropertyInfo = GetType(MyClass).GetProperty("Name")
or
Dim prop as PropertyInfo = MyClass.GetType.GetProperty("Name")
Upvotes: 0
Views: 704
Reputation:
A safer way:
Dim prop as PropertyInfo = GetType(MyClass).GetProperty(NameOf(MyClass.MyProperty))
Upvotes: 0
Reputation: 887453
Dim prop as PropertyInfo = GetType(MyClass).GetProperty("Name")
That is exactly correct.
Upvotes: 1