Click here to Skip to main content
15,616,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I have the class FilterableList(Of T As CodeElement).

FilterableList(Of T As CodeElement) has a static method which I want to access.

All I know at a given time is GetType(T). I know I need to do something like;

VB
FilterableList(Of ???).method()


But I can't figure out how to convert from T to the class itself.

- Rix

What I have tried:

....................................................
Posted
Updated 11-Jun-17 19:42pm
v2

1 solution

You seem confused: the FilterableList class has methods, which are not associated with the generic parameter. To access CodeElement methods, you need to provide a constraint of what classes T can be:
Public Class FilterableList(Of T As CodeElement)
	Inherits List(Of T)
...
	Public Sub DoSomething(x As T)
		x.MyMethod()
	End Sub
End Class


I'm trying to reverse the GetType() function so I can access a static method - that's why I don't believe I need the instance.

GetType(CodeElement) returns its type; can I get that type's class and then access the methods in it?


For static methods, you don't need GetType

CodeElement.MyStaticMethod()

is all you need. You can access static (Shared in VB talk) methods directly via the class name, and even access the base class methods that way:
VB
Public Class B
	Public Shared Sub MyBaseMethod()
	End Sub
End Class
Public Class D
	Inherits B
	Public Shared Sub MyMethod()
	End Sub
End Class
Private Sub MyButton_Click(sender As Object, e As EventArgs)
	B.MyBaseMethod()
	D.MyBaseMethod()
	D.MyMethod()
End Sub
 
Share this answer
 
v2
Comments
[no name] 12-Jun-17 1:47am    
Let's take this example:

x = GetType(MyCodeElement)

How do I get MyCodeElement from x?
OriginalGriff 12-Jun-17 2:01am    
Sorry?
That doesn't make any sense.
GetType returns a value of type Type - and you already have MyCodeElement before you call it...

I'm not trying to be annoying here, but we only get exactly what you type to work from, we get no other context, and we can't see your screen, access your HDD, or read your mind. So providing one line code fragments with no other context doesn't really help us to help you!
[no name] 12-Jun-17 5:19am    
The type of CodeElement gets passed through various classes and methods and eventually CodeElement itself goes out of scope so all I have is its type.
OriginalGriff 12-Jun-17 5:28am    
woah, woah - if CodeElement has gone out of scope then the variable is no longer available - and you can't access the instance it contained unless you have saved it somewhere else.

I think you are missing a lot out here, and that doesn't help us help you any faster.
If CodeElement is a type, then you don't need GetType but you can't use the instance methods of the class (or it's base class) because you don't have an instance for them to work on. You can call it's static classes with no problems, since they don't require an instance.
But to get an instance into "x", you need to either find an existing instance, or create one - you can't do anything particularly useful with just the Type info that GetType returns.

What are you actually trying to achieve? Ignore your specific types when you reply and use English, or all I'll get is another context free code fragment which doesn't help anybody!
[no name] 12-Jun-17 5:49am    
I'm trying to reverse the GetType() function so I can access a static method - that's why I don't believe I need the instance.

GetType(CodeElement) returns its type; can I get that type's class and then access the methods in it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900