呼叫代表泛型 [英] call delegate generics

查看:78
本文介绍了呼叫代表泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public delegate void BindGtype <   int  > (T id); 

var PointGtypeMethod = new BindGtype < int > (BindGasTypes);
PointGtypeMethod(; ------------------------------------------- ---------->如何在这里调用?

public void BindGasTypes < int > (T UserID)
{
try
{
var ds = WeldService.GetUserSelectedGasTypeDetails(UserID);

gvGas.DataSource = ds;
gvGas.DataBind();
}
catch(Exception ex)
{}

}

解决方案

嗨anbujerimiah,



读完你的 - 非问题;-) - 再次。我想我猜你想知道什么。



所以你的代表定义应该是通用的(这是错误的......)

 public delegate void BindGtype< T>(T id); 





假设你有一个匹配你的通用方法委托类型 BindGtype

 void AMethod< T>(T id){} 





然后你可以创建一个这个方法的新函数指针,如下所示:

 var PointGtypeMethod = new BindGtype< int> (AMethod); 





你可以做同样的事情来指向一个已经专门化(非泛型)的方法

 void AnotherMethod(int id){} 
var PointGtypeMethod = new BindGtype< int>(AnotherMethod);





这有帮助吗?



亲切的问候



Johannes


public delegate void BindGtype<int>(T id);

 var PointGtypeMethod = new BindGtype<int>(BindGasTypes);
 PointGtypeMethod(;   -----------------------------------------------------> how to call here ?

  public void BindGasTypes<int>(T UserID)
    {
        try
        {
            var ds = WeldService.GetUserSelectedGasTypeDetails(UserID);

            gvGas.DataSource = ds;
            gvGas.DataBind();
        }
        catch (Exception ex)
        { }

    }

解决方案

Hi anbujerimiah,

After reading your - non question ;-) - again. I think I can guess what you want to know.

So your delegate Definition should be generic (it is wrong...)

public delegate void BindGtype<T>(T id);



Let's say you have a generic method matching your delegate type BindGtype

void AMethod<T>(T id) {}



Then you can create a new function pointer to this method like this:

var PointGtypeMethod = new BindGtype<int>(AMethod);



and you can do the same to point to an already "specialized" (non-generic) method

void AnotherMethod(int id) {}
var PointGtypeMethod = new BindGtype<int>(AnotherMethod);



Does this help?

Kind regards

Johannes


这篇关于呼叫代表泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