Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:06:53+00:00 2026-06-11T20:06:53+00:00

I like to send a generic type converter function to a method but I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:06:54+00:00Added an answer on June 11, 2026 at 8:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to send out an update of my apk, but i did
I would like to send a javascript array to be processed by a method
I am trying to make a generic Duplicate linq extension method. But I cannot
Say I have a generic method with multiple type constraints, this this: public static
I have a web service that looks like this [WebMethod] public int Import(System.Collections.Generic.List<Record> importRecords)
Is there a JSON library for Java that can handle something like the following
I'd like to send arbitrary strings (not paths or URLs) between two of my
I would like to send the message from HelloWorldLayer, and receive it in ScoreLayer,
I'd like to send an email for each row of a result set using
I would like to send a html string with a GET request like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.