Home All Groups Group Topic Archive Search About

More than one method is found with VB but not with C#

Author
29 Oct 2006 1:43 PM
De Roeck
Hi,

I've want to launch an application by reflection and get his handle
back.

But when I run this code in C# then it's handled correctly.
The application(-path) is started on a new thread and obj is the hwnd
of the started application.

private static BindingFlags allFlags = BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

public static object StartAUT(string applicationPath, string typeName)
{
    try
    {
        Assembly asm = Assembly.LoadFrom(applicationPath);
        Type typeUT = asm.GetType(typeName);
        object obj = Activator.CreateInstance(typeUT);
        MethodInfo mi = typeUT.GetMethod("Show", allFlags);
        mi.Invoke(obj, null);
        return obj;
    }
    catch{}
    return null;
}

but when I launch this code in VB.NET than I received an
AmbiguousMatchException execption.


Private Shared allFlags As BindingFlags = BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance

Public Shared Function StartAUT(ByVal applicationPath As String, ByVal
typeName As String) As Object
Try
    Dim asm As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom(applicationPath)
    Dim typeUT As Type = asm.GetType(typeName)
    Dim obj As Object = Activator.CreateInstance(typeUT)
    Dim mi As MethodInfo = typeUT.GetMethod("Show", allFlags)
    mi.Invoke(obj, Nothing)
    Return obj
Catch
End Try
Return Nothing
End Function


What do I wrong?

Greetings,
Davy

Author
29 Oct 2006 4:11 PM
Göran_Andersson
You are catching any exception that might occur in the method and
ignoring it. Start by at least throwing the exception again, so that you
see it.

De Roeck wrote:
Show quoteHide quote
> Hi,
>
> I've want to launch an application by reflection and get his handle
> back.
>
> But when I run this code in C# then it's handled correctly.
> The application(-path) is started on a new thread and obj is the hwnd
> of the started application.
>
> private static BindingFlags allFlags = BindingFlags.Public |
> BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
>
> public static object StartAUT(string applicationPath, string typeName)
> {
>     try
>     {
>         Assembly asm = Assembly.LoadFrom(applicationPath);
>         Type typeUT = asm.GetType(typeName);
>         object obj = Activator.CreateInstance(typeUT);
>         MethodInfo mi = typeUT.GetMethod("Show", allFlags);
>         mi.Invoke(obj, null);
>         return obj;
>     }
>     catch{}
>     return null;
> }
>
> but when I launch this code in VB.NET than I received an
> AmbiguousMatchException execption.
>
>
> Private Shared allFlags As BindingFlags = BindingFlags.Public Or
> BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance
>
> Public Shared Function StartAUT(ByVal applicationPath As String, ByVal
> typeName As String) As Object
> Try
>     Dim asm As System.Reflection.Assembly =
> System.Reflection.Assembly.LoadFrom(applicationPath)
>     Dim typeUT As Type = asm.GetType(typeName)
>     Dim obj As Object = Activator.CreateInstance(typeUT)
>     Dim mi As MethodInfo = typeUT.GetMethod("Show", allFlags)
>     mi.Invoke(obj, Nothing)
>     Return obj
> Catch
> End Try
> Return Nothing
> End Function
>
>
> What do I wrong?
>
> Greetings,
> Davy
Author
29 Oct 2006 6:17 PM
De Roeck
When I go through all the methods with .methods,
then I found in VB-language two methods with that name,
but only 1 in C# (and there should only one)

On Sun, 29 Oct 2006 17:11:31 +0100, Göran Andersson <gu***@guffa.com>
wrote:

Show quoteHide quote
>You are catching any exception that might occur in the method and
>ignoring it. Start by at least throwing the exception again, so that you
>see it.
>
>De Roeck wrote:
>> Hi,
>>
>> I've want to launch an application by reflection and get his handle
>> back.
>>
>> But when I run this code in C# then it's handled correctly.
>> The application(-path) is started on a new thread and obj is the hwnd
>> of the started application.
>>
>> private static BindingFlags allFlags = BindingFlags.Public |
>> BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
>>
>> public static object StartAUT(string applicationPath, string typeName)
>> {
>>     try
>>     {
>>         Assembly asm = Assembly.LoadFrom(applicationPath);
>>         Type typeUT = asm.GetType(typeName);
>>         object obj = Activator.CreateInstance(typeUT);
>>         MethodInfo mi = typeUT.GetMethod("Show", allFlags);
>>         mi.Invoke(obj, null);
>>         return obj;
>>     }
>>     catch{}
>>     return null;
>> }
>>
>> but when I launch this code in VB.NET than I received an
>> AmbiguousMatchException execption.
>>
>>
>> Private Shared allFlags As BindingFlags = BindingFlags.Public Or
>> BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance
>>
>> Public Shared Function StartAUT(ByVal applicationPath As String, ByVal
>> typeName As String) As Object
>> Try
>>     Dim asm As System.Reflection.Assembly =
>> System.Reflection.Assembly.LoadFrom(applicationPath)
>>     Dim typeUT As Type = asm.GetType(typeName)
>>     Dim obj As Object = Activator.CreateInstance(typeUT)
>>     Dim mi As MethodInfo = typeUT.GetMethod("Show", allFlags)
>>     mi.Invoke(obj, Nothing)
>>     Return obj
>> Catch
>> End Try
>> Return Nothing
>> End Function
>>
>>
>> What do I wrong?
>>
>> Greetings,
>> Davy
Author
29 Oct 2006 6:35 PM
Theo Verweij
I think both C# and VB are right from there respective points of view.
C# is case sensitive, VB is not.

