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

  • Home
  • SEARCH
  • 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 126941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:23:51+00:00 2026-05-11T05:23:51+00:00

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

  • 0

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

using System;  class Program {     static void Main()     {         var f = new IFoo {                      Foo = 'foo',                     Print = () => Console.WriteLine(Foo)             };     } }  interface IFoo {     String Foo { get; set; }     void Print(); } 

The anonymous type created would look something like this:

internal sealed class <>f__AnonymousType0<<Foo>j__TPar> : IFoo {     readonly <Foo>j__TPar <Foo>i__Field;      public <>f__AnonymousType0(<Foo>j__TPar Foo)     {         this.<Foo>i__Field = Foo;     }      public <Foo>j__TPar Foo     {         get { return this.<Foo>i__Field; }     }      public void Print()     {         Console.WriteLine(this.Foo);     } } 

Is there any reason that the compiler would be unable to do something like this? Even for non-void methods or methods that take parameters the compiler should be able to infer the types from the interface declaration.

Disclaimer: While I do realize that this is not currently possible and it would make more sense to simply create a concrete class in this instance I am more interested in the theoretical aspects of this.

  • 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. 2026-05-11T05:23:52+00:00Added an answer on May 11, 2026 at 5:23 am

    There would be a few issues with overloaded members, indexers, and explicit interface implementations.

    However, you could probably define the syntax in a way that allows you to resolve those problems.

    Interestingly, you can get pretty close to what you want with C# 3.0 by writing a library. Basically, you could do this:

    Create<IFoo> (     new     {         Foo = 'foo',         Print = (Action)(() => Console.WriteLine(Foo))     } ); 

    Which is pretty close to what you want. The primary differences are a call to ‘Create’ instead of the ‘new’ keyword and the fact that you need to specify a delegate type.

    The declaration of ‘Create’ would look like this:

    T Create<T> (object o) { //... } 

    It would then use Reflection.Emit to generate an interface implementation dynamically at runtime.

    This syntax, however, does have problems with explicit interface implementations and overloaded members, that you couldn’t resolve without changing the compiler.

    An alternative would be to use a collection initializer rather than an anonymous type. That would look like this:

    Create {     new Members<IFoo>     {         {'Print', ((IFoo @this)=>Console.WriteLine(Foo))},         {'Foo', 'foo'}     } } 

    That would enable you to:

    1. Handle explicit interface implementation by specifying something like ‘IEnumerable.Current’ for the string parameter.
    2. Define Members.Add so that you don’t need to specify the delegate type in the initializer.

    You would need to do a few things to implement this:

    1. Writer a small parser for C# type names. This only requires ‘.’, ‘[]’, ‘<>’,ID, and the primitive type names, so you could probably do that in a few hours
    2. Implement a cache so that you only generate a single class for each unique interface
    3. Implement the Reflection.Emit code gen. This would probably take about 2 days at the most.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if something exists (in Java world) able to take an snapshot
I was wondering what would be the best strategy to implement a badges system
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
I am wondering how you would approach this problem I have two Taxrates that
I am wondering how do I make pages automatically use https? Like if a
I provide a web service to my organisation. i was wondering would anyone recommeding
I'm wondering what would be the best prectice regarding mainataining connections to the database
As a follow-up to another question , I was wondering what would be the
I'm wondering how you would go about designing a good permgen space string in
I was wondering if it would be possible to retrieve the complete list of

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.