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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:38:11+00:00 2026-05-17T15:38:11+00:00

Given the class: public class Foo { public string Name { get; set; }

  • 0

Given the class:

public class Foo
{
    public string Name { get; set; }
}

Is it possible to have a Foo instance created from a string through Convert.ChangeType:

Type type = typeof(Foo);
object value = "one";

value = System.Convert.ChangeType(value, type);

This is how a 3rd party API is attempting to rebuild objects. Someone mentioned this is possible with implicit operators, but from my understanding that will let me do the following, not create the object:

Foo foo = new Foo() { Name = "one" };
string fooAsString = foo;  // implicit conversion -- no cast needed

Is there a way to create the object this way? Also, I do have the ability to change the Convert.ChangeType if there is another way to do this.

Update:
The reason I am asking is because it throws and exception:

Invalid cast from ‘System.String’ to
‘JibbaJabba+Foo’.

and adding the operator did not resolve the issue.

  • 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-17T15:38:11+00:00Added an answer on May 17, 2026 at 3:38 pm

    According to the MSDN documentation:

    For the conversion to succeed, value
    must implement the IConvertible
    interface, because the method simply
    wraps a call to an appropriate
    IConvertible method. The method
    requires that conversion of value to
    conversionType be supported.

    Looking at the IConvertible interface, it has a ToType method. You could try that, maybe? (Disclaimer: I haven’t. It’s just a thought.)

    Edit: In your case, it seems that you want to convert from a string to a Foo. Since the string type (obviously) does not define a conversion to Foo in its IConvertible implementation, I believe you’re out of luck.


    Update: I don’t want to suggest that this is how you should always approach this sort of problem, but…

    I took a look at the code for Convert.ChangeType in Reflector. It’s long; I won’t reproduce it here. But basically it’s doing as the documentation says: it only works if:

    • The value parameter is a non-null instance of a type that implements IConvertible, or:
    • The type of the value parameter and the conversionType parameter are the same (so: Convert.ChangeType(myFoo, typeof(Foo)) would also work, though it’d be pretty useless).

    Then, it cycles through all the types supported by IConvertible (which obviously does not include any user-defined types) and ultimately uses ToType as a fallback.

    So, we need to look at the string type’s implementation of ToType.

    Sadly, it is one unfortunate line:

    return Convert.DefaultToType(this, type, provider);
    

    What does DefaultToType do? Exactly the same thing as ChangeType (minus the ToType fallback, obviously to avoid infinite recursion).

    So this just simply isn’t going to work.

    If you’re absolutely tied to this 3rd party library that’s using Convert.ChangeType behind the scenes, I would recommend contacting the library’s developer and asking them to extend their API in some way that will allow you to accomplish what you’re trying to accomplish. Some possiblities might be:

    • Accepting an optional Converter<string, T> or Func<string, T> delegate parameter, as suggested by Ben Voigt in a comment.
    • Accepting a TypeConverter parameter
    • Accepting a parameter of some type that implements an interface like IParser<T>

    Anyway, best of luck.

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

Sidebar

Related Questions

Given the following Model, public class A { public string Name { get; set;
I have something like this: public class Foo { public Bar[] Bars{get; set;} }
I have two objects and I want to merge them: public class Foo {
Given the following code... class Program { static void Main(string[] args) { Foo foo
Given a class with several constructors - how can I tell Resolve which constructor
Is there any way to have a strongly typed UpdateModel(myEntity, MagicStringPrefix) without the magic
I'm trying to do a query through NHibernate where the criterion for the result
C#, .NET 3.5 I am trying to get all of the properties of an
New to Subsonic 3.0 and wondering how to perform LIKE operators on object attributes.
I've got some library code that works on a range of .NET runtimes (regular,

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.