[c#] Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Not in the Traditional Sense... Convert to Json, then to your object, and boom, done! Jesse above had the answer posted first, but didn't use these extension methods which make the process so much easier. Create a couple of extension methods:

    public static string ConvertToJson<T>(this T obj)
    {
        return JsonConvert.SerializeObject(obj);
    }
    public static T ConvertToObject<T>(this string json)
    {
        if (string.IsNullOrEmpty(json))
        {
            return Activator.CreateInstance<T>();
        }
        return JsonConvert.DeserializeObject<T>(json);
    }

Put them in your toolbox forever, then you can always do this:

var derivedClass = baseClass.ConvertToJson().ConvertToObject<derivedClass>();

Ah, the power of JSON.

There are a couple of gotchas with this approach: We really are creating a new object, not casting, which may or may not matter. Private fields will not be transferred, constructors with parameters won't be called, etc. It is possible that some child json won't be assigned. Streams are not innately handled by JsonConvert. However, if our class doesn't rely on private fields and constructors, this is a very effective method of moving data from class to class without mapping and calling constructors, which is the main reason why we want to cast in the first place.

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to casting

Subtracting 1 day from a timestamp date Cast object to interface in TypeScript TypeScript enum to object array Casting a number to a string in TypeScript Hive cast string to date dd-MM-yyyy Casting int to bool in C/C++ Swift double to string No function matches the given name and argument types C convert floating point to int PostgreSQL : cast string to date DD/MM/YYYY

Examples related to derived-class

Calling the base class constructor from the derived class constructor Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Examples related to base-class

Creating a singleton in Python Is it possible to assign a base class object to a derived class reference with an explicit typecast? Does delete on a pointer to a subclass call the base class destructor? Interface vs Base class

Examples related to downcast

What is the difference between up-casting and down-casting with respect to class variable Convert base class to derived class downcast and upcast Is it possible to assign a base class object to a derived class reference with an explicit typecast?