简体   繁体   English

如何使用泛型类型参数传入func?

[英]How can I pass in a func with a generic type parameter?

I like to send a generic type converter function to a method but I can't figure out how to do it. 我喜欢将泛型类型转换器函数发送到方法,但我无法弄清楚如何做到这一点。

Here's invalid syntax that explains what I like to achieve, the problem is I don't know how to specify the generic type together with my func: 这是解释我想要实现的内容的无效语法,问题是我不知道如何与我的func一起指定泛型类型:

public void SomeUtility(Func<T><object,T> converter)
{
    var myType = converter<MyType>("foo");
}

Edit (see also my discussion in the comments with Lawrence) : By "generic type converter" I meant I would like to pass in a converter that can convert to any strong type <T> (not object), so the next line in my method could be: 编辑(参见我在Lawrence评论中的讨论):通过“泛型转换器”我的意思是我想传入一个可以转换为任何强类型<T>(不是对象)的转换器,所以我的下一行方法可能是:

var myOtherType = converter<MyOtherType>("foo");

The delegate I like to pass as a parameter would look something like this: 我想作为参数传递的委托看起来像这样:

private delegate TOutput myConverterDelegate<TOutput>(object objectToConvert);

This is more a syntax / C# exploration now, to get things done I will probably use an interface instead, but I do hope this is possible to accomplish with a func/delegate. 这更像是一种语法/ C#探索,为了完成任务,我可能会使用一个接口,但我希望这可以通过func / delegate来实现。

You cannot have instances of generic functions or actions - all type parameters are defined upfront and cannot be redefined by the caller. 您不能拥有通用函数或操作的实例 - 所有类型参数都是预先定义的,并且不能由调用者重新定义。

An easy way would be to avoid polymorphism altogether by relying on down-casting: 一种简单的方法是依靠向下转换来完全避免多态性:

public void SomeUtility(Func<Type, object, object> converter)
{
    var myType = (MyType)converter(typeof(MyType), "foo");
}

If you want type safety, you need to defer the definition of the type parameters to the caller. 如果需要类型安全性,则需要将类型参数的定义推迟给调用者。 You can do this by composing a generic method within an interface: 您可以通过在界面中组合通用方法来完成此操作:

public void SomeUtility(IConverter converter)
{
    var myType = converter.Convert<MyType>("foo");
}

interface IConverter
{
   T Convert<T>(object obj);
}

Edit: 编辑:

If the 'converter type' is known at the call-site, and only this type will be used inside the utility method, then you can define a generic type on the method and use that, just like other posters have suggested. 如果在调用站点已知'转换器类型',并且在实用程序方法中只使用此类型,那么您可以在方法上定义泛型类型并使用它,就像其他海报所建议的那样。

public void SomeUtility<T>(Func<object, T> converter)
{
    var myType = converter("foo");
}

and then: 然后:

SomeUtility(arg => new MyType());

The generic type inference will work in this case. 在这种情况下,泛型类型推断将起作用。

You need to make SomeUtility generic as well. 您还需要使SomeUtility通用的。 Doing this and fixing the syntax gives: 这样做并修复语法给出:

public void SomeUtility<T>(Func<object,T> converter)
{
    var myType = converter("foo");
}

You have to know the T type at compile-time to use it. 你必须在编译时知道T类型才能使用它。 The T can either be specified at class-level or at method-level. T可以在类级别或方法级别指定。

class SomeClass<T> {
    public void SomeUtility(Func<object, T> converter) {
        var myType = converter("foo"); // Already is the T-type that you specified.
    }
}

or 要么

public void SomeUtility<T>(Func<object, T> converter) {
    var myType = converter("foo"); // Already is the T-type that you specified.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM