简体   繁体   中英

How to write extension methods for anonymous types?

I'm trying to create a CSV extension method for my enumerable list and I'm stumped. Here's how I created my simple enumerated list:

var CAquery = from temp in CAtemp
 join casect in CAdb.sectors
 on temp.sector_code equals casect.sector_code
 select new
 {       
     CUSIP = temp.equity_cusip,
     CompName = temp.company_name,
     Exchange = temp.primary_exchange       
 };

CAquery.WriteToCSVFile();

This is what I have done so far in creating an extension method (which I think is wrong):

public static class CSVExtensions
{        
    public static void WriteToCSVFile(this IEnumerable<T> myList)
    {

Do you see what I'm doing wrong?

You have to specify the generic type parameter in the method signature:

public static class CSVExtensions
{        
    public static void WriteToCSVFile<T>(this IEnumerable<T> myList)
    {
       //your code here
    }
}

Are you truly trying to write an extension method that should work on any IEnumerable<T> or is your type more specific? If the later is the case you should replace T with the type you want to support (or add sufficient constraints).

Edit:

In light of comments - you should project to a class instead of an anonymous type in your query - then you can use an extension method for this particular type, ie:

class CompanyTicker
{
  public string CUSIP {get;set;}
  public string CompName  {get;set;}
  public string Exchange {get;set;}
}

Now your query can be:

var CAquery = from temp in CAtemp
 join casect in CAdb.sectors
 on temp.sector_code equals casect.sector_code
 select new CompanyTicker
 {       
     CUSIP = temp.equity_cusip,
     CompName = temp.company_name,
     Exchange = temp.primary_exchange       
 };

And your extension method (which now doesn't need to be generic) becomes:

public static class CSVExtensions
{        
    public static void WriteToCSVFile(this IEnumerable<CompanyTicker> myList)
    {
       //your code here
    }
}

It is possible to do what you are trying to do using reflection. The performance is going to be somewhat worse than if you write non-generic code however.

Here is a complete code sample:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        var seq =
        Enumerable.Range(0, 100)
            .Select(i => new { Name = "Item" + i, Value = i })
            ;

        seq.WriteCsv(Console.Out);
        Console.ReadLine();
    }
}

public static class CsvExtension
{

    public static void WriteCsv<T>(this IEnumerable<T> seq, TextWriter writer)
    {
        var type = typeof(T);

        MethodInfo[] getters = type.GetProperties().Select(pi => pi.GetGetMethod()).ToArray();


        // only supporting simple properties
        // indexer properties will probably fail
        var args = new object[0];

        foreach (var item in seq)
        {
            for (int i = 0; i < getters.Length; i++)
            {
                if (i != 0)
                    writer.Write(",");

                Object value = getters[i].Invoke(item, args);
                var str = value.ToString();

                if (str.Contains(",") || str.Contains("\""))
                {
                    var escaped = str.Replace("\"", "\\\"");
                    writer.Write("\"");
                    writer.Write(escaped);
                    writer.Write("\"");
                }
                else
                {
                    writer.Write(str);
                }
            }


            writer.WriteLine();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM