Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2006

Tip: Looking for answers? Try searching our database.

GetMethod AmbiguousMatchException  in VB but not in C# - Need Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
De Roeck - 29 Oct 2006 16:01 GMT
I 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 the same 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
Bryan Phillips - 29 Oct 2006 17:22 GMT
Do you have Option Strict turned on for VB?

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

> I want to launch an application by reflection and get his handle
> back.
[quoted text clipped - 46 lines]
> Greetings,
> Davy
De Roeck - 29 Oct 2006 19:15 GMT
It was "Custum", i've changed it to strict On, but the same problem.

when i go through al the methods, i found two methods with the name
"Show" in the VB-Language, but only 1 in C#

>Do you have Option Strict turned on for VB?
>
>Bryan Phillips
>MCSD, MCDBA, MCSE
>Blog:  http://bphillips76.spaces.live.com
De Roeck - 29 Oct 2006 21:10 GMT
I've futher analysed the differences and found with Reflector that
there are indeed 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?

>Do you have Option Strict turned on for VB?
>
[quoted text clipped - 52 lines]
>> Greetings,
>> Davy
Bryan Phillips - 29 Oct 2006 21:23 GMT
How about just finding the member based on the number of parameters?

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 = Nothing

For Each m As MethodInfo In typeUT.GetMethods("Show", allFlags)
    If m.GetParameters().Length = 0 Then
        mi = m
        Exit For
    End If
Next

mi.Invoke(obj, Nothing)

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

> I've futher analysed the differences and found with Reflector that
> there are indeed two Show-methods
[quoted text clipped - 87 lines]
> >> Greetings,
> >> Davy
De Roeck - 29 Oct 2006 21:37 GMT
Thanks for your reply

When I use following code :

----
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 = Nothing
For Each m As MethodInfo In typeUT.GetMethods(allFlags)
    If m.Name = "Show" And m.GetParameters().Length = 0 Then
        mi = m
           Exit For
    End If
Next
mi.Invoke(obj, Nothing)
---

Then I recieve an InvalidOperationException in the application that's
invoked.
But I C#-sharp code it's executed perfectly. So, i should think that
there isn't any problem in the called application.

>How about just finding the member based on the number of parameters?
>
[quoted text clipped - 108 lines]
>> >> Greetings,
>> >> Davy

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.