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

How to scale a Size Object by percentage in C# and VB.NET

To scale a Size Object by percentage in C# and VB.NET you can use the following snippet.

Sample C#

public static Size Scale(this Size size, float scalePercent)
{
	if (Math.Abs(scalePercent - 1) < float.Epsilon)
	{
		return size;
	}

	var height = (int) (size.Height*(scalePercent/100));
	var width = (int)(size.Width * (scalePercent / 100));

	return new Size(width, height);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Scale(size As Size, scalePercent As Single) As Size
	If Math.Abs(scalePercent - 1) < Single.Epsilon Then
		Return size
	End If

	Dim height = CInt(size.Height * (scalePercent / 100))
	Dim width = CInt(size.Width * (scalePercent / 100))

	Return New Size(width, height)
End Function

Mid, Left, Right Extension Methods for C# and VB.NET

Here are three Extension Methods which let you use the Left, Mid and Right Methods from VB6 in C# and VB.NET. The Extension Methods simply wrap the on-board .NET Framework Method Substring. These Extension Methods are now part of the Fesslersoft.Extensions.

Samples

C# Sample

VB.NET Sample

Compatibility (as Extension method):
working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5
Compatibility (Non-Extension method):
working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links: