调用Func< T1,T2,T3>有可选参数? [英] Invoke Func<T1, T2, T3> which has optional parameters?

查看:196
本文介绍了调用Func< T1,T2,T3>有可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种方法有两个可选参数,一个ConfigSourceType枚举和一个表示filePath的字符串,并返回一个IConfigFile实例(根据配置文件的不同)。



我试图将这些方法传递给Func,以便我可以加载配置,但是我无法使用参数调用委托。如果我使用Type.Missing,它仍然抱怨ConfigSourceType枚举丢失。



编辑以包含代码:



DataHandler类:

  public IConfigFile RetrieveMyConfiguration(
ConfigurationSourceType source = ConfigurationSourceType.Live,
string fileName = )
{
/ *不相关* /
返回新的ConfigFile();
}

主要:

  private void SomeMethod()
{
var dHandler = new DataHandler();
MyConfig = LoadConfigFile(dHandler.RetrieveMyConfiguration);
}

public IConfigFile LoadConfigFile(
Func< ConfigurationSourceType,string,IConfigFile> func)
{
IConfigFile configFile;
//下面的行不编译:
configFile = func.Invoke(null,Type.Missing);
return configFile;
}


解决方案

参数到 Func< ...> ,但您可以为自己的代表。



这个 LINQPad 示例显示如何:

  void Main()
{
var t = new Test(XYZ);
t();
t(15);
}

public delegate void Test(int a = 10);

public void XYZ(int a)
{
a.Dump();
}

输出:

  10 
15

然而,这意味着它是代理的接收端,需要知道可选参数,但是您似乎希望为方法指定一个委托,并在发送端指定可选参数,或从您包装的方法继承可选参数代理人,除了创建匿名方法之外,这是不可能的:

  SomeMethod((a,b)=> WrappedMethod a,b,42)); 

你可以这样做:

  public delegate void MyDelegate(int a,int b,int?c = null,int?d = null); 

public void方法(int a,int b,int c = 10,int d = 42){...}

new MyDelegate((a,b,c ,d)=>
{
if(c.HasValue&& d.HasValue)
方法(a,b,c.Value,d.Value);
else if(c.HasValue)
方法(a,b,c.Value);
else if(d.HasValue)
方法(a,b,d:d.Value) ;
else
方法(a,b);
});

但这开始变得荒唐了。



所以不,你不能真的很容易做你想要的。


I have various methods which have two optional params, a ConfigSourceType enum and a string representing the filePath and return an instance of "IConfigFile" (Differs depending on the config file).

I'm trying to pass these methods as Func so that I can load configs, but I can't invoke the delegate with no parameters. If I use Type.Missing it still complains about the ConfigSourceType enum being missing.

Edit to include code:

DataHandler class:

    public IConfigFile RetrieveMyConfiguration(
        ConfigurationSourceType source = ConfigurationSourceType.Live,
        string fileName = "")
    {
         /* Irrelevant */
         return new ConfigFile();
    }

Main:

    private void SomeMethod()
    {  
        var dHandler = new DataHandler();
        MyConfig = LoadConfigFile(dHandler.RetrieveMyConfiguration);
    }

    public IConfigFile LoadConfigFile(
            Func<ConfigurationSourceType, string, IConfigFile> func)
    {
        IConfigFile configFile;        
        // Line below doesn't compile:
        configFile = func.Invoke(null, Type.Missing);
        return configFile;
     }

解决方案

You can't specify optional parameters to a Func<...>, but you can for your own delegates.

This LINQPad example shows how:

void Main()
{
    var t = new Test(XYZ);
    t();
    t(15);
}

public delegate void Test(int a = 10);

public void XYZ(int a)
{
    a.Dump();
}

Output:

10
15

This, however, means that it is the receiving end of a delegate that needs to know about the optional parameters, but it seems you want to specify a delegate for a method and specify optional parameters on the sending end, or inherit optional parameters from the methods that you wrap in the delegate, that's not possible other than to create an anonymous method:

SomeMethod((a, b) => WrappedMethod(a, b, 42));

You could do something like this:

public delegate void MyDelegate(int a, int b, int? c = null, int? d = null);

public void Method(int a, int b, int c = 10, int d = 42) { ... }

new MyDelegate((a, b, c, d) =>
{
    if (c.HasValue && d.HasValue)
        Method(a, b, c.Value, d.Value);
    else if (c.HasValue)
        Method(a, b, c.Value);
    else if (d.HasValue)
        Method(a, b, d: d.Value);
    else
        Method(a, b);
});

But this is starting to get ludicrous.

So no, you can't really easily do what you want.

这篇关于调用Func&lt; T1,T2,T3&gt;有可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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