How to make a type explicitly castable to any enum











up vote
2
down vote

favorite












I have



struct MyType
{
public int value;
}


How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.



The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.



It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)



An example use case :



if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}


Another one,



if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}









share|improve this question




















  • 2




    Your MyType is no enum, just any value-type. Why should it be castable to enum?
    – HimBromBeere
    Nov 8 at 10:16








  • 3




    But why would you want to do this? int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.
    – René Vogt
    Nov 8 at 10:24






  • 1




    It sounds like you'd like to implement a generic operator. But that's not supported in c#.
    – René Vogt
    Nov 8 at 10:26






  • 1




    Castable to any enum? Including those which are byte based rather than int?
    – Damien_The_Unbeliever
    Nov 8 at 10:42






  • 2




    What exactly do you mean by "any enum"? You refer to all enums, e.g. MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?
    – HimBromBeere
    Nov 8 at 11:19















up vote
2
down vote

favorite












I have



struct MyType
{
public int value;
}


How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.



The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.



It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)



An example use case :



if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}


Another one,



if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}









share|improve this question




















  • 2




    Your MyType is no enum, just any value-type. Why should it be castable to enum?
    – HimBromBeere
    Nov 8 at 10:16








  • 3




    But why would you want to do this? int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.
    – René Vogt
    Nov 8 at 10:24






  • 1




    It sounds like you'd like to implement a generic operator. But that's not supported in c#.
    – René Vogt
    Nov 8 at 10:26






  • 1




    Castable to any enum? Including those which are byte based rather than int?
    – Damien_The_Unbeliever
    Nov 8 at 10:42






  • 2




    What exactly do you mean by "any enum"? You refer to all enums, e.g. MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?
    – HimBromBeere
    Nov 8 at 11:19













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have



struct MyType
{
public int value;
}


How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.



The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.



It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)



An example use case :



if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}


Another one,



if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}









share|improve this question















I have



struct MyType
{
public int value;
}


How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.



The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.



It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)



An example use case :



if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}


Another one,



if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 9:40

























asked Nov 8 at 10:13









5argon

1,23511430




1,23511430








  • 2




    Your MyType is no enum, just any value-type. Why should it be castable to enum?
    – HimBromBeere
    Nov 8 at 10:16








  • 3




    But why would you want to do this? int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.
    – René Vogt
    Nov 8 at 10:24






  • 1




    It sounds like you'd like to implement a generic operator. But that's not supported in c#.
    – René Vogt
    Nov 8 at 10:26






  • 1




    Castable to any enum? Including those which are byte based rather than int?
    – Damien_The_Unbeliever
    Nov 8 at 10:42






  • 2




    What exactly do you mean by "any enum"? You refer to all enums, e.g. MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?
    – HimBromBeere
    Nov 8 at 11:19














  • 2




    Your MyType is no enum, just any value-type. Why should it be castable to enum?
    – HimBromBeere
    Nov 8 at 10:16








  • 3




    But why would you want to do this? int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.
    – René Vogt
    Nov 8 at 10:24






  • 1




    It sounds like you'd like to implement a generic operator. But that's not supported in c#.
    – René Vogt
    Nov 8 at 10:26






  • 1




    Castable to any enum? Including those which are byte based rather than int?
    – Damien_The_Unbeliever
    Nov 8 at 10:42






  • 2




    What exactly do you mean by "any enum"? You refer to all enums, e.g. MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?
    – HimBromBeere
    Nov 8 at 11:19








2




2




Your MyType is no enum, just any value-type. Why should it be castable to enum?
– HimBromBeere
Nov 8 at 10:16






Your MyType is no enum, just any value-type. Why should it be castable to enum?
– HimBromBeere
Nov 8 at 10:16






3




3




But why would you want to do this? int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.
– René Vogt
Nov 8 at 10:24




But why would you want to do this? int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.
– René Vogt
Nov 8 at 10:24




1




1




It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26




It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26




1




1




Castable to any enum? Including those which are byte based rather than int?
– Damien_The_Unbeliever
Nov 8 at 10:42




Castable to any enum? Including those which are byte based rather than int?
– Damien_The_Unbeliever
Nov 8 at 10:42




2




2




What exactly do you mean by "any enum"? You refer to all enums, e.g. MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?
– HimBromBeere
Nov 8 at 11:19




What exactly do you mean by "any enum"? You refer to all enums, e.g. MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?
– HimBromBeere
Nov 8 at 11:19












2 Answers
2






active

oldest

votes

















up vote
4
down vote













If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:



static void Main(string args)
{
MyType myType;
myType.value = 1;
MyEnum myEnum = (MyEnum) myType;
Console.WriteLine(myEnum);
Console.ReadLine();
}

enum MyEnum
{
a = 0,
b = 1
}

struct MyType
{
public int value;

public static explicit operator MyEnum(MyType myType)
{
return (MyEnum)myType.value;
}
}


The output is:



b


If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:



