I’m attempting to use an unknown type in a for each loop as per the following code:
private sub ReflectThis(ByVal rawData As Object()) Dim dataType As Type = rawData(0).GetType() Dim properties As PropertyInfo() = dataType.getProperties() For Each item As dataType In rawData ''//AAAA For Each property As System.Reflection.PropertyInfo properties ''//reflected code use here
The issue that I get is on the line marked AAAA. It complains that ‘dataType’ is not declared, which I take to mean it does not exist as a proper class.
The intent is to call a web service elsewhere, and regardless of which web service I call, use reflection to add the resulting data struct object’s information to a database.
What, if any, are the restrictions on doing something like
Dim myObject As variableInstanceOfTypeObjectHere
or am I making a more fundamental error? If I’m right, what are the workarounds, if any?
When you declare a variable ‘As’ a type, that means you know the type at compile time. That lets the compiler check what you’re doing with it. In this case, you don’t know the type at compile time – you’re getting it at execution time. All you know is that each item is an object – so either don’t specify the type (as suggested by Joel) or specify it as Object: