Generic Type Conversion snippet for C# and VB.NET

Here you can find a generic Type conversion snippet for C# and VB.NET.

Sample C#

public static T GenericConvert<T, X>(X value) where X : IConvertible
{
	return (T)Convert.ChangeType(value, typeof(T));
}

Sample VB.NET

Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
End Function

Please rate this snippet

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

    Please rate this snippet

    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

    Please rate this snippet

    How to convert a string to slug in PHP

    To convert a string to slug in PHP you can use the following snippet.

    Sample PHP

    function StringToSlug($input, $replacement){
    	(!isset($replacement) || trim($replacement)===''){
    		$replacement = "/"
    	}
    	$input = strtolower(trim($input));
    	$input = preg_replace('/[^a-z0-9-]/', $replacement, $input);
    	$input = preg_replace('/-+/', $replacement, $input);
    	return $input;
    }
    

    Please rate this snippet