C# feature request: implement interfaces on anonymous types

前端 未结 10 1417
走了就别回头了
走了就别回头了 2021-02-19 06:57

I am wondering what it would take to make something like this work:

using System;

class Program
{
    static void Main()
    {
        var f = new IFoo { 
              


        
相关标签:
10条回答
  • 2021-02-19 07:02

    An anonymous type can't be made to do anything except to have read-only properties.

    Quoting the C# Programming Guide (Anonymous Types):

    "Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. An anonymous type cannot be cast to any interface or type except for object."

    0 讨论(0)
  • 2021-02-19 07:07

    You could have something like anonymous classes in Java:

    using System; 
    
    class Program { 
      static void Main() { 
        var f = new IFoo() {  
          public String Foo { get { return "foo"; } } 
          public void Print() { Console.WriteLine(Foo); }
        }; 
      } 
    } 
    
    interface IFoo { 
      String Foo { get; set; } 
      void Print(); 
    } 
    
    0 讨论(0)
  • 2021-02-19 07:10

    As long as we're putting out an interface wish list, I'd really like to be able to tell the compiler that a class implements an interface outside the class definition- even in a separate assembly.

    For example, let's say I'm working on a program to extract files from different archive formats. I want to be able to pull in existing implementations from different libraries — say, SharpZipLib and a commercial PGP implementation — and consume both libraries using the same code without creating new classes. Then I could use types from either source in generic constraints, for example.

    Another use would be telling the compiler that System.Xml.Serialization.XmlSerializer implements the System.Runtime.Serialization.IFormatter interface (it already does, but the compiler doesn't know it).

    This could be used to implement your request as well, just not automatically. You'd still have to explicitly tell the compiler about it. Not sure how the syntax would look, because you'd still have to manually map methods and properties somewhere, which means a lot of verbiage. Maybe something similar to extension methods.

    0 讨论(0)
  • 2021-02-19 07:11

    This wouldn't be possible currently.

    What would be the difference between this and simply making IFoo a concrete class instead? Seems like that might be the better option.

    What it would take? A new compiler and tons of checks to ensure they didn't break the other features. Personally, I think it'd just be easier to require developers to just create a concrete version of their class.

    0 讨论(0)
  • 2021-02-19 07:12

    Wouldn't this be cool. Inline anonymous class:

    List<Student>.Distinct(new IEqualityComparer<Student>() 
    { 
        public override bool Equals(Student x, Student y)
        {
            return x.Id == y.Id;
        }
    
        public override int GetHashCode(Student obj)
        {
            return obj.Id.GetHashCode();
        }
    })
    
    0 讨论(0)
  • 2021-02-19 07:12

    Interesting idea, I'd be a little concerned that even if it could be done it might get confusing. E.g. when defining a property with non-trivial setters and getters, or how to disambiguate Foo if the the declaring type also contained a property called Foo.

    I wonder if this would be easier in a more dynamic language, or even with the dynamic type and DLR in C# 4.0?

    Perhaps today in C# some of the intent could be achieved with lambdas:

    void Main() {
        var foo = new Foo();
        foo.Bar = "bar";
        foo.Print = () => Console.WriteLine(foo.Bar);
        foo.Print();
    }
    
    
    class Foo : IFoo {
        public String Bar { get; set; }    
        public Action Print {get;set;}
    }
    
    0 讨论(0)
提交回复
热议问题