0

I have a method -

public T GetField<T> (string tableName, string fieldName)
{
    //Code
}

I need a delegate to hold this method. Not sure how to declare delegate for above method.

Now if I change my method

public T GetField<T> (string tableName, string fieldName, params IDbDataParameter[] parameters)
{
    //Code
}

public T Func<string,string ,string,IDbDataParameter[],T> GetFieldAction = GetField;// This is wrong

Any suggestion? Is it possible? Can I have a delegate which can hold generic return type method?

Is there any Func() workaround?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ranjan Kumar
  • 497
  • 2
  • 8
  • 24
  • 2
    You can't without providing `T`. Then you can use `Func`. – Lee May 29 '14 at 12:44
  • Ok, now I have a params IDbDataParameter[] parameters function parameter. public T GetField (string tableName, string fieldName, params IDbDataParameter[] parameters). – Ranjan Kumar May 29 '14 at 13:01
  • 2
    @RanjanKumar: `delegate T MyDelegate(string tableName, string fieldName, params IDbDataParameter[] parameters);` should work – RobSiklos May 29 '14 at 13:09
  • @RobSiklos T here is for delegate and not for method that return T. – Ranjan Kumar May 30 '14 at 13:44

2 Answers2

4

Why not Func<string, string, T>?

Or, alternatively, if you don't want to use Func:

delegate T MyDelegate<T>(string tableName, string fieldName);

EDIT: Example code which addresses your comment:

delegate T MyDelegate<T>(string tableName, string fieldName, params IDbDataParameter[] parameters);

private T GetField<T>(string tableName, string fieldName, params IDbDataParameter[] parameters)
{
  return default(T);
}

void Main()
{
  MyDelegate<int> del = this.GetField<int>;
}
RobSiklos
  • 8,348
  • 5
  • 47
  • 77
  • 1
    Not my downvote, but at the time of downvote your answer was `Why not Func?` Someone might have felt that's kinda rude. – Sriram Sakthivel May 29 '14 at 12:55
  • You are hard coding Int in delegate initialization whereas I need something like MyDelegate del = this.GetField; – Ranjan Kumar May 29 '14 at 13:21
  • This cannot be done, unless there is already a `T` that you can use (e.g. you are in a generic method with a `T` parameter). What is the underlying problem you are trying to solve by trying to use such a delegate? – RobSiklos May 29 '14 at 13:26
  • You might be able to use `dynamic` to accomplish your goal, but this is a hack, and should probably be avoided. You're asking how to implement a solution to something, but you haven't actually said what problem you're trying to solve. Why do you need this delegate in the first place? – RobSiklos May 29 '14 at 13:38
2

Based on your second method if you want to call that using c# Func , here is the code below.

public class Stackoverflow<T> 
{
    public  static Func<string,string,IDbDataParameter[],T> CallingFunc; 

    public static T GetField(string tableName, string fieldName, params IDbDataParameter[] parameters)
    {
        //Code
        return default(T);
    }        

}

public class MainProgram
{
    static void Main(string[] args)
    {
        Stackoverflow<int>.CallingFunc = Stackoverflow<int>.GetField;
        Stackoverflow<int>.CallingFunc("SampleTableName", "SampleField", new[]{
            new SqlParameter{DbType = DbType.Int32,Size = sizeof(Int32),ParameterName = "Id",Direction = ParameterDirection.Input},
            new SqlParameter{DbType = DbType.String,Size = 100,ParameterName = "name",Direction = ParameterDirection.Output}
        });

    }

}

Main method can't be call under generic class, so it's reside in other class. This workaround also can be possible using generic delegates like below

public class Stackoverflow<T> 
{

    public  delegate T CallingDelegate(string tableName, string fieldName, params IDbDataParameter[] parameters); 

    public static T GetField(string tableName, string fieldName, params IDbDataParameter[] parameters)
    {
        //Code
        return default(T);
    }        

}

public class MainProgram
{
    static void Main(string[] args)
    {
        Stackoverflow<int>.CallingDelegate del = Stackoverflow<int>.GetField;
        del("SampleTableName", "SampleField", new[]{
            new SqlParameter{DbType = DbType.Int32,Size = sizeof(Int32),ParameterName = "Id",Direction = ParameterDirection.Input},
            new SqlParameter{DbType = DbType.String,Size = 100,ParameterName = "name",Direction = ParameterDirection.Output}
        });

    }

}
Humayoun_Kabir
  • 2,003
  • 1
  • 17
  • 13