C# feature request: implement interfaces on anonymous types

前端 未结 10 1584
走了就别回头了
走了就别回头了 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."

提交回复
热议问题