Extension Method On Casted Type

Moo-Juice

I have a simple enum, that looks something like the following:

public enum Brand
{
    [Description("Friendly Brand Name")]
    Brand1,

    [Description("Once Again")]
    Brand2
} // eo enum Brand

I have an extension method with the following signature:

public static string ToDescription(this Enum self) { /* .. implementation .. */ }

A quick check in LINQPad shows me that:

Brand brand = Brand.Brand1;
brand.ToDescription().Dump();

... all works as intended.

Now comes the fun part. In my code, at this point I want to iterate through the values of an arbituary enum (in this case Brand), and I've only got a System.Type to go on. First, I implemented a quick extension method for Array:

public static IEnumerable<object> AsEnumerable(this Array self)
{
    foreach(object o in self)
        yield return o;
} // eo AsEnumerable

Knowing that my type is an Enum, I iterate through the values thusly (where type is the actual Brand enum type) (Note: CastTo is just a shorthand extension method for Convert.ChangeType):

foreach (var enumValue in Enum.GetValues(type).AsEnumerable().Select((e) => e.CastTo(type)))
    Console.WriteLine(enumValue.ToDescription());

And I get the following runtime error:

'MyNameSpace.Brand' does not contain a definition for 'ToDescription'

Debugging, in the immediate window the type of enumValue is indeed Brand. I am guessing this may have something to do with the way extension methods work - or that I am missing something obvious. Perhaps there's a workaround?

King King

I think your CastTo just changes the type at runtime, why not use the Cast<T> instead?

foreach (var enumValue in Enum.GetValues(type).Cast<Enum>())
  Console.WriteLine(enumValue.ToDescription());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

typeof generic and casted type

From Dev

Extension Method with Generic Type

From Dev

Why address is type casted to char *?

From Dev

Type casted pointer constructor call

From Dev

Type casted pointer constructor call

From Dev

Call extension method on derived type

From Dev

Generic type vs Extension method

From Dev

Extension method for nested generic type

From Dev

Generic type vs Extension method

From Dev

Writing extension method for String type as ( String.Extension NOT "String".Extension)

From Dev

Why can't an incomplete type be casted to void?

From Dev

increment of void* type-casted as char* fails

From Dev

Why super classes generic type not erased/casted

From Dev

Is there a way to get the Generic Type from an Extension method?

From Dev

collection with type tagging not recognised in extension method

From Dev

How to create MVC Extension Method for Dynamic type

From Dev

How to create extension method for Enum type argument?

From Dev

Java Generics: Type Extension In Method Declaration Parameters

From Dev

Extension method with F# function type

From Dev

Intellisense cannot infer type from extension method

From Dev

Generic extension method can return type of IEnumerable?

From Dev

Method with generic type parameter inside Swift extension

From Dev

How to create extension method for Enum type argument?

From Dev

Extension method with F# function type

From Dev

Extension method to return datatypes based on object type

From Dev

Is it possible to write an extension method for a generic Enum type?

From Dev

Nullable nested type in extension method in C#

From Dev

Static color type initializer extension method

From Dev

How to work with Generic type members in extension method?

Related Related

  1. 1

    typeof generic and casted type

  2. 2

    Extension Method with Generic Type

  3. 3

    Why address is type casted to char *?

  4. 4

    Type casted pointer constructor call

  5. 5

    Type casted pointer constructor call

  6. 6

    Call extension method on derived type

  7. 7

    Generic type vs Extension method

  8. 8

    Extension method for nested generic type

  9. 9

    Generic type vs Extension method

  10. 10

    Writing extension method for String type as ( String.Extension NOT "String".Extension)

  11. 11

    Why can't an incomplete type be casted to void?

  12. 12

    increment of void* type-casted as char* fails

  13. 13

    Why super classes generic type not erased/casted

  14. 14

    Is there a way to get the Generic Type from an Extension method?

  15. 15

    collection with type tagging not recognised in extension method

  16. 16

    How to create MVC Extension Method for Dynamic type

  17. 17

    How to create extension method for Enum type argument?

  18. 18

    Java Generics: Type Extension In Method Declaration Parameters

  19. 19

    Extension method with F# function type

  20. 20

    Intellisense cannot infer type from extension method

  21. 21

    Generic extension method can return type of IEnumerable?

  22. 22

    Method with generic type parameter inside Swift extension

  23. 23

    How to create extension method for Enum type argument?

  24. 24

    Extension method with F# function type

  25. 25

    Extension method to return datatypes based on object type

  26. 26

    Is it possible to write an extension method for a generic Enum type?

  27. 27

    Nullable nested type in extension method in C#

  28. 28

    Static color type initializer extension method

  29. 29

    How to work with Generic type members in extension method?

HotTag

Archive