-1

For example, say you have

class Item { }
class Book: Item { }

and you have an object declaration such as

Item myBook = new Book();

you could cast it to the subclass with the code

Book myBookCasted = myBook as Book;

What I would like to know is if there is a method, probably with reflection, to cast the instance to the subclass with a string instead of the type name? Something like:

Book myBookCasted = myBook as "Book";

But with the correct syntax of course.

Thanks.

Chris
  • 21
  • 2
  • 1
    Then how would you determine the variable type (on the left hand side)? You can do something similar via Reflection, but that gets a lot more verbose than what you want – UnholySheep Dec 03 '20 at 22:13
  • 1
    I agree with @UnholySheep. If you already have access to the type `Book`, why bother trying to write syntax such as `myBook as "Book"`? Perhaps this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), and there's more to the story. Why do you think you want such syntax? Is there a deeper issue you're trying to solve, and you think this approach is the way to do it? – Sean Skelly Dec 03 '20 at 22:20
  • Yes, firstly I can use var instead of the subclass name, secondly my app a game will have dynamic data stored as text files, so being able to cast to a specific subclass from the string name of the subclass from the file is a potential requirement. Actually this may also include an instantiation situation; Basically I want smarter RTTI then merely relying on a list of if statements "if (myvar is Book)". – Chris Dec 03 '20 at 23:00
  • Could you rewrite your answer to closer align with the way you intent to use this? You might have overlooked a pattern that you can use instead of ending up trying to solve this using reflection. – Nikolaj Dam Larsen Dec 04 '20 at 08:11
  • It sounds like you just want to serialise/deserialise data. – Charleh Dec 04 '20 at 08:35

1 Answers1

1

You could do something like this, using the dynamic keyword:

using System;
using System.Collections.Generic;
using System.Linq;

namespace MorphTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Item myBook = new Book();
            dynamic myBookCasted = Convert.ChangeType(myBook, Type.GetType("MorphTest.Book"));
            Console.WriteLine(myBookCasted.GetType()); // Prints "MorphTest.Book"
        }
    }

    public class Item { }
    public class Book: Item { }
}

The only things to keep in mind is, you must be using .NET 4.0 or higher and you must use the fully qualified name (e.g. instead of just "Book", it must be "MorphTest.Book". Obviously you'll change MorphTest to whatever namespace the class is in.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • "Could" someone use `dynamic` for this? Sure. "Should" someone? Probably not. Here's a good explanation for [when and when not to use `dynamic`](https://stackoverflow.com/a/2690837/3791245). The answer might want to point out some of the ideas in that link about what `dynamic` is really best used for. – Sean Skelly Dec 03 '20 at 22:35
  • 1
    @SeanSkelly I understand what `dynamic` should be used for. And to make it clear, I am not encouraging the OP to program like this. I am simply pointing out that there is a way to do it, to answer his question. Should it be used? Hell, no. – Icemanind Dec 03 '20 at 22:40
  • No worries - I didn't mean to imply _you_ didn't know what `dynamic` should be used for, but _future readers_ might not. The answer as written didn't acknowledge these details about `dynamic`, hence the link and suggestion to update the answer with that info. – Sean Skelly Dec 03 '20 at 23:05