如何调用泛型方法 [英] How to call generic method

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

问题描述

如何调用此通用方法,如何发送参数

How to invoke this generic method,how to send parameters

public static IQueryable<t> Where<t>(this IQueryable<t> source, string predicate, params object[] values)
        {
            return (IQueryable<t>)Where((IQueryable)source, predicate, values);
        }


请告诉我...
感谢帮助.


plz tell me...
thanks for help.

推荐答案

让我们尝试看看我们是否可以在不知道任何进一步细节的情况下做到这一点……它必须类似于:
Let''s try and see if we can do it without knowing any further detail… It must be something like:
using // the namespace of your extension method declaration;
// alias or using fully-qualified method name won't work in this case

//...

class MyType {/* ... */} // this should instantiate the generic with a concrete type;
// as the "where" constrain is not there, it could be anything at all

IQueriable<MyType> thisParameter = //... // you need some class/structure implementing the interface,
// instantiate it to use for "this"

IQueriable<MyType> result = 
    thisParameter.Where<MyType>("some predicate", someObject, someOtherObject /* any number of objects */);



那是.我演示了如何正式进行呼叫,而不知道其含义.它是扩展方法,这使它有些棘手(但是为了理解,这是棘手的,因为这是相对较新的语法,有些不寻常;对于用法而言,不是棘手的).请参阅:
http://msdn.microsoft.com/en-us/library/bb383977%28v = vs.100%29.aspx [ ^ ].

该方法是通用的这一事实并不会改变太多.您应该实例化具有通用参数的所有内容,并带有一些具体的完整类型.

顺便说一句,我怀疑声明的语义是否正确或正确实现.例如,所谓的谓词"不能是字符串类型.根据定义,谓词是返回布尔值的方法,属性或运算符.例如,属性IsEnabled是谓词.

不要担心.更重要的是,您应该现在就知道.

—SA



That''s is. I demonstrated how to do a call formally, not knowing the semantic of it. It is the extension method, which makes is a bit tricky (but tricky just for understanding, because this is relatively new syntax, somewhat unusual; not tricky for the usage). Please see:
http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.100%29.aspx[^].

The fact that the method is generic does not change things much. You should just instantiate everything having a generic parameter with some concrete complete type.

By the way, I doubt the semantic of the declaration is correct or correctly implemented. For example, something called "predicate" cannot be of the string type. The predicate, by definition, is a method, property or operator returning Boolean. For example, the property IsEnabled is a predicate.

Not to worry. More importantly, you should get the idea by now.

—SA


这篇关于如何调用泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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