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 5962345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:05:34+00:00 2026-05-22T19:05:34+00:00

I have a class Exporter which has a generic method which accepts an IEnumerable<T>

  • 0

I have a class Exporter which has a generic method which accepts an IEnumerable<T> and creates an export document by enumerating its property values using reflection:

  public class Exporter
  {
    public string Export<T>(IEnumerable<T> enumerable)
    {
      //Implementation omitted
    }
  }

Because of generic type inference, I can supply this with an anonymous collection type. Note the absence of the generic parameter in the method call below:

string fooString =
        new Exporter().Export(new List<Foo>()
                                {
                                  new Foo() {Name = "cats", NumberOfHams = 1},
                                  new Foo() {Name = "dogs", NumberOfHams = 8}
                                }
                       .Select(x => new { TwiceTheHams = x.NumberOfHams * 2 }));

We all love C#3. However, I would like to adapt this class so that I can enter more information about the columns in the export document, for example the width.

I have created a new method in the export class which looks like this:

public string Export<T>(IEnumerable<T> enumerable, IEnumerable<ColumnDetails<T>> columnDetails)
    {
      //Implementation omitted
    }

Ideally, the syntax would be like this, where foos is of type IEnumerable<Foo>:

fooString = new Exporter().Export(foos,
                                      new List<ColumnDetails<Foo>>
                                        {
                                          new ColumnDetails<Foo>(x => x.Name, 12),
                                          new ColumnDetails<Foo>(x => x.NumberOfHams, 4),
                                        });

However, when I call the new Export() overload as above, the generic type inference doesn’t seem to be clever enough to infer that the generic parameter T for the ColumnDetails<T> should be the same as the generic parameter T for the IEnumerable. This means that I must specify List<ColumnDetails<Foo>> as the parameter, and therefore I cannot use this with anonymous collections.

I’m really new to generics and type inference. Is what I’m trying to do possible? Do I need to restructure the code somehow?

Edit: this is what I cannot do, because Visual Studio needs the generic parameter for ColumnDetails, which I don’t have:

fooString = new Exporter().Export(foos.Select(x => new {TwiceTheHams = x.NumberOfHams * 2}),
                                          new List<ColumnDetails>
                                            {
                                              new ColumnDetails(x => x.TwiceTheHams, 12)
                                            });
  • 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-22T19:05:34+00:00Added an answer on May 22, 2026 at 7:05 pm

    How about something like this?

    The anonList lets you have a reference IEnumerable<anontype> and the T foo in CreateColumnDetails<T> lets the compiler infer what type T is, allowing you to construct the object with an anonymous type as the value of T

    class Program
    {
        public static ColumnDetails<T> CreateColumnDetails<T>(T foo, Func<T, object> func, int x)
        {
            return new ColumnDetails<T>(func, x);
        }
        static void Main(string[] args)
        {
            IEnumerable<Foo> foos = new List<Foo>();
            var anonList = foos.Select(x => new {TwiceTheHams = x.NumberOfHams*2});
            var fooString = new Exporter().Export(anonList,
                                          anonList.Select(y => CreateColumnDetails(y, z => z.TwiceTheHams, 12)));
        }
    }
    public class Exporter
    {
        public string Export<T>(IEnumerable<T> enumerable, IEnumerable<ColumnDetails<T>> columnDetails)
        {
            return string.Empty;
        }
    }
    
    public class ColumnDetails<T>
    {
        public ColumnDetails(Func<T, object> func, int x)
        {
    
        }
    }
    public class Foo
    {
        public string Name { get; set; }
        public string NumberOfHams { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class A with its inner class defined A1 and class B with
I have strange problem. I have class which behaves similar dropdown list. package test.view;
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with a member function that takes a default argument. struct Class
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
If I have class ObjA { public ObjB B; } class ObjB { public

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.