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 3309176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:36:34+00:00 2026-05-17T21:36:34+00:00

Firstly, I’ve asked this question elsewhere , but meta.stackoverflow.com seems to think that asking

  • 0

Firstly, I’ve asked this question elsewhere, but meta.stackoverflow.com seems to think that asking the same questions elsewhere is fine, so here goes. I’ll update both if/when I get an answer.

I’m doing a lot of conversion between simple types and I wanted to handle it as nicely as possible. What I thought would be nice would be to be able to do something like this:

var converter = GetMySingletonConverterRegistryPlease();
var somePoint = GetMeSomePointFromSomewhereThanks();

converter.Register<Point, List<int>>( 
  point => new List<int>{ point.X, point.Y } 
); 

var someInts = somePoint.To<List<int>>();

So, I came up with the following. Which I’m not at all happy with and would like some advice on (I’ll enumerate why I don’t like it after the code snippet)

public sealed class TypeConverterRegistry : TypeConverterRegistryBase {
  public static readonly TypeConverterRegistry Instance = new 
    TypeConverterRegistry();
  static TypeConverterRegistry() {}
  TypeConverterRegistry() {}
}

public abstract class TypeConverterRegistryBase {
  private readonly Dictionary<object, Delegate> _converters = new 
    Dictionary<object, Delegate>();

  public void Register<TFrom, TTo>( Func<TFrom, TTo> converter ) {
    var key = new { From = typeof( TFrom ), To = typeof( TTo ) };
    _converters[ key ] = converter;
  }

  public TTo ConvertTo<TTo>( object from ) {
    var key = new { From = from.GetType(), To = typeof( TTo ) };

    if( !_converters.ContainsKey( key ) ) 
      throw new KeyNotFoundException( 
        "No converter has been registered that takes a " + key.From + 
        " and returns a " + key.To );

    var converter = _converters[ key ];
    return (TTo) converter.DynamicInvoke( from );
  }
}

public static class Extensions {
  public static TTo To<TTo>( this object value ) {
    return TypeConverterRegistry.Instance.ConvertTo<TTo>( value );
  }
}  

I don’t like what I’ve done here, because it’s not strongly typed/constrained, it’s easy to accidentally misuse it and get a runtime exception, I’m using object as a catch all, and I also have a bad feeling about the way I’m calling DynamicInvoke and boxing the result. You guys will no doubt see even more problems than that! Also I feel bad about making an extension method on object.

What I do like is the resulting syntax!

So I’d really appreciate any advice, even if it’s just a nudge in the right direction.

Or reassurance that it’s not such a bad way to do it after all 😛

  • 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-05-17T21:36:34+00:00Added an answer on May 17, 2026 at 9:36 pm

    Take a look at AutoMapper (http://automapper.codeplex.com/). While different in intent, your project shares a lot of commonality and you may be able to get a lot from their patterns & code.

    In terms of runtime exceptions, this may come down to unit testing since you’re relying on the user registering the type before using it. There’s no way around that and it’s the same thing AutoMapper does but they provide some testing stuff in there too.

    I would highly recommend rewriting the ConvertTo method to be generic so that the from parameter is generic instead of type object. You could do that something like:

      public TTo ConvertTo<TFrom, TTo>( TFrom from ) {
        var key = new { From = typeof( TFrom ) To = typeof( TTo ) };
    
        if( !_converters.ContainsKey( key ) ) 
          throw new KeyNotFoundException( 
            "No converter has been registered that takes a " + key.From + 
            " and returns a " + key.To );
    
        var converter = _converters[ key ] as Func<TFrom, TTo>;
        return converter(from);
      }
    

    this would also solve your issue with the extension method. You’d now invoke using:

    public static TTo Convert<TFrom, TTo>(this TFrom value){
    return TypeConverterRegistry.Instance.Convert<TFrom, TTo>(value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I'm pretty new to C++. I believe that getline() isn't a standard C
Firstly, let me set out what I'd like to do. Assume I have three
Firstly, I'm a newbie to C# and SharePoint, (less than a month's experience) so
Firstly, Real World Haskell , which I am reading, says to never use foldl
So firstly here's my query: ( NOTE:I know SELECT * is bad practice I
I wish to order a table: Firstly by Field1=3 Then by Field2 DESC I
Firstly - my description ;) I've got a XmlHttpRequests JSON response from the server.
Firstly, bear with me here. I have a custom model binder which is successfully
Firstly, is there a way to use document.write() inside of JQuery's $(document).ready() method? If
Firstly anyone else trying to get onto forum.hibernate.org. I have been trying for a

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.