473,383 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Reflection that works in C# but not VB?

balabaster
797 Expert 512MB
Why does this work in C#

Expand|Select|Wrap|Line Numbers
  1.     class test
  2.     {
  3.         public double Value
  4.         {
  5.             get;
  6.             set;
  7.         }
  8.  
  9.         public test(double newVal){
  10.             Value = newVal;
  11.         }
  12.     }
  13.  
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             object a = new test(123.45);
  19.             Type dynType = Type.GetType("ConsoleApplication29.test");
  20.             System.Reflection.PropertyInfo dynProperty = dynType.GetProperty("Value");
  21.             double val = (double)dynProperty.GetValue(a,
  22.                                                       System.Reflection.BindingFlags.GetProperty,
  23.                                                       null,
  24.                                                       null,
  25.                                                       null);
  26.             Console.WriteLine(val);
  27.             Console.ReadLine();
  28.         }
  29.     }
But the direct equivalent in VB does not?
Expand|Select|Wrap|Line Numbers
  1.     Class Test
  2.  
  3.         Private _Value As Double
  4.  
  5.         Public Property Value() As Double
  6.             Get
  7.                 Return _Value
  8.             End Get
  9.             Set(ByVal value As Double)
  10.                 _Value = value
  11.             End Set
  12.         End Property
  13.  
  14.         Public Sub New(ByVal Value As Double)
  15.             _Value = Value
  16.         End Sub
  17.  
  18.     End Class
  19.  
  20.     Sub Main()
  21.  
  22.         Dim a As Object = New Test(123.45)
  23.         Dim dynType As Type = Type.GetType("ConsoleApplication28.Test2")
  24.         Dim dynProperty As System.Reflection.PropertyInfo = dynType.GetProperty("Value")
  25.         Dim val As Double = dynProperty.GetValue(a, _
  26.                                                  System.Reflection.BindingFlags.GetProperty, _
  27.                                                  Nothing, _
  28.                                                  Nothing, _
  29.                                                  Nothing)
  30.         Console.WriteLine(val)
  31.         Console.ReadLine()
  32.  
  33.     End Sub
So much for the two .NET languages doing the same thing... do you think this is a bug?

It fails in VB at the line:
Dim dynType As Type = Type.GetType("ConsoleApplication28.Test2")

Which returns nothing to dynType, but in C# it actually returns a type instance!

I'd like not to have to write a C# assembly for the sole purpose of allowing this function in my VB application...

Anyone got any ideas how to work around this?
Sep 20 '08 #1
3 1449
balabaster
797 Expert 512MB
No worries, figured it out...

I had my class specified inside a module... and C# explicitly adds the "using System;" clause but VB does not explicitly add the "imports System" clause.

So fixing those two things fixed it...
Sep 20 '08 #2
Plater
7,872 Expert 4TB
How did the missing System namespace reference affect things? Shouldn't there have been a compile error?
Sep 22 '08 #3
balabaster
797 Expert 512MB
How did the missing System namespace reference affect things? Shouldn't there have been a compile error?
You might think...now I'm not exactly sure what the original problem was, I just noted that after some tinkering, it worked. One of the things I did was to add the imports, as I noted that in C# the using line is added by the IDE when you create the project, but in VB the imports line is not, so it was one of the things I added in the course of my investigation.

All other things remaining equal, the code works in both platforms so I was wondering if there's some underlying bug not in the IDE or the compiler, but in the application template (when you create the new application).

I noted that a lot of people came across this issue and most never found the solution so I knew it wasn't just my code that was the problem - and it just didn't make sense to me that Type.GetType("MyClass") would work in C# and not in VB as the two languages are supposed to only differ syntactically, not fundamentally, so I knew there had to be a solution.

I wonder if in VB it may be that the basic GetType() causes some ambiguity. GetType() doesn't have any overloads that accept a string as an argument, but System.Type.GetType() does. So maybe there's some confusion during the compilation of the code. I'm the furthest thing from the best person to try and pin down what the underlying cause of the issue is in this kind of problem. All I know is that explicitly specifying System.Type.GetType() instead of just using Type.GetType() OR adding the imports line into the beginning of the code document appears to alleviate it.

Now that I've found a workaround, I've gone onto other tasks - I may come back and further investigate this later to see if I can figure it out in detail.
Sep 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Lev | last post by:
Hi, I have some code that does reflection on an assembly I load. When I try to get the attributes on one of the methods implemented in the assembly, the MC++ version does not return anything....
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
7
by: BK | last post by:
I've used reflection quite a bit to launch forms, it works great, requires little coding, and allows me to launch forms from a dynamic menu. Now I have a need to instantiate any one of several...
2
by: =?Utf-8?B?UmVuYXVkIExhbmdpcw==?= | last post by:
Hello, I have an asp.net web page (say page.aspx) which derives from a custom base page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method called DoSomething(params). My web...
5
by: | last post by:
I am having problems with casting or converting a class to to an interface from which it derives. I'm certain that it's due to how it's being loaded, but I'm not sure how to get past the problem....
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.