473,786 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection that works in C# but not VB?

balabaster
797 Recognized Expert Contributor
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("C onsoleApplicati on28.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 1470
balabaster
797 Recognized Expert Contributor
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 Recognized Expert Expert
How did the missing System namespace reference affect things? Shouldn't there have been a compile error?
Sep 22 '08 #3
balabaster
797 Recognized Expert Contributor
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("M yClass") 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.Get Type() 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.Get Type() 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
3697
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 discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
10
7372
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 Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
2
1914
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. See code below: Object __gc* memberAttributes = info->GetCustomAttributes(__typeof(EntryPointAttribute), false);
0
1548
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 execute button2's click event. This works great when i know the name of the method that i want to invoke. I do so by doing this: Dim AssemblyPointer As Reflection.Assembly
2
7775
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 dynamically load both the DALC component (for the method call) and the Entity component (for the custom type collection to pass in), but I keep getting an error ( Message "Object type cannot be converted to target type.") when invoking the method. If I...
7
2232
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 business classes dynamically, so my natural inclination was to use reflection. The problem I'm running into is that my business classes require arguments to be passed in where as the forms did not. Here is an example of launching a form:
2
2981
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 page contains a UserControl (say Control.ascx). In the control class, i want to call the DoSomething method. I cannot use type cast since in fact the base class Page structure is a little more complex (see bottom of post). Hence i cannot do:
5
1863
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. Here's a general outline of the architecture. It's oversimplified, but I think it's enough info to help: Assembly A.dll { interface IMyBase {}
17
2312
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 to build a compiler? One that will construct a user defined class "on the fly" (literally, the user defines a class, instantiates it, and runs it from the console mode, all the while prompted by the program)? I guess so, but my final question...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.