VB reflection in for each loop - vb.net

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:
For Each item As Object In rawData ''//AAAA

Just don't specify the type:
For Each item in rawData

Related

c++ CLI: get HKEY from SafeRegistryHandle

I work with the registry and use the function RegNotifyChangeKeyValue. The first option requires registry key handle (HKEY), but I have RegistryKey, from which I can get SafeRegistryKey (key->Handle).
RegistryKey^ key = myKey;
RegNotifyChangeKeyValue(key->Handle, true, dwFilter, events[i], true);
Error: "argument of type "Microsoft::Win32::SafeHandles::SafeRegistryHandle" is incompatible with parameter of type "HKEY""
How do I get HKEY?
A conversion is required to get from the wrapped IntPtr in the safe-handle class to the native handle. It looks like this:
HKEY handle = (HKEY)key->Handle->DangerousGetHandle().ToPointer();
You do get to fret over the word "Dangerous" in the method name, it is aptly named. There is nothing that the CLR can do to ensure that the handle stays valid until you no longer need the change notifications. It is now your job to ensure that the key object reference stays visible and the garbage collector won't collect it too early.
It cannot be a local variable, it could be a member of a class but that in turn requires the class object to stay referenced. Storing it in a static variable is the safest way, calling GCHandle::Alloc() is the next best way, maybe you need gcroot<>. There's no context in the question to make the call.

Option Strict and Anonymous Types dont go together?

I have a Linq query that yields anonymous types. However, now I want to work with the parameters of this anonymous type and it does not seem to work.
For Each obj As Object in Query
Dim row As DataRow = obj.parameter
...
Next obj
Now the compiler throws an error on the expression obj.parameter: "Option Strict On disallows late binding". If I understand it right, the compiler doesnt know the parameters of the anonymous type. I tried Option Infer On (and removed As Object), based on Google results, but it didnt help. Which seems to make sense, because it always seems to be a widening conversion to me.
Is there anyway to fix this, or should I just create a custom type?
The code that declares the anonymous type (i.e. the Select part of your LINQ query) must be in the same method as the the code that uses it and the Query variable's declaration must have an inferred type. You cannot access the properties of an anonymous type after it has been cast to an Object since there is no named type to which you can cast it.
So make sure that your LINQ query (or, at least, the part that Selects into a new anonymous type) is in the same method. E.g.
Dim Query = From prod In products
Select prod.Name, prod.Price
For Each obj in Query
Dim name = obj.Name
...
Next obj

GetType not defined?

I'm developing a vb.net program with VS2008. In my case, dt_list is a instance member as List(of Single). When I wrote
dt_list = CType(ser2.Deserialize(r), dt_list.GetType())
VS2008 complains about "Type 'dt_list.GetType()' is not defined.".
Why? Thanks in advance.
The reason this is not allowed is that the exact value of the type for GetType() must be determined at runtime. The second parameter of CType can only be an "expression that is legal within an As clause in a Dim statement, that is, the name of any data type, object, structure, class, or interface", for instance, List(Of String), which can be determined at compile time.
The following is accordingly legal:
dt_list = CType(ser2.Deserialize(r), List(Of Single))
EDIT:
Casting to an arbitrary type, where the type is not known at compile time, is more involved, however. The proposed answer to this question in the MSDN Forums shows one approach. Though it's in C#, I will guess the same approach can be used in VB.NET as well.
What are you trying to achieve by selecting the cast type at runtime?
You may be able to solve your problem with an interface.
Dim dt_list As IList = Ctype(ser2.Deserialize(r), IList)
This gives you access to all of the List members in IList.

How Do I Create Something 'OF' a Variable's Type?