So, when you have a function named "Show" and another function named
"show" in the assembly, and you are asking for the info of the method
"Show", then C# will find one and VB will find two.

If you add the flag BindingFlags.IgnoreCase to the c# version, I think
you will also get 2 functions in C#.


De Roeck wrote:
Show quoteHide quote
> When I go through all the methods with .methods,
> then I found in VB-language two methods with that name,
> but only 1 in C# (and there should only one)
>
> On Sun, 29 Oct 2006 17:11:31 +0100, Göran Andersson <gu***@guffa.com>
> wrote:
>
>> You are catching any exception that might occur in the method and
>> ignoring it. Start by at least throwing the exception again, so that you
>> see it.
>>
>> De Roeck wrote:
>>> Hi,
>>>
>>> I've want to launch an application by reflection and get his handle
>>> back.
>>>
>>> But when I run this code in C# then it's handled correctly.
>>> The application(-path) is started on a new thread and obj is the hwnd
>>> of the started application.
>>>
>>> private static BindingFlags allFlags = BindingFlags.Public |
>>> BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
>>>
>>> public static object StartAUT(string applicationPath, string typeName)
>>> {
>>>     try
>>>     {
>>>         Assembly asm = Assembly.LoadFrom(applicationPath);
>>>         Type typeUT = asm.GetType(typeName);
>>>         object obj = Activator.CreateInstance(typeUT);
>>>         MethodInfo mi = typeUT.GetMethod("Show", allFlags);
>>>         mi.Invoke(obj, null);
>>>         return obj;
>>>     }
>>>     catch{}
>>>     return null;
>>> }
>>>
>>> but when I launch this code in VB.NET than I received an
>>> AmbiguousMatchException execption.
>>>
>>>
>>> Private Shared allFlags As BindingFlags = BindingFlags.Public Or
>>> BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance
>>>
>>> Public Shared Function StartAUT(ByVal applicationPath As String, ByVal
>>> typeName As String) As Object
>>> Try
>>>     Dim asm As System.Reflection.Assembly =
>>> System.Reflection.Assembly.LoadFrom(applicationPath)
>>>     Dim typeUT As Type = asm.GetType(typeName)
>>>     Dim obj As Object = Activator.CreateInstance(typeUT)
>>>     Dim mi As MethodInfo = typeUT.GetMethod("Show", allFlags)
>>>     mi.Invoke(obj, Nothing)
>>>     Return obj
>>> Catch
>>> End Try
>>> Return Nothing
>>> End Function
>>>
>>>
>>> What do I wrong?
>>>
>>> Greetings,
>>> Davy
Author
29 Oct 2006 8:05 PM
De Roeck
I've futher analysed the differences and found that in Reflector that
there are two Show-methods

Public Sub Show()
Public Sub Show(ByVal owner As IWin32Window)

I want to invoke the first one, that without paramaters
so I use:

VB.NET
-------
Dim asm As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom(applicationPath)
Dim typeUT As Type = asm.GetType(typeName)
Dim obj As Object = Activator.CreateInstance(typeUT)
Dim mi As MethodInfo = typeUT.GetMethod("Show", allFlags)
mi.Invoke(obj, Nothing)
------

But he also invoke the second method (of show).

when I run the same in C# then he only invokes that one without
paramters:

C#
------
Assembly asm = Assembly.LoadFrom(applicationPath);
Type typeUT = asm.GetType(typeName);
object obj = Activator.CreateInstance(typeUT);
MethodInfo mi = typeUT.GetMethod("Show", allFlags);
mi.Invoke(obj, null);
------

How to tell VB that he has to invoke only those without parameters?
Author
29 Oct 2006 8:04 PM
De Roeck
I've futher analysed the differences and found that in Reflector that
there are two Show-methods

Public Sub Show()
Public Sub Show(ByVal owner As IWin32Window)

I want to invoke the first one, that without paramaters
so I use:

VB.NET
-------
Dim asm As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom(applicationPath)
Dim typeUT As Type = asm.GetType(typeName)
Dim obj As Object = Activator.CreateInstance(typeUT)
Dim mi As MethodInfo = typeUT.GetMethod("Show", allFlags)
mi.Invoke(obj, Nothing)
------

But he also invoke the second method (of show).

when I run the same in C# then he only invokes that one without
paramters:

C#
------
Assembly asm = Assembly.LoadFrom(applicationPath);
Type typeUT = asm.GetType(typeName);
object obj = Activator.CreateInstance(typeUT);
MethodInfo mi = typeUT.GetMethod("Show", allFlags);
mi.Invoke(obj, null);
------

How to tell VB that he has to invoke only those without parameters?