Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to create custom extension methods for VS UT Assert class?

I would like to know what is the best way to write custom extensions methods for Microsoft Visual Studio Unit Testing Assert class.

like image 718
Thurein Avatar asked Aug 11 '11 03:08

Thurein


People also ask

How do you create a extension method?

To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example. The IntExtensions class will contain all the extension methods applicable to int data type.

How do you implement and call a custom extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

What is extension method in C# and how?

In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.

What is extension method in .NET core?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.


1 Answers

If you are referring to this Assert class, then you cannot add extension methods. Extension methods can only be applied to object instances. Since this class is static, it can never be instantiated.

You could add your own custom Assert type class like so though:

public static class MyAssert {
    public static void AreEqual(object expected, object actual) {
        // TODO: throw if not equal
    }
}
like image 64
CodeNaked Avatar answered Sep 21 '22 11:09

CodeNaked