Asp.net 将泛型委托设置为类级别变量
在这方面,我遇到了另外一个问题: 根据上述解决方案,建议如下:Asp.net 将泛型委托设置为类级别变量,asp.net,c#-3.0,delegates,Asp.net,C# 3.0,Delegates,在这方面,我遇到了另外一个问题: 根据上述解决方案,建议如下: class Example { //the delegate declaration public delegate IEnumerable<T> GetGridDataSource<T>(); //the generic method used to call the method public void someMethod<T>(GetGridDataSou
class Example
{
//the delegate declaration
public delegate IEnumerable<T> GetGridDataSource<T>();
//the generic method used to call the method
public void someMethod<T>(GetGridDataSource<T> method)
{
method();
}
//a method to pass to "someMethod<T>"
private IEnumerable<string> methodBeingCalled()
{
return Enumerable.Empty<string>();
}
//our main program look
static void Main(string[] args)
{
//create a new instance of our example
var myObject = new Example();
//invoke the method passing the method
myObject.someMethod<string>(myObject.methodBeingCalled);
}
}
类示例
{
//委托声明
公共委托IEnumerable GetGridDataSource();
//用于调用该方法的泛型方法
公共方法(GetGridDataSource方法)
{
方法();
}
//传递给“someMethod”的方法
私有IEnumerable方法被调用()
{
返回可枚举的.Empty();
}
//我们的主要节目外观
静态void Main(字符串[]参数)
{
//创建我们示例的新实例
var myObject=新示例();
//调用传递该方法的方法
myObject.someMethod(myObject.methodBeingCalled);
}
}
请注意,在someMethod中,调用了委托“method()”。是否仍然需要设置稍后调用的类级委托
即:
类示例{
//委托声明
公共委托IEnumerable GetGridDataSource();
//这会失败,因为从未提供T
私有GetGridDataSource getDS;
//用于调用该方法的泛型方法
公共方法(GetGridDataSource方法)
{
getDS=方法;
}
公共无效无热法(){
getDS();
}
}
您也需要将类型设置为泛型,或者在需要调用时使用普通的委托
并返回到正确的类型。不能只在泛型上下文之外使用t
,编译器会认为您试图引用名为t
的普通类型
换句话说,如果你想在两个不同的地方使用同一类型的
T
,你需要知道T
在类型中的某个地方是什么。。。如果类型不是泛型的,那么这些信息将在哪里存在呢?根据您要实现的目标以及您在设计中的灵活性,有很多选择。我试着涵盖那些我觉得最可能与你想做的事情相关的事情
非泛型类的单个实例中的多个T值
这基本上就是你想要的。但是,由于方法调用的通用性,您需要一个类级变量,该变量可以支持T
的任何可能值,并且在为委托存储值时需要知道T
因此,您可以使用字典
,也可以使用封装类级别变量和方法的嵌套类型,然后使用列表
然后需要根据所需类型查找相应的委托
class Example {
//the delegate declaration
public delegate IEnumerable<T> GetGridDataSource<T>();
//this works because T is provided
private Dictionary<Type, object> getDSMap;
//the generic method used to call the method
public void someMethod<T>(GetGridDataSource<T> method)
{
getDSMap[typeof(T)] = method;
}
//note, this call needs to know the type of T
public void anotherMethod<T>() {
object getDSObj = null;
if (this.getDSMap.TryGetValue(typeof(T), out getDSObj))
{
GetGridDataSource<T> getDS = getDSObj as GetGridDataSource<T>;
if (getDS != null)
getDS();
}
}
类示例{
//委托声明
公共委托IEnumerable GetGridDataSource();
//这是因为提供了T
私有字典getDSMap;
//用于调用该方法的泛型方法
公共方法(GetGridDataSource方法)
{
getDSMap[typeof(T)]=方法;
}
//注意,此调用需要知道T的类型
公共无效无热法(){
对象getDSObj=null;
if(this.getDSMap.TryGetValue(typeof(T),out getDSObj))
{
GetGridDataSource getDS=getDSObj作为GetGridDataSource;
if(getDS!=null)
getDS();
}
}
非泛型类的单个实例中的单个T值
在这种情况下,您可以将委托实例存储在非类型化委托中,然后在需要时将其转换为适当的类型,并且您知道T的值。当然,您需要在首次创建委托时知道T,这首先就消除了对泛型方法或委托的需要
泛型类的多个实例中的多个T值
在这里,您可以使您的父类成为泛型类,并预先提供T。这样,您的示例就可以正常工作,因为从一开始就知道T的类型
class Example<T> {
//the delegate declaration
public delegate IEnumerable<T> GetGridDataSource<T>();
//this works because T is provided
private GetGridDataSource<T> getDS;
//the generic method used to call the method
public void someMethod<T>(GetGridDataSource<T> method)
{
getDS = method;
}
public void anotherMethod() {
if (getDS != null)
getDS();
}
}
类示例{
//委托声明
公共委托IEnumerable GetGridDataSource();
//这是因为提供了T
私有GetGridDataSource getDS;
//用于调用该方法的泛型方法
公共方法(GetGridDataSource方法)
{
getDS=方法;
}
公共无效无热法(){
if(getDS!=null)
getDS();
}
}
class Example<T> {
//the delegate declaration
public delegate IEnumerable<T> GetGridDataSource<T>();
//this works because T is provided
private GetGridDataSource<T> getDS;
//the generic method used to call the method
public void someMethod<T>(GetGridDataSource<T> method)
{
getDS = method;
}
public void anotherMethod() {
if (getDS != null)
getDS();
}
}