Why does this work in C# - class test
-
{
-
public double Value
-
{
-
get;
-
set;
-
}
-
-
public test(double newVal){
-
Value = newVal;
-
}
-
}
-
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
object a = new test(123.45);
-
Type dynType = Type.GetType("ConsoleApplication29.test");
-
System.Reflection.PropertyInfo dynProperty = dynType.GetProperty("Value");
-
double val = (double)dynProperty.GetValue(a,
-
System.Reflection.BindingFlags.GetProperty,
-
null,
-
null,
-
null);
-
Console.WriteLine(val);
-
Console.ReadLine();
-
}
-
}
But the direct equivalent in VB does not? - Class Test
-
-
Private _Value As Double
-
-
Public Property Value() As Double
-
Get
-
Return _Value
-
End Get
-
Set(ByVal value As Double)
-
_Value = value
-
End Set
-
End Property
-
-
Public Sub New(ByVal Value As Double)
-
_Value = Value
-
End Sub
-
-
End Class
-
-
Sub Main()
-
-
Dim a As Object = New Test(123.45)
-
Dim dynType As Type = Type.GetType("ConsoleApplication28.Test2")
-
Dim dynProperty As System.Reflection.PropertyInfo = dynType.GetProperty("Value")
-
Dim val As Double = dynProperty.GetValue(a, _
-
System.Reflection.BindingFlags.GetProperty, _
-
Nothing, _
-
Nothing, _
-
Nothing)
-
Console.WriteLine(val)
-
Console.ReadLine()
-
-
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?
3 1470
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...
Plater 7,872
Recognized Expert Expert
How did the missing System namespace reference affect things? Shouldn't there have been a compile error?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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);
|
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
|
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...
| |
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:
|
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:
|
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 {}
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |