我出现了以下代码:
public static class FuncUtils
{
public static Func<T1, T3> Compose<T1, T2, T3> (Func<T1, T2> f1, Func<T2, T3> f2)
{
return a => f2(f1(a));
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说最大的谜团就是这个
返回 a => f2(f1(a));
你能解释一下它是如何工作的吗?
有没有办法为通用 Func<...> 的命名参数来支持智能感知?
例子:
Func<int, int, int> f1 = new Func<int, int, int>(
(a, b) => { return a + b }
);
f1(2, 3);
Run Code Online (Sandbox Code Playgroud)
我如何调用项目,以便TestAction写出"s.Hello"?现在我什么都不做,它跳过了"action = s .."这一行.
或者是另一种方法吗?由于我不想返回任何代码,我使用Action而不是Func
我刚开始使用Action.
public class Items
{
public string Hello { get; set; }
}
public class TestClass
{
public void TestAction(Action<Items> action)
{
action = s => Console.WriteLine(s.Hello);
}
public TestClass()
{
TestAction(b => b.Hello = "Hello world!");
}
}
Run Code Online (Sandbox Code Playgroud) 首先让我说我不确定这个问题的标题是否有意义,但我不确定如何说出我的问题.
我有一个定义为的类
public static class NaturalSort<T>
Run Code Online (Sandbox Code Playgroud)
这个类有一个方法
public static IEnumerable<T> Sort(IEnumerable<T> list, Func<T, String> field)
Run Code Online (Sandbox Code Playgroud)
基本上,它在某个列表上执行自然排序,给定一个Func,返回要排序的值.我一直在使用它来做任何我想做的事情.
通常我会做类似的事情
sorted = NaturalSort<Thing>.sort(itemList, item => item.StringValueToSortOn)
Run Code Online (Sandbox Code Playgroud)
现在我有一个案例,我想要排序的值不是项目的字段,而是调用某些方法
就像是
sorted = NaturalSort<Thing>.sort(itemList, item => getValue(item))
Run Code Online (Sandbox Code Playgroud)
现在如果我的getValue返回一个对象而不是一个字符串.我需要做一些条件逻辑来获取我的字符串值
sorted = NaturalSort<Thing>.sort(itemList, item => getValue(item).Something == null ? getValue(item).SomethingElse : getValue(item).SomeotherThing)
Run Code Online (Sandbox Code Playgroud)
这可以工作,除了调用getValue是昂贵的,我不想调用它3次.有没有什么方法可以在表达式中调用它一次?
将实现引用透明方法作为静态只读Func而不是简单地作为方法,是否有任何性能影响?我个人认为Func版本更具可读性,但传统方式可能更有效.
这个:
static readonly Func<DateTime, DateTime> TruncateDay =
date => date.AddHours(-date.Hour)
.AddMinutes(-date.Minute)
.AddSeconds(-date.Second)
.AddMilliseconds(-date.Millisecond);
static readonly Func<DateTime, DateTime> TruncateMonth =
date => TruncateDay(date).AddDays(1 - date.Day);
static readonly Func<DateTime, DateTime> TruncateYear =
date => TruncateMonth(date).AddMonths(1 - date.Month);
static readonly Func<DateTime, int> QuarterSwitch =
date => Switch(date.Month % 3, 0,
Case(1, 3),
Case(2, 4),
Case(0, 5));
Run Code Online (Sandbox Code Playgroud)
对此:
static DateTime TruncateDay (DateTime date)
{
return date.AddHours(-date.Hour)
.AddMinutes(-date.Minute)
.AddSeconds(-date.Second)
.AddMilliseconds(-date.Millisecond);
}
static DateTime TruncateMonth (DateTime date)
{
return TruncateDay(date).AddDays(1 - date.Day);
}
static DateTime …Run Code Online (Sandbox Code Playgroud) 平等比较如何适用于Func?我已将问题的复杂性降低到这些单元测试:
[Test]
public void Will_Pass()
{
Func<string> func = () => "key";
Assert.That(func, Is.EqualTo(func));
}
[Test]
public void Will_Fail()
{
Func<string> funcA = () => "key";
Func<string> funcB = () => "key";
Assert.That(funcA, Is.EqualTo(funcB));
}
Run Code Online (Sandbox Code Playgroud)
我需要测试 - 并成功断言 - a的一个实例Func等于另一个实例.所以我基本上需要一种方法来使失败测试通过.
有没有办法在不创建自定义类型和覆盖的情况下执行此操作Equals()?
我尝试创建一个比较原始值和更新后的值的函数,并将原始值设置为更新后的值(如果不同)。该函数的作用更多,因此我简化了对主题的讨论:
public void Match<T>(Expression<Func<object>> original, Expression<Func<object>> updated)
{
var mex = original.Body as MemberExpression;
var funcOriginal = original.Compile();
var funcUpdated = updated.Compile();
var valueOriginal = funcOriginal();
var valueUpdated = funcUpdated();
if (valueOriginal != valueUpdated)
{
var info = mex.Member as PropertyInfo;
var target = ???; //How to get the original.TestProperty here?
info.SetValue(target, valueUpdated);
}
}
Run Code Online (Sandbox Code Playgroud)
我想这样打电话:
manager.Match<TestClass>(() => original.TestProperty, () => updated.TestProperty);
Run Code Online (Sandbox Code Playgroud) 我有很多方法需要从外部API获取数据并以相同的方式管理错误.
所以我想避免在所有方法中使用相同的代码......
我试图使用Func,我有这个代码工作的方法没有参数.
方法 :
private async Task<T> ExecuteAction<T>(Func<Task<T>> action) where T : class
{
var securityException = false;
object response = null;
try
{
response = await action();
}
catch (MySecurityException)
{
securityException = true;
}
if (securityException)
{
// new login
response = await action();
}
else if (// other condition)
{
//Do something
}
return (T) response;
}
Run Code Online (Sandbox Code Playgroud)
方法调用:
Func<Task<string>> action = myApiClient.Action;
var test = await ExecuteAction<string>(action);
Run Code Online (Sandbox Code Playgroud)
我怎么能用参数做同样的事情?就像是 :
Func<Task<string>> action = myApiClient.Action(parameter1, parameter2);
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来修改我的方法来添加参数?
说我有一个功能如何在记录上设置属性
public void SetProperty<TRecord, TEnum>(TRecord item,
Func<TRecord, TEnum> property, string enumValue )
where TEnum : struct
where TRecord : class
{
TEnum enumType;
if (Enum.TryParse(enumValue, true, out enumType))
{
//How Do I set this?
property.Invoke(item) = enumType;
}
}
Run Code Online (Sandbox Code Playgroud)
我不想把它换成表达式.有人知道如何设置属性吗?
func ×10
c# ×9
.net ×3
lambda ×3
action ×1
c ×1
equality ×1
expression ×1
generics ×1
methods ×1
nunit ×1
performance ×1
reflection ×1