Anonymous

My feedback

  1. 13 votes
    Vote
    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      You have left! (?) (thinking…)
      1 comment  ·  Visual Studio IDE » .NET  ·  Flag idea as inappropriate…  ·  Admin →
      Anonymous commented  · 

      public static class EnumX
      {
      /// <summary>Typ safe Enum.Parse</summary>
      public static E Parse<E>(string value)
      where E : struct,IComparable,IConvertible,IFormattable
      // where E : Enum required but invalid constraint
      { return (E)Enum.Parse(typeof(E), value);
      }
      }

      will do the job pretty well, except that you need to write
      var foo = EnumX.Parse<MyEnum>("EnumMember");

    • 165 votes
      Vote
      Sign in
      Check!
      (thinking…)
      Reset
      or sign in with
      • facebook
      • google
        Password icon
        Signed in as (Sign out)
        You have left! (?) (thinking…)
        12 comments  ·  Visual Studio IDE » Languages - C#  ·  Flag idea as inappropriate…  ·  Admin →
        Anonymous supported this idea  · 
      • 3,388 votes
        Vote
        Sign in
        Check!
        (thinking…)
        Reset
        or sign in with
        • facebook
        • google
          Password icon
          Signed in as (Sign out)
          You have left! (?) (thinking…)
          34 comments  ·  Visual Studio IDE » Languages - C#  ·  Flag idea as inappropriate…  ·  Admin →
          Anonymous commented  · 

          I do not think that this is a good idea. Although I already had similar problems it is only the tip of the iceberg.

          A constructor is basically a factory method. A factory method embedded in the class itself is usually a static method that returns a new object of the certain type.

          So the basic idea is the need for static methods that are provided by a generic type. This likely ends up with the request for static interface methods. See also http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2268973-virtual-static-method-in-interfaces-generics
          This would provide a solution not only for factory functions like the constructor.

          Unfortunately while the latter looks quite clear from the syntax point of view the implementation is not that easy. The runtime has to invoke different methods for different types but it cannot simply use callvirt as for normal interface methods. So there has to be a static dispatch table for each type (not instance) to call the methods without reflection.

        • 241 votes
          Vote
          Sign in
          Check!
          (thinking…)
          Reset
          or sign in with
          • facebook
          • google
            Password icon
            Signed in as (Sign out)
            You have left! (?) (thinking…)
            6 comments  ·  Visual Studio IDE » Languages - C#  ·  Flag idea as inappropriate…  ·  Admin →
            Anonymous commented  · 

            +1

            Let's give some examples where this would be particular useful:

            /// <summary>Typ safe Enum.Parse</summary>
            public static E Parse<E>(string value)
            where E : struct,IComparable,IConvertible,IFormattable
            // where E : Enum required but invalid constraint
            { return (E)Enum.Parse(typeof(E), value);
            }

            /// <summary>Get all enum values</summary>
            public static E[] GetValues<E>()
            where E : struct,IComparable,IConvertible,IFormattable
            // where E : Enum required but invalid constraint
            { return (E[])Enum.GetValues(typeof(E));
            }

            public static T CreateOpenInstanceDelegate<T>(this MethodInfo mi) where T : class
            // where T : MulticastDelegate
            { ... }

            /// <summary>Create Proxy to call constructor by delegate</summary>
            public static T CreateDelegate<T>(this ConstructorInfo ci) where T : class
            // where T : MulticastDelegate
            { ... }

            Anonymous supported this idea  · 

          Feedback and Knowledge Base