IsSmallerThan generic extension method for C# and VB.NET

This is the IsSmallerThan generic extension method, this extension method which is part of the Fesslersoft.Extensions. It should only work with numeric values.

Samples

Sample C#

Sample VB.NET


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

Related links:

IsLargerThan generic extension method for C# and VB.NET

This is the IsLargerThan generic extension method, this extension method which is part of the Fesslersoft.Extensions. It should only work with numeric values.

Samples

Sample C#

Sample VB.NET

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

Related links:

Left, Right, Mid methods for C#

This snippet let’s you use the Left, Right, Mid methods known from VisualBasic in C# without referencing Microsoft.VisualBasic Namespace.

public static string Left(string input, int index)
{
	return input.Substring(0, index);
}
public static string Right(string input, int index)
{
	return input.Substring(input.Length - index, index);
}

public static string Mid(string input, int startingIndex, int endingIndex)
{
	return input.Substring(startingIndex, endingIndex);
}

public static string Mid(string input, int startingIndex)
{
	return input.Substring(startingIndex);
}

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

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

IsNull extension method for C# and VB.NET

You can use the IsNull Extension method by using the snippet below.

Sample C#

public static bool IsNull(this object source)
{
     return source == null;
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsNull(source As Object) As Boolean
	Return source Is Nothing
End Function

As always, this extension method has been added to the Github Fesslersoft.Extensions Repository

How to check if a number is a power of 2 in C# and VB.NET

To check if a number is a power of 2 in C# and VB.NET you can use the following extension method.

Sample C#

public static bool IsPowerOfTwo(this ulong input)
{
    return (input != 0) && ((input & (input - 1)) == 0);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsPowerOfTwo(input As ULong) As Boolean
	Return (input <> 0) AndAlso ((input And (input - 1)) = 0)
End Function

As always, the extension method was added to out Fesslersoft.Extensions library @Github

How to get the name of the calling method in C# and VB.NET

To get the name of the calling method in C# and VB.NET you can use the snippet below.

Sample C#

public static string GetCallingMethodName()
{
	var stackTrace = new StackTrace();
	return stackTrace.GetFrame(1).GetMethod().Name;
}

Sample VB.NET

Public Shared Function GetCallingMethodName() As String
	Dim stackTrace = New StackTrace()
	Return stackTrace.GetFrame(1).GetMethod().Name
End Function

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