How to get all types that implement a specific Interface in C# and VB.NET

To retrieve a IEnuemerable of all types in the current or another Assembly that implement a specific Interface / Abstract class and so on, you can use the snippets below.

Methods

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

Do you have an alternate or better method for this task?
If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet

How to enumerate an Enum in C# and VB.NET

To enumerate an Enum in C# and VB.NET you can use the snippet below.

Sample C#

public enum TestEnum
{
	These = 0,
	Are = 1,
	Sample = 2,
	Enums = 3,
	Duplicates = 3,
	Included = 4
}

static void Main(string[] args)
{
	foreach (var testEnum in Enum.GetNames(typeof(TestEnum)))
	{
		System.Console.WriteLine(testEnum);
	}
	Console.Read();
}

Sample VB.NET

Public Enum TestEnum
	These = 0
	Are = 1
	Sample = 2
	Enums = 3
	Duplicates = 3
	Included = 4
End Enum

Private Shared Sub Main(args As String())
	For Each testEnum As var In [Enum].GetNames(GetType(TestEnum))
		System.Console.WriteLine(testEnum)
	Next
	Console.Read()
End Sub

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 get the Month name of a Datetime in C# and VB.NET

    To get the Month name of a Datetime in C# and VB.NET you can use the following snippet.

    Sample C#

    Console.WriteLine(new DateTime(2010, 1, 1).ToString("MMM", CultureInfo.InvariantCulture)); //Jan
    Console.WriteLine(new DateTime(2010, 1, 1).ToString("MMMM", CultureInfo.InvariantCulture)); //January
    

    Sample VB.NET

    Console.WriteLine(New DateTime(2010, 1, 1).ToString("MMM", CultureInfo.InvariantCulture))
    'Jan
    Console.WriteLine(New DateTime(2010, 1, 1).ToString("MMMM", CultureInfo.InvariantCulture))
    'January
    

    Please rate this snippet

    How to get the path of the active app.config file in C# and VB.NET

    To get the path of the active app.config file in C# and VB.NET you can use the following snippet

    Sample C#

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
    

    Sample VB.NET

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
    

    Please rate this snippet

    How to get monday date of current week in MSSQL

    To get the Monday date of the current week you can use the snippet below.

    SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AS Monday
    

    Please rate this snippet