Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make this extension method more generic?

I'm having a brain fart trying to make the following method more generic such that any List<T> can be passed in for the columnValues parameter. Here's what I have:

public static DataRow NewRow(this DataTable dataTable, List<string> columnValues)
{
    DataRow returnValue = dataTable.NewRow();

    while (columnValues.Count > returnValue.Table.Columns.Count)
    {
        returnValue.Table.Columns.Add();
    }

    returnValue.ItemArray = columnValues.ToArray();
    return returnValue;
}

I could change it to a List<object> and convert the original list prior to passing it to the method but I'm sure there is a better option :-)

Edit:

Frank's post made me rethink this. In most cases that source List<T> would be a List<object> since the column values will most likely be different types.

For my initial use a List<string> made sense because I was creating a dataset from a CSV parse which is all text at that point.

like image 271
Cory Charlton Avatar asked Feb 05 '10 17:02

Cory Charlton


People also ask

What is the correct way to define generic extension?

The expression Generic extension is a concept introduced in ASB 501 04. Unlike a traditional extension which is affiliated to an equipment position a generic extension is affiliated to a LIM, directory number and a terminal which enables implementation of features like free seating and Mobility.

Can we override extension method?

You can use extension methods to extend a class or interface, but not to override them.

What are extension methods explain with an example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

How do you extend a method in C#?

C# extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

What is extension method in Java?

An extension method is a feature of object-oriented-programming languages that allows methods defined outside of a class to be used with objects of that class (using the '. ' operator) as if they are part of the class. Many programming languages such as C#, Scala, Kotlin, and TypeScript have this feature.


3 Answers

Why not just use params object[]:

public static DataRow NewRow(this DataTable dataTable, params object[] objects)
{
    DataRow returnValue = dataTable.NewRow();

    while (objects.Length > returnValue.Table.Columns.Count)
    {
        returnValue.Table.Columns.Add();
    }

    returnValue.ItemArray = objects;
    return returnValue;
}

Then you can just call it like this:

myDataTable.NewRow(1,2,"hello");
like image 87
BFree Avatar answered Oct 13 '22 13:10

BFree


You're basically out of luck, because the Item Array of the DataRow is an array of objects, that is, ultimately you can only pass in list of objects.

If you put in a generic parameter of the list all items of the list would have to be of that type, which is highly unlikely to be useful.

Having said that, in order to get numerous columns, all with different types, you could change your extension method to accept an object into which you instantiate an anonymous type:

table.NewRow(new  { A = "Hello", B = 1, C = DateTime.Now })

With the aid to convert the anonymous type values to a string,object dictionary either by reflection or by a dynamic method it should be a fairly useful thing.

like image 44
flq Avatar answered Oct 13 '22 12:10

flq


What about

IEnumerable<object>

in connection with

columnValues.Select(x => x.ToString()).ToArray();
like image 31
Dario Avatar answered Oct 13 '22 12:10

Dario