public static class MyTypeExtensions
{
public static T ToAnyEnum<T>(this MyType myType)
{
return (T)Enum.ToObject(typeof(T), myType.value);
}
}


Then you can write:



MyType myType;
myType.value = 1;
MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();





share|improve this answer























  • docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
    – SazooCat
    Nov 8 at 10:21






  • 1




    MyEnum doesn't need to inherit from int
    – Antoine V
    Nov 8 at 10:23










  • You are right, I changed it...
    – Isma
    Nov 8 at 10:28






  • 1




    This converts to MyEnum, but not any enum.
    – marsze
    Nov 8 at 10:59










  • Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
    – Isma
    Nov 8 at 11:14




















up vote
1
down vote













It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:



var anyEnum = (AnyEnum)myType.value;


Another way would be using something like a ToEnum method maybe:



struct MyType
{
int value;

public T ToEnum<T>() where T : struct
{
return (T)Enum.ToObject(typeof(T), this.value);
}
}


Usage:



var anyEnum = myType.ToEnum<AnyEnum>();


You could also create an explicit cast to your value type:



public static explicit operator int(MyType myType)
{
return myType.value;
}


Then you can do:



var anyEnum = (AnyEnum)(int)myType;


I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.



Personal Note



I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205568%2fhow-to-make-a-type-explicitly-castable-to-any-enum%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:



    static void Main(string args)
    {
    MyType myType;
    myType.value = 1;
    MyEnum myEnum = (MyEnum) myType;
    Console.WriteLine(myEnum);
    Console.ReadLine();
    }

    enum MyEnum
    {
    a = 0,
    b = 1
    }

    struct MyType
    {
    public int value;

    public static explicit operator MyEnum(MyType myType)
    {
    return (MyEnum)myType.value;
    }
    }


    The output is:



    b


    If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:



    public static class MyTypeExtensions
    {
    public static T ToAnyEnum<T>(this MyType myType)
    {
    return (T)Enum.ToObject(typeof(T), myType.value);
    }
    }


    Then you can write:



    MyType myType;
    myType.value = 1;
    MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
    MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();





    share|improve this answer























    • docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
      – SazooCat
      Nov 8 at 10:21






    • 1




      MyEnum doesn't need to inherit from int
      – Antoine V
      Nov 8 at 10:23










    • You are right, I changed it...
      – Isma
      Nov 8 at 10:28






    • 1




      This converts to MyEnum, but not any enum.
      – marsze
      Nov 8 at 10:59










    • Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
      – Isma
      Nov 8 at 11:14

















    up vote
    4
    down vote













    If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:



    static void Main(string args)
    {
    MyType myType;
    myType.value = 1;
    MyEnum myEnum = (MyEnum) myType;
    Console.WriteLine(myEnum);
    Console.ReadLine();
    }

    enum MyEnum
    {
    a = 0,
    b = 1
    }

    struct MyType
    {
    public int value;

    public static explicit operator MyEnum(MyType myType)
    {
    return (MyEnum)myType.value;
    }
    }


    The output is:



    b


    If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:



    public static class MyTypeExtensions
    {
    public static T ToAnyEnum<T>(this MyType myType)
    {
    return (T)Enum.ToObject(typeof(T), myType.value);
    }
    }


    Then you can write:



    MyType myType;
    myType.value = 1;
    MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
    MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();





    share|improve this answer























    • docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
      – SazooCat
      Nov 8 at 10:21






    • 1




      MyEnum doesn't need to inherit from int
      – Antoine V
      Nov 8 at 10:23










    • You are right, I changed it...
      – Isma
      Nov 8 at 10:28






    • 1




      This converts to MyEnum, but not any enum.
      – marsze
      Nov 8 at 10:59










    • Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
      – Isma
      Nov 8 at 11:14















    up vote
    4
    down vote










    up vote
    4
    down vote









    If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:



    static void Main(string args)
    {
    MyType myType;
    myType.value = 1;
    MyEnum myEnum = (MyEnum) myType;
    Console.WriteLine(myEnum);
    Console.ReadLine();
    }

    enum MyEnum
    {
    a = 0,
    b = 1
    }

    struct MyType
    {
    public int value;

    public static explicit operator MyEnum(MyType myType)
    {
    return (MyEnum)myType.value;
    }
    }


    The output is:



    b


    If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:



    public static class MyTypeExtensions
    {
    public static T ToAnyEnum<T>(this MyType myType)
    {
    return (T)Enum.ToObject(typeof(T), myType.value);
    }
    }


    Then you can write:



    MyType myType;
    myType.value = 1;
    MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
    MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();





    share|improve this answer














    If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:



    static void Main(string args)
    {
    MyType myType;
    myType.value = 1;
    MyEnum myEnum = (MyEnum) myType;
    Console.WriteLine(myEnum);
    Console.ReadLine();
    }

    enum MyEnum
    {
    a = 0,
    b = 1
    }

    struct MyType
    {
    public int value;

    public static explicit operator MyEnum(MyType myType)
    {
    return (MyEnum)myType.value;
    }
    }


    The output is:



    b


    If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:



    public static class MyTypeExtensions
    {
    public static T ToAnyEnum<T>(this MyType myType)
    {
    return (T)Enum.ToObject(typeof(T), myType.value);
    }
    }


    Then you can write:



    MyType myType;
    myType.value = 1;
    MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
    MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 8 at 11:40

























    answered Nov 8 at 10:19









    Isma

    6,60541733




    6,60541733












    • docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
      – SazooCat
      Nov 8 at 10:21






    • 1




      MyEnum doesn't need to inherit from int
      – Antoine V
      Nov 8 at 10:23










    • You are right, I changed it...
      – Isma
      Nov 8 at 10:28






    • 1




      This converts to MyEnum, but not any enum.
      – marsze
      Nov 8 at 10:59










    • Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
      – Isma
      Nov 8 at 11:14




















    • docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
      – SazooCat
      Nov 8 at 10:21






    • 1




      MyEnum doesn't need to inherit from int
      – Antoine V
      Nov 8 at 10:23










    • You are right, I changed it...
      – Isma
      Nov 8 at 10:28






    • 1




      This converts to MyEnum, but not any enum.
      – marsze
      Nov 8 at 10:59










    • Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
      – Isma
      Nov 8 at 11:14


















    docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
    – SazooCat
    Nov 8 at 10:21




    docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
    – SazooCat
    Nov 8 at 10:21




    1




    1




    MyEnum doesn't need to inherit from int
    – Antoine V
    Nov 8 at 10:23




    MyEnum doesn't need to inherit from int
    – Antoine V
    Nov 8 at 10:23












    You are right, I changed it...
    – Isma
    Nov 8 at 10:28




    You are right, I changed it...
    – Isma
    Nov 8 at 10:28




    1




    1




    This converts to MyEnum, but not any enum.
    – marsze
    Nov 8 at 10:59




    This converts to MyEnum, but not any enum.
    – marsze
    Nov 8 at 10:59












    Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
    – Isma
    Nov 8 at 11:14






    Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
    – Isma
    Nov 8 at 11:14














    up vote
    1
    down vote













    It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:



    var anyEnum = (AnyEnum)myType.value;


    Another way would be using something like a ToEnum method maybe:



    struct MyType
    {
    int value;

    public T ToEnum<T>() where T : struct
    {
    return (T)Enum.ToObject(typeof(T), this.value);
    }
    }


    Usage:



    var anyEnum = myType.ToEnum<AnyEnum>();


    You could also create an explicit cast to your value type:



    public static explicit operator int(MyType myType)
    {
    return myType.value;
    }


    Then you can do:



    var anyEnum = (AnyEnum)(int)myType;


    I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.



    Personal Note



    I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.






    share|improve this answer



























      up vote
      1
      down vote













      It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:



      var anyEnum = (AnyEnum)myType.value;


      Another way would be using something like a ToEnum method maybe:



      struct MyType
      {
      int value;

      public T ToEnum<T>() where T : struct
      {
      return (T)Enum.ToObject(typeof(T), this.value);
      }
      }


      Usage:



      var anyEnum = myType.ToEnum<AnyEnum>();


      You could also create an explicit cast to your value type:



      public static explicit operator int(MyType myType)
      {
      return myType.value;
      }


      Then you can do:



      var anyEnum = (AnyEnum)(int)myType;


      I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.



      Personal Note



      I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:



        var anyEnum = (AnyEnum)myType.value;


        Another way would be using something like a ToEnum method maybe:



        struct MyType
        {
        int value;

        public T ToEnum<T>() where T : struct
        {
        return (T)Enum.ToObject(typeof(T), this.value);
        }
        }


        Usage:



        var anyEnum = myType.ToEnum<AnyEnum>();


        You could also create an explicit cast to your value type:



        public static explicit operator int(MyType myType)
        {
        return myType.value;
        }


        Then you can do:



        var anyEnum = (AnyEnum)(int)myType;


        I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.



        Personal Note



        I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.






        share|improve this answer














        It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:



        var anyEnum = (AnyEnum)myType.value;


        Another way would be using something like a ToEnum method maybe:



        struct MyType
        {
        int value;

        public T ToEnum<T>() where T : struct
        {
        return (T)Enum.ToObject(typeof(T), this.value);
        }
        }


        Usage:



        var anyEnum = myType.ToEnum<AnyEnum>();


        You could also create an explicit cast to your value type:



        public static explicit operator int(MyType myType)
        {
        return myType.value;
        }


        Then you can do:



        var anyEnum = (AnyEnum)(int)myType;


        I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.



        Personal Note



        I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 11:58

























        answered Nov 8 at 11:04









        marsze

        3,95631640




        3,95631640






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205568%2fhow-to-make-a-type-explicitly-castable-to-any-enum%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Landwehr

            Reims

            Schenkenzell