I have some code like:
Lookup(Of String)("Testing")
Lookup(Of Integer)("Testing")
And both of those Lookups work great. What I'm trying to is call the appropriate LookUp based on the type of another variable. Something that would look like...
Lookup(Of GetType(MyStringVariable))("Testing")
I've tried to Google this but I'm having a hard time coming up with an appropriate search. Can anyone tell me how to do what I want?
You do not specify the full signature for the method that you're calling, but my psychic powers tell me that it is this:
Function Lookup(Of T)(key As String) As T
And you want to avoid having to repeat Integer twice as in the example below:
Dim x As Integer
x = Lookup(Of Integer)("foo");
The problem is that type parameters are only deduced when they're used in argument context, but never in return value context. So, you need a helper function with a ByRef argument to do the trick:
Sub Lookup(Of T)(key As String, ByRef result As T)
T = Lookup(Of T)(key)
End Sub
With that, you can write:
Dim x As Integer
Lookup("foo", x);
One solution to this is to use reflection. See this question for details.
You can't use a dynamic type unless you do runtime compiling, which of course is really inefficient.
Although generics allows you to use different types, the type still has to be known at compile time so that the compiler can generate the specific code for that type.
This is not the way to go. You should ask about what problem you are trying to solve, instead of asking about the way that you think that it should be solved. Even if it might be possible to do something close to what you are asking, it's most likely that the best solution is something completely different.
The VB.NET compiler in VS2008 actually uses type-inference. That means if you are using a generic method, and one of the parameters is of the generic type, then you don't need to specify the generic type in your call.
Take the following definition...
Function DoSomething(Of T)(Target As T) As Boolean
If you call it with a strongly-typed String for Target, and don't specify the generic parameter, it will infer T as String.
If you call it with a strongly-typed Integer for Target, and don't specify the generic parameter, it will infer T as Integer.
So you could call this function as follows:
Dim myResult As Boolean = DoSomething("my new string")
And it will automatically infer the type of T as String.
EDIT:
NOTE: This works for single or multiple generic parameters.
NOTE: This works also for variables in the argument list, not just literals.

VB.NET Get underlying system.type from nullable type

I'm attempting to create a dataset based on the properties of an object. For example, I have an instance of a Person class with properties including ID, Forename, Surname, DOB etc. Using reflection, I'm adding columns to a new dataset based on the object properties:
For Each pi As PropertyInfo In person.GetType().GetProperties()
Dim column As New DataColumn(pi.Name, pi.PropertyType)
table.Columns.Add(column)
Next
My problem is that some of those properies are nullable types which aren't supported by datasets. Is there any way to extract the underlying system type from a nullable type?
Thanks.
Here's your answer, in VB. This may be overkill for your purposes, but it also might be useful to some other folks.
First off, here's the code to find out if you're dealing with a Nullable type:
Private Function IsNullableType(ByVal myType As Type) As Boolean
Return (myType.IsGenericType) AndAlso (myType.GetGenericTypeDefinition() Is GetType(Nullable(Of )))
End Function
Note the unusual syntax in the GetType. It's necessary. Just doing GetType(Nullable) as one of the commentors suggested did not work for me.
So, armed with that, you can do something like this... Here, in an ORM tool, I am trying to get values into a generic type that may or not be Nullable:
If (Not value Is Nothing) AndAlso IsNullableType(GetType(T)) Then
Dim UnderlyingType As Type = Nullable.GetUnderlyingType(GetType(T))
Me.InnerValue = Convert.ChangeType(value, UnderlyingType)
Else
Me.InnerValue = value
End If
Note that I check for Nothing in the first line because Convert.ChangeType will choke on it... You may not have that problem, but my situation is extremely open-ended.
Hopefully if I didn't answer your question directly, you can cannibalize this and get you where you need to go - but I just implemented this moments ago, and my tests are all passing.
Nullable.GetUnderylingType(myType)
will return the underlying type or null if it's not a nullable type.
I'm guessing that the problem is recognizing whether the property is nullable or not. In C# you do this with this code:
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
... but I'm not sure what the equivalent of that last clause is in VB.NET.
You can also use the GetGenericParameters() method on that type. myNullableObject.GetType().GetGenericParameters()[0] should give you the type of nullable it is (so Guid, Int32, etc.)
#Mendelt Siebenga: You can only call GetType on the value property if the variable is not set to null; otherwise, you'll get an exception.
What you want to do is use the "GetValueOrDefault" property and call GetType on that, since you are guaranteed it will not be null. Example:
Dim i As Nullable(Of Integer) = Nothing
Dim t As Type = i.GetValueOrDefault().GetType()