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
  • How to get XNode as Xml String with indentation in C# and VB.NET

    Sample C#

    public static String XNodeToString(XNode node, int indentation = 2)
    {
    	using (var sw = new StringWriter())
    	{
    		using (var xw = new XmlTextWriter(sw))
    		{
    			xw.Formatting = Formatting.Indented;
    			xw.Indentation = indentation;
    			node.WriteTo(xw);
    		}
    		return sw.ToString();
    	}
    }
    

    Sample VB.NET

    Public Shared Function XNodeToString(node As XNode, Optional indentation As Integer = 2) As String
    	Using sw = New StringWriter()
    		Using xw = New XmlTextWriter(sw)
    			xw.Formatting = Formatting.Indented
    			xw.Indentation = indentation
    			node.WriteTo(xw)
    		End Using
    		Return sw.ToString()
    	End Using
    End Function
    

    for more informations on indentation = 2 see XmlTextWriter.Indentation Property

    How to get XmlNode as Xml String with indentation in C# and VB.NET

    Sample C#

    public static String XmlNodeToString(XmlNode node, int indentation = 2)
    {
    	using (var sw = new StringWriter())
    	{
    		using (var xw = new XmlTextWriter(sw))
    		{
    			xw.Formatting = Formatting.Indented;
    			xw.Indentation = indentation;
    			node.WriteContentTo(xw);
    		}
    		return sw.ToString();
    	}
    }
    

    Sample VB.NET

    Public Shared Function XmlNodeToString(node As XmlNode, Optional indentation As Integer = 2) As String
    	Using sw = New StringWriter()
    		Using xw = New XmlTextWriter(sw)
    			xw.Formatting = Formatting.Indented
    			xw.Indentation = indentation
    			node.WriteContentTo(xw)
    		End Using
    		Return sw.ToString()
    	End Using
    End Function
    

    for more informations on indentation = 2 see XmlTextWriter.Indentation Property

    How to Split a String at newline in C# and VB.NET

    To Split a String at newline in C# and VB.NET you can use the following snippet.

    Sample C#

    public static IEnumerable<String> SplitAtNewLine(string input)
    {
    	return input.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
    }
    

    Sample VB.NET

    Public Shared Function SplitAtNewLine(input As String) As IEnumerable(Of String)
    	Return input.Split(New () {Environment.NewLine}, StringSplitOptions.None)
    End Function