Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension method for precisely two different types

I'm using the code below. It's designed for a certain type to limit it's popup-ness in intellisense etc.

public static Generic Get<Generic>(this Entity input)
{
  return (Generic)input;
}

Now I'd like to use the same Get method for another type (or, to be fully covered, a few another types but still a fix number of). So I added a second method and the code looks as follows.

public static Generic Get<Generic>(this Entity input)
{
  return (Generic)input;
}

public static Generic Get<Generic>(this Entity2 input)
{
  return (Generic)input;
}

It strikes me that a better approach would be to keep it in the same method body and still cover all the regarded types. Is there a syntax for including e.g. two different types in the signature? Something like this pseudo-code below.

public static Generic Get<Generic>(this [Entity, Entity2] input)
{
  return (Generic)input;
}

The best approach I can think of, as shown below, consists of a entry method for each type and the logic in a private place. It makes sense when the logic is extensive but looks kind of superfluous when it's only a line or two.

public static Generic Get<Generic>(this Entity input)
{
  return CommonLogic(input);
}

public static Generic Get<Generic>(this Entity2 input)
{
  return CommonLogic(input);
}

private static Generic CommonLogic(Object input)
{
  return (Generic)input;
}
like image 529
Konrad Viltersten Avatar asked Jun 03 '15 12:06

Konrad Viltersten


People also ask

What are extension methods explain with an example?

An extension method is actually a special kind of static method defined in a static class. 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.

What is correct extension method?

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.

What are extension methods in LINQ?

Extension Methods are a new feature in C# 3.0, and they're simply user-made pre-defined functions. An Extension Method enables us to add methods to existing types without creating a new derived type, recompiling, or modifying the original types.

What is extension method in OOP?

In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages.


1 Answers

C# doesn't support the [Entity, Entity2] notation, so that option is out.

If Entity and Entity2 share a common interface or base class, then declare it as:

public static Generic Get<Generic>(this IEntityCommon input)
{
    return (Generic)input;
}

If not and you have created Entity, Entity2 etc, then add a common interface. This interface need not define any methods and can be empty, it simply provides a common type for the extension method.

Failing all that, the "CommonLogic" way is your best option.

like image 162
David Arno Avatar answered Sep 27 '22 17:09

David Arno