Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:52:06+00:00 2026-05-24T18:52:06+00:00

I built a extension method for Enums (Enumerations) – , name it, say GetEnumSecondName

  • 0

I built a extension method for Enums (Enumerations) – , name it, say GetEnumSecondName

static string GetEnumSecondName(this Enum myEnumInstance)    {...}

Now, I have a generic method, that should take a Enumeration and return all the second names for that type.

List<string> GetSecondNames<T : ?T:Enum ? >()
{ 
  // ...

  foreach T member in GetAllMembers<T>()
    // should work only for Enum instances
    resultList.Add(member.GetEnumSecondName()); 

  // ...
}

Is there a workaround to do it?

Edit:

As I understood (thanks to Jon Skeet), C# does not support thins kind of constraint. If there are any VB.NET expert to confirm that “ordinary” VB.NET does not support it either. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:52:07+00:00Added an answer on May 24, 2026 at 6:52 pm

    Yes, there’s a workaround. You may not like it though. You have to rewrite the IL to express the constraint you want – because the CLR allows it, but C# doesn’t. (The compiler respects the constraint; it just doesn’t let you express it in C# code.)

    I have a project called Unconstrained Melody which does exactly this, introduced in a blog post.

    It’s regrettable that you can’t express this, and maybe it’ll be fixed in a future version of the language. For now, IL rewriting is all there is as far as I know.

    EDIT: I’ve just tried the constraint you’d want in VB:

    Foo(Of T As { System.Enum, Structure }) (...)
    

    And the compiler complains with:

    error BC32061: 'Enum' cannot be used as a type constraint.
    

    So no, you can’t do it in VB either. Oddly enough, the web page about that error doesn’t mention the restriction…

    EDIT: To anyone wanting to play with Unconstrained Melody, there are a few steps required to get it working:

    • You need to make sure you have an appropriate SDK directory as referred to by ConstraintChanger\Program.cs. In particular, check in \Program Files\Microsoft SDKs\Windows to see what version you’ve got – and change Program.cs appropriately
    • Critically, you need a directory called “Rewritten” at the top level (i.e. alongside lib)
    • If you’re using VS2010 you’ll need to go through the project upgrade at the start

    Once all of that is correct, you should just be able to hit Ctrl-Shift-B and get a working build. Do not remove and replace the project references – the test assembly needs to refer to the rewritten one, not the project it’s created from.

    I’ll attempt to address some of these issues tonight – and possibly even create a Nuget package…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to build extension method for IQuerable like this: public static IQueryable<T> FilterByString<T>(this
I am attempting to build a string extension method to trim a string to
I am looking for a built-in extension method like Apply: collection.Apply (predicate) which will
I created an extension method that uses the built-in ASP.NET serializer to serialize my
I built the following code to output XML: public static XDocument Serialize<T>(this T source)
I have the following extension method that I've built and have working for one
I have an extension method that looks like the following: //[MethodImpl(MethodImplOptions.NoOptimization)] public static IEnumerable<char>
I have tried to add an extension method to an enum which converts it
I could have sworn that there was an extension method already built for the
In C#, I'm trying to build an extension method for StringBuilder called AppendCollection() that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.