Home All Groups Group Topic Archive Search About

How to tell .methode with one to take?

Author
31 Oct 2006 6:46 PM
De Roeck
Thanks for reading,

I've got the following function:

Private 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
  Dim aTtype As Type =
Assembly.LoadFrom(applicationPath).GetType(typeName)
  Dim obj As Object = Activator.CreateInstance(type1)
  aType.GetMethod("Show", allFlags).Invoke(obj, Nothing)
  Return obj
End Function

When I give as applicationPath a simple form (afrom.exe) then there
are by default two show-methods

1. show()
2. show(IWin32Window)

The program is giving an execption and complaining that he found two
methods.

I want to invoke the first one, that without parameters.
How can I do that, what do I wrong?

They already told me that I can use follow code to have the same
result:

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

I think that the problem is that I don't give the correct "empty"
parameter, and would like to know the sollution in VB.

I found the following C# code with do correctly, but I need it in VB.

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);
Return obj;
------


Thanks for helping.

Author
1 Nov 2006 5:00 AM
werD
here's the C# block in VB.net maybe this will get you started

Dim asm As Assembly = 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


Show quoteHide quote
"De Roeck" wrote:

> Thanks for reading,
>
> I've got the following function:
>
> Private 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
>   Dim aTtype As Type =
> Assembly.LoadFrom(applicationPath).GetType(typeName)
>   Dim obj As Object = Activator.CreateInstance(type1)
>   aType.GetMethod("Show", allFlags).Invoke(obj, Nothing)
>   Return obj
> End Function
>
> When I give as applicationPath a simple form (afrom.exe) then there
> are by default two show-methods
>
> 1. show()
> 2. show(IWin32Window)
>
> The program is giving an execption and complaining that he found two
> methods.
>
> I want to invoke the first one, that without parameters.
> How can I do that, what do I wrong?
>
> They already told me that I can use follow code to have the same
> result:
>
> ------
> For Each m As MethodInfo In typeUT.GetMethods("Show", allFlags)
>   If m.GetParameters().Length = 0 Then
>     mi = m
>     Exit For
>   End If
> Next
> ------
>
> I think that the problem is that I don't give the correct "empty"
> parameter, and would like to know the sollution in VB.
>
> I found the following C# code with do correctly, but I need it in VB.
>
> 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);
> Return obj;
> ------
>
>
> Thanks for helping.
>
Author
1 Nov 2006 8:49 PM
De Roeck
WerD, thanks for your reply..

I've used the code you've provided, but then I received an
AmbiguousMatchException.

So you do not take the method without parameters (and do not know with
one to take)


So I'm still here with the problem.

On Tue, 31 Oct 2006 21:00:01 -0800, werD
<w***@discussions.microsoft.com> wrote:

Show quoteHide quote
>here's the C# block in VB.net maybe this will get you started
>
>Dim asm As Assembly = 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
>
>
>"De Roeck" wrote:
>
>> Thanks for reading,
>>
>> I've got the following function:
>>
>> Private 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
>>   Dim aTtype As Type =
>> Assembly.LoadFrom(applicationPath).GetType(typeName)
>>   Dim obj As Object = Activator.CreateInstance(type1)
>>   aType.GetMethod("Show", allFlags).Invoke(obj, Nothing)
>>   Return obj
>> End Function
>>
>> When I give as applicationPath a simple form (afrom.exe) then there
>> are by default two show-methods
>>
>> 1. show()
>> 2. show(IWin32Window)
>>
>> The program is giving an execption and complaining that he found two
>> methods.
>>
>> I want to invoke the first one, that without parameters.
>> How can I do that, what do I wrong?
>>
>> They already told me that I can use follow code to have the same
>> result:
>>
>> ------
>> For Each m As MethodInfo In typeUT.GetMethods("Show", allFlags)
>>   If m.GetParameters().Length = 0 Then
>>     mi = m
>>     Exit For
>>   End If
>> Next
>> ------
>>
>> I think that the problem is that I don't give the correct "empty"
>> parameter, and would like to know the sollution in VB.
>>
>> I found the following C# code with do correctly, but I need it in VB.
>>
>> 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);
>> Return obj;
>> ------
>>
>>
>> Thanks for helping.
>>