Jaquarh
Jaquarh

Reputation: 6683

Generic method cannot be accessed

I have currently been learning and researching Generic's inside C# but am struggling with actually using the method once created.

I have tried:

public class myTestClass
{
    class example
    {
        public static DataTable LINQtoDataTable<T>(IEnumerable<T> data)
        {
            DataTable dt = new DataTable();
            PropertyInfo[] objectProps = null; // Reflection

            if (data == null) return null;
            foreach (T record in data)
            {
                if (objectProps == null) objectProps = ((Type)data.GetType()).GetProperties();
                foreach (PropertyInfo pi in objectProps)
                {
                    Type columnType = pi.PropertyType;
                    if ((columnType.IsGenericType) && (columnType.GetGenericTypeDefinition() == typeof(Nullable<>))) columnType = columnType.GetGenericArguments()[0];
                    dt.Columns.Add(new DataColumn(pi.Name, columnType));
                }
            }
            return dt;
        }

    }

    example ex { get; set; }

    public myTestClass()
    {
        this.ex = new example();
    }
}

But when I do (In a C# Form):

// Namespace area
myTestClass test;
// Main Method
test = new myTestClass();

test.LINQtoDataTable() doesn't come up or exist. Can anyone please help me out? I am confused to why this won't appear since I public'd the method and instanced the class it is inside :(

Greatly appreciated & thanks in advance.

Upvotes: 0

Views: 73

Answers (3)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You are trying to create extension method, and for extension method there are some pre-requisites for the method to have, it should be static and in a static class, the one you are missing is this keyword and your class is not static in start of it:

public static DataTable LINQtoDataTable<T>(this IEnumerable<T> data)
{
     DataTable dt = new DataTable();
     PropertyInfo[] objectProps = null; // Reflection

     if (data == null) return null;
     foreach (T record in data)
     {
        if (objectProps == null) objectProps = ((Type)data.GetType()).GetProperties();
        foreach (PropertyInfo pi in objectProps)
        {
             Type columnType = pi.PropertyType;
             if ((columnType.IsGenericType) && (columnType.GetGenericTypeDefinition() == typeof(Nullable<>))) columnType = columnType.GetGenericArguments()[0];
                 dt.Columns.Add(new DataColumn(pi.Name, columnType));
        }
     }
     return dt;
}

and you will still now see it in intellisense, because you are creating extension method for IEnumerable<T> while you are trying to call it on just T.

For able to call it, you have to create a List<T> :

List<myTestClass> listTestClass = new List<myTestClass>();
listTestClass.Add(new myTestClass());
listTestClass.LINQtoDataTable();

I once wrote a blog post on extension methods topic, you may want to read about extension method with a simple examplehere

Upvotes: 3

dovid
dovid

Reputation: 6472

becuse the LINQtoDataTable metode it static, is not need an example instance. which locat in the ex propery, and accessible via its name.

myTestClass.example.LINQtoDataTable(...)

Upvotes: 2

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

An extension-method exists in a static public class whilst your current code has only a static method within a private class. So you need this:

 public static class MyTestClass {
     public static DataTable LINQtoDataTable<T>(this IEnumerable<T> data) { ... }
 }

Furthermore you need the this-keyword on the param you want to be that extension-method be bound to.

Last an extension-method can´t stay in a nested class, which you apparently don´t really need at all. Delete the nested class and make MyTestClass (also consider the naming-conventions for classes) publc and static and put the method there. Thus you won´t need any instance of this class. Simpy call myEnumerable.LINQtoDataTable().

Upvotes: 3

Related Questions