Tag Archives: .net

What are extension methods? How/When to use them?

Extension methods are methods that can be created extending existing types without the need to inherit from a class and creating your own custom logic. It can also be applied on interfaces.

2015-47-15 05-47-11-407

From MSDN:

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

That means we can extend methods from any type, even types where we don’t have access to the source code.

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

We build extension methods by declaring it as a static method, but there is a little tweak makes it compile into a method that can be called from an instance.
Continue reading

Using Reflection in .NET

Reflection uses the object of type Type that describes assemblies, modules, types…

Through reflection it is possible to do many different things, for example:

  • Dynamically create object instances
  • Dynamically invoke methods
  • Check if an object implements an interface
  • Manipulate attributes set on objects
  • Mock values for testing
  • Retrieve debug information

These are just a few things you can do with it. In this post I’m going to show a simple example on how to use it to get descriptions on enums.
Continue reading

The power of Linq

Linq is a powerful feature introduced on .NET 3.5 that allows you to easily query your objects to manipulate data.

Entity Framework uses Linq queries to generate database queries and retrieve the data for you. So you don’t have to worry about writing SQL anymore.

Linq also provides methods to work with XML among other things.

From MSDN:

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

Continue reading

Using MEF to setup Dependency Injection

Managed Extensibility Framework (MEF) is a built-in set of elements that allows you to “export” and “import” objects¬†across projects which allows you not to rely on hard dependencies.

From Microsoft:

The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.

Continue reading