private static K ExecuteStoredProcedure<K>(string connectionString, string storedProcedure, SqlParameter[] parameters, Func<I, K> readFunction)
Run Code Online (Sandbox Code Playgroud)
示例用法如下所示:
return ExecuteStoredProcedure<SqlDataReader, Dictionary<string, string>>(
connectionString,
"uspSearchStatisticsSelectByTokenPerDayGroupByDate",
parameters,
(reader) =>
{
reader.Read();
int readCount = reader.FieldCount;
Dictionary<string, string> results = new Dictionary<string, string>();
results.Add("FailedRequests", Convert.ToString(reader[5]));
results.Add("TotalRequests", Convert.ToString(reader[4]));
results.Add("AverageResponseTime", Convert.ToString(reader[3]));
return results;
}
);
Run Code Online (Sandbox Code Playgroud)
编译器回来了
错误520无法找到类型或命名空间名称"我"(您是否缺少using指令或程序集引用?)
但我认为它应该可以参考吗?
更新:我试图寻找太通用的东西而不是真的需要它.
我跟着以下......
private static K ExecuteStoredProcedure<K>(string connectionString, string storedProcedure, SqlParameter[] parameters, Func<SqlDataReader, K> readFunction)
我有(例如) a ,Func<int, int>我想像往常一样调用它,除了参数的类型为objectratingr than int。我只知道 Func 的确切类型和运行时的参数,因为 Func 是使用表达式树创建的,现在可以从变量访问dynamic。(简化)代码示例:
using System.Linq.Expressions;
namespace FuncExample
{
class Program
{
static void Main(string[] args)
{
object myFunc = CreateFunc(); // Could return something like
// Func<int, int>, but may return a
// completely different Func<> depending on
// arguments etc.
object result = getFromFunc(5, myFunc);
}
public static object CreateFunc()
{
LambdaExpression expr = Expression.Lambda(
/*
* Create an expression
*/
);
return expr.Compile();
} …Run Code Online (Sandbox Code Playgroud) 例如,我有一个func里面的方法:
private void Method()
{
Func<int, string> myFunc => (int) { return int.ToString();}
var res = myFunc(42);
}
Run Code Online (Sandbox Code Playgroud)
Func将被编译1次或每次调用方法时?
如果你有这样的话,也请分享一些链接,因为这是一种争论
我在代码中看到过这样的属性:
public static Func<string> TabMainDataToolTip
{
get
{
return (Func<string>) (() => "Main Data");
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:在某些地方,string从应用程序资源(本地化)中检索实际值.
返回的原因是什么Func<T>而不仅仅是类型T?它有什么优势吗?
我有搜索文件的代码.现在有Lambda Expression用于过滤.如何将Expression转换为Func<string>变量.谢谢
代码:
Directory.GetFiles(folder, "*" + KeyWord + "*").Where(f => formatFile.Contains(f.Split('.').Last().ToLower()));
Run Code Online (Sandbox Code Playgroud)
变量:
Func<string> Lambda = ?? (f => formatFile.Contains(f.Split('.').Last().ToLower())) \\ convert the Expression;
Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
private Func<Request, Response, byte[], string, string, Task> _started;
private Func<Request, Response, byte[], string, string, Task> _progress;
private Func<Request, Response, byte[], string, string, Task> _completed;
private Func<Request, Response, byte[], string, string, Task> _errorOccurred;
private Func<Request, Response, byte[], string, string, Task> _cancelled;
Run Code Online (Sandbox Code Playgroud)
我最好有类似的东西:
typedef Func<Request, Response, byte[], string, string, Task> StatusUpdateAsyncCallback; // in C++ way.
private StatusUpdateAsyncCallback _started;
// and so on.
Run Code Online (Sandbox Code Playgroud)
无法弄清楚如何用 Func 做到这一点。我习惯于委托(我没有这个问题,因为我可以为任何委托指定一个唯一的名称)但现在无法弄清楚如何为 Func 声明重复相同的内容。
我正在从委托类型的简单声明中迁移,delegate Task StatusUpdateAsyncCallback(Request req, Response resp, byte[] data, string account, string alias)因为我现在将依赖于一些特定于 …
我用谷歌搜索,但找不到满意的答案.我基本上试图让这段代码工作:
public List<WordEntry> WordDataBase = new List<WordEntry>();
public List<CharacterEntry> CharacterDataBase = new List<CharacterEntry>();
public List<Entry> SelectWhere<T>(System.Func<T, bool> predicate) where T : Entry
{
if (typeof(T) == typeof(WordEntry))
return WordDataBase.Where(predicate);
else if (typeof(T) == typeof(CharacterEntry))
return CharacterDataBase.Where(predicate);
else
return null;
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,WordEntry和CharacterEntry都是从Entry派生的.我得到编译器错误:
Error CS1503 Argument 2: cannot convert from 'System.Func<T, bool>' to 'System.Func<WordEntry, int, bool>'
Run Code Online (Sandbox Code Playgroud)
和
Error CS1503 Argument 2: cannot convert from 'System.Func<T, bool>' to 'System.Func<CharacterEntry, int, bool>'
Run Code Online (Sandbox Code Playgroud)
希望你能帮助我.提前致谢
我找到了以下代码片段,并想知道该Func<object> getObject属性的目的是什么:
public void InsertCacheItem(string key, Func<object> getObject, TimeSpan duration)
{
try
{
var value = getObject();
if (value == null)
return;
key = GetCacheKey(key);
_cache.Insert(
key,
value,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
duration,
System.Web.Caching.CacheItemPriority.Normal,
null);
}
catch { }
}
Run Code Online (Sandbox Code Playgroud)
如何通过传递Func属性来调用此特定函数?
我试图使用Func <>来平方列表的项目并返回平方项目列表.我应该在操作员的右边写些什么?我应该编写一个foreach循环来遍历列表中的项目吗?我是初学者.真的很困惑.
List<int> myintList = new List<int> { 1, 4, 5, 6 };
Func<List<int>, List<int>> SquareList = (m) =>
Run Code Online (Sandbox Code Playgroud) 我希望能够创建多个Funcs,每个 s 接受一个类型的实例并返回相同的类型,例如:
Func<Foo, Foo>
Func<Bar, Bar>
Run Code Online (Sandbox Code Playgroud)
然后将这些添加到 a List(或者可能添加到 a Dictionary,由Func处理的类型键控)。
然后给定任何实例y(其类型在编译时未知),我想检索并调用Func适用于 on 的实例y。
我所要求的甚至可能吗?