我正在尝试编写以下内容:我想编写一个方法"A",它将另一个方法"B"作为参数,并为此方法B提供未知数量的参数.(params object [] args).现在,在方法A中,我想用参数args调用B. B现在将返回一个我希望A返回的对象.
这听起来有点奇怪,因此我将添加一些示例代码:
public object A(Func<object> B, params object[] args)
{
object x = B.Method.Invoke(args);
return x;
}
Run Code Online (Sandbox Code Playgroud)
问题是,Func不能那样工作.有谁知道这样做的方法?
此致,基督徒
我需要用一些重试策略逻辑包装一些Linq查询.
传递这个是安全的:
return WithRetry<User>(() =>
dataContext.Users.Where(u => u.UserID == userID).SingleOrDefault());
Run Code Online (Sandbox Code Playgroud)
对此:
public TResult WithRetry<TResult>(Func<TResult> methodCall)
{
// My Try/Catch Retry Code
}
Run Code Online (Sandbox Code Playgroud)
或者第一行应该像这样构造:
return WithRetry<User>(() =>
{
return dataContext.Users
.Where(u => u.UserID == userID)
.SingleOrDefault();
});
Run Code Online (Sandbox Code Playgroud) public class Foo
{
public void DoFoo()
{
int x;
var coll = TheFunc("bar", out x);
}
public Func<string, int, ICollection<string>> TheFunc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"参数2不应与'out'关键字一起传递."
public class Foo
{
public void DoFoo()
{
int x;
var coll = TheFunc("bar", out x);
}
public Func<string, out int, ICollection<string>> TheFunc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"无效的方差修饰符.只能将接口和委托类型参数指定为变体."
如何在此函数中获取out参数?
在C#中可以传递一个可以为空的Func方法吗?
既不工作Func<A, bool>?也不Func?<A, bool>工作.
我正在寻找一种方法将实例转换MemberInfo为"Func"类型(稍后通过lambda表达式使用它).
让我们说我有类型的成员函数
public bool func(int);
Run Code Online (Sandbox Code Playgroud)
使用反射,我以某种方式获得MemberInfo"mi"的实例,现在我想将其转换为Func<int, bool>;类型.就像是:
Func<int, bool f = myType.GetMember(mi.Name);
Run Code Online (Sandbox Code Playgroud)
有办法吗?
PS.Marc Grawell的回答解决了我的问题,无需进一步评论
我需要一个接一个地执行大量语句,并且我需要在sigle语句抛出异常时,程序流继续执行下一个语句,例如:
double a = Double.Parse("2.5");
double b = Double.Parse("ADFBBG");
Geometry g = Geometry.Parse("M150,0L75,200 225,200z");
Run Code Online (Sandbox Code Playgroud)
所有语句都必须执行,所以我需要一种级联的try-catch块:
double a, b;
Geometry g;
try
{
a = Double.Parse("2.5");
}
catch
{}
try
{
b = Double.Parse("ADFBBG");
}
catch
{}
try
{
g = Geometry.Parse("M150,0L75,200 225,200z");
}
catch
{}
Run Code Online (Sandbox Code Playgroud)
显然,这不是编写程序的最优雅方式.有更好的方法(更优雅,不会显着降低性能)?
我尝试Func<TResult>以这种方式使用委托:
我写了以下方法:
T Try<T>(Func<T> func)
{
try
{
return func();
}
catch
{
return default(T);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样使用它:
double x = Try(() => Double.Parse("77"));
Geometry g = Try(() => …Run Code Online (Sandbox Code Playgroud) 因此,当我返回一个对象时,我认为它将内存地址返回给您可以引用和使用的对象(或包含内存地址的对象).
但是当你返回一个函数时实际发生了什么?
您的应用如何知道该func使用哪个对象实例?
我的直觉告诉我一个对象实例引用与func一起传递但是实际发生了什么?
我似乎无法在这个主题上找到太多.
编辑:澄清一下,我问一个方法何时返回一个函数
我知道Func<>s不能通过var关键字直接隐式输入,尽管我希望我可以执行以下谓词赋值:
Func<Something, bool> filter = (someBooleanExpressionHere)
? x => x.SomeProp < 5
: x => x.SomeProp >= 5;
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误 cannot resolve the symbol, 'SomeProp'
目前,我已经采取了更加繁琐的if branch任务,这似乎并不优雅.
Func<Something, bool> filter;
if (someBooleanExpressionHere)
{
filter = x => x.SomeProp < 5;
}
else
{
filter = x => x.SomeProp >= 5;
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么,或者我是否需要坚持使用if-branch作业?
考虑以下:
public interface ISomething
{
string Something();
}
public class A : ISomething
{
public string Something()
{
return "A";
}
}
public class B : ISomething
{
public string Something()
{
return "B";
}
}
public class Helper
{
private static readonly Dictionary<string, Func<string, string>> Actions =
new Dictionary<string, Func<string, string>>(StringComparer.OrdinalIgnoreCase);
public Helper()
{
Actions["A"] = DoSomething<A>;
Actions["B"] = DoSomething<B>;
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ISomething)));
foreach (var type in types)
{
Console.WriteLine(type.Name);
// Actions[type.Name] = ????;
}
} …Run Code Online (Sandbox Code Playgroud) 在我们的代码中尝试了类似的东西,但它失败了:
Func<Employee, Employee> _myFunc;
void Main()
{
Func<Employee, Employee> test1 = _myFunc;//Ok
Func<Employee, Person> test2 = _myFunc;//Ok
Func<Person, Employee> test3 = _myFunc;//Fails
Func<Person, Person> test4 = _myFunc;//Fails
}
public class Person { }
public class Employee : Person { }
Run Code Online (Sandbox Code Playgroud)
最后两个案例给出了这个错误:
无法隐式转换
System.Func<Employee, Employee>为System.Func<Person, Employee>.存在显式转换(您是否错过了演员?)
知道为什么吗?
func ×10
c# ×9
c#-4.0 ×1
covariance ×1
datacontext ×1
delegates ×1
dictionary ×1
exception ×1
lambda ×1
linq ×1
out ×1
reflection ×1
try-catch ×1