标签: func

C#执行给定方法的方法

我正在尝试编写以下内容:我想编写一个方法"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不能那样工作.有谁知道这样做的方法?

此致,基督徒

c# delegates func

5
推荐指数
1
解决办法
296
查看次数

将Linq和.ToList(),. Single()等作为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)

linq datacontext func c#-4.0

5
推荐指数
1
解决办法
225
查看次数

C# - 如何将引用传递给需要out变量的函数?

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# out func out-parameters

5
推荐指数
2
解决办法
2803
查看次数

Nullable Func <T,TResult>

在C#中可以传递一个可以为空的Func方法吗?

既不工作Func<A, bool>?也不Func?<A, bool>工作.

c# func

5
推荐指数
2
解决办法
4049
查看次数

反映MemberInfo到Func <T1,T2>

我正在寻找一种方法将实例转换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的回答解决了我的问题,无需进一步评论

c# reflection func

5
推荐指数
1
解决办法
641
查看次数

如何在try-catch块中正确放置语句?

我需要一个接一个地执行大量语句,并且我需要在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)

c# exception try-catch func

5
推荐指数
1
解决办法
271
查看次数

func如何正常工作

因此,当我返回一个对象时,我认为它将内存地址返回给您可以引用和使用的对象(或包含内存地址的对象).

但是当你返回一个函数时实际发生了什么?

您的应用如何知道该func使用哪个对象实例?

我的直觉告诉我一个对象实例引用与func一起传递但是实际发生了什么?

我似乎无法在这个主题上找到太多.

编辑:澄清一下,我问一个方法何时返回一个函数

c# func

5
推荐指数
2
解决办法
2093
查看次数

如何使用条件三元运算符分配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作业?

c# lambda func

5
推荐指数
1
解决办法
1314
查看次数

如何动态填充Func <T>字典

考虑以下:

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)

c# dictionary func

5
推荐指数
1
解决办法
191
查看次数

具有多个参数的Func方差

在我们的代码中尝试了类似的东西,但它失败了:

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>.存在显式转换(您是否错过了演员?)

知道为什么吗?

c# func covariance

5
推荐指数
1
解决办法
670
查看次数