How to get return Value from method invocation using reflection in C# and VB.NET

To get return Value from method invocation using reflection in C# and VB.NET you can use the snippet below.

Sample C#

var returnValue = (Int32)methodInfo.Invoke(null,null); //just cast the returned object of Invoke to the needed Type. Theres no difference if it is a static method/class or not

Sample VB.NET

Dim returnValue = DirectCast(methodInfo.Invoke(Nothing, Nothing), Int32) 'just cast the returned object of Invoke to the needed Type. Theres no difference if it is a static method/class or not

For more informations on how to invoke methods from other assemblies using reflection see:

  • How to call a static method in different assembly using Reflection in C# and VB.NET
  • How to call a non-static method in different assembly using Reflection in C# and VB.NET
  • Please rate this snippet

    How to call a non-static method in different assembly using Reflection in C# and VB.NET

    To call a static method in different assembly using Reflection in C# and VB.NET you can use the snippet below.

    Sample C#

    Assembly assembly = Assembly.LoadFile(@"Assembly_Location");   
    var type = assembly.Types().FirstOrDefault(x => x.FullName.Contains("MyClassName"));
    var constructor = type.GetConstructor(Type.EmptyTypes);
    
    var classObject = constructor.Invoke(new object[] { }); //call parameterless constructor
    var classObject = constructor.Invoke(new object[] { param1 }); //call non-parameterless constructor
    
    MethodInfo methodInfo = type.GetMethod("MyMethodName");
    methodInfo.Invoke(classObject, new object[] { }); //pass classobject, parameterless method
    methodInfo.Invoke(classObject, new object[] { param1 }); //pass classobject, parameterized method
    

    Sample VB.NET

    Dim assembly__1 As Assembly = Assembly.LoadFile("Assembly_Location")
    Dim type__2 = assembly__1.Types().FirstOrDefault(Function(x) x.FullName.Contains("MyClassName"))
    Dim constructor = type__2.GetConstructor(Type.EmptyTypes)
    
    Dim classObject = constructor.Invoke(New Object() {})'call parameterless constructor
    Dim classObject = constructor.Invoke(New Object() {param1})'call non-parameterless constructor
    
    Dim methodInfo As MethodInfo = type__2.GetMethod("MyMethodName")
    methodInfo.Invoke(classObject, New Object() {})'pass classobject, parameterless method
    methodInfo.Invoke(classObject, New Object() {param1})'pass classobject, parameterized method
    

    Please rate this snippet

    How to call a static method in different assembly using Reflection in C# and VB.NET

    To call a static method in different assembly using Reflection in C# and VB.NET you can use the snippet below.

    Sample C#

    Assembly assembly = Assembly.LoadFile(@"Assembly_Location"); 
    var t = assembly.Types().FirstOrDefault(x=>x.FullName.Contains("MyClassName"));
    var method = t.GetMethod("MyMethodName", BindingFlags.Public | BindingFlags.Static);
    method.Invoke(null, null); //assuming that the method does not have parameters
    method.Invoke(null, new object[] { param1}); //assuming that the method has 1 parameter
    

    Sample VB.NET

    Dim assembly__1 As Assembly = Assembly.LoadFile("Assembly_Location")
    Dim t = assembly.Types().FirstOrDefault(Function(x) x.FullName.Contains("MyClassName"))
    Dim method = t.GetMethod("MyMethodName", BindingFlags.[Public] Or BindingFlags.[Static])
    method.Invoke(Nothing, Nothing) 'assuming that the method does not have parameters
    method.Invoke(Nothing, New Object() {param1}) 'assuming that the method has 1 parameter
    

    Please rate this snippet

    How to use substring in IOS using Objective-C

    To use substring in IOS using Objective-C you can use the snippet below.

    Sample Objective-C

    NSString *myString = @"http://codesnippets.fesslersoft.de";
    NSString *myNewString = [myString substringFromIndex:7]; //myNewString will be "codesnippets.fesslersoft.de"
    

    Please rate this snippet

    How to autosize UILabel to Textsize in IOS using Objective-C

    labelTest.text = @"This is my Teststring.This is my Teststring.This is my Teststring.This is my Teststring.This is my Teststring.This is my Teststring.";
    labelTest.numberOfLines = 0;
    [labelTest sizeToFit];
    

    Please rate this snippet

    How to clear notifications in IOS using Objective-C

    To clear notifications in IOS using objective-c you can use the snippet below.

    Sample Objective-C

    //in method: application:didFinishLaunchingWithOptions
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    //in method: application:didReceiveRemoteNotification
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1]; //we need to to increment first
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

    Please rate this snippet

    How to show software keyboard when focusing textedit in Android

    To show software keyboard when focusing textedit in Android you can use the snippet below.

    Sample Java

    myTextEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean focus) {
            if (focus) {
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
    

    Please rate this snippet

    How to create an alert dialog in Android

    To create an alert dialog in Android you can use the following snippet.

    Sample Java

    new AlertDialog.Builder(this)
        .setTitle("Delete items")
        .setMessage("Are you sure you want to delete this items?")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                // YES
            }
         })
        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                // NO
            }
         })
        .setIcon(android.R.drawable.ic_dialog_alert)
         .show();
    

    Please rate this snippet

    How to reload or rerender the page using AngularJS

    To reload or rerender the page using AngularJS you can use the snippet below.

    Sample AngularJS

    $route.reload();
    

    Please rate this snippet