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;
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With