Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

func

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:

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:

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.

like image 639
joeriks Avatar asked Sep 25 '12 08:09

joeriks


People also ask

How do you pass a generic type as parameter TypeScript?

Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.

How do you pass a generic object in Java?

Generics Work Only with Reference Types: When we declare an instance of a generic type, the type argument passed to the type parameter must be a reference type. We cannot use primitive data types like int, char. Test<int> obj = new Test<int>(20);

Does GoLand support generics?

At the moment, GoLand does not support running generics code based on the . go2 file format. So, we'll have to configure an External Tool to make this work.

What is a generic type parameter?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.


1 Answers

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.

like image 67
Lawrence Wagerfield Avatar answered Oct 14 '22 05:10

Lawrence Wagerfield