C# Is it possible to get actual type out of a string representing that type? [duplicate]
Asked Answered
S

1

0

I have string "Car", and I would like to get the type Car from it. My class Car is:

namespace MySolution.MyProjectA
{
    public class Car
    {
      ...
    }
}

I trying getting type like this but it returns null:

Type myType = Type.GetType("MySolution.MyProjectA.Car"); // returns null

Given a string variable representing my type (i.e. "Car"), how do I get its Type Car?

UPDATE AND SOLUTION

Thanks to suggestion from @AsafPala here (Type.GetType("namespace.a.b.ClassName") returns null), I got it to work. Turns out if you are calling GetType from another project in your solution (which is another assembly), you have to provide also assembly name.

Since MySolution has 2 project MyProjectA and MyProjectB, these are 2 separate projects (or assemblies) in same solution so you need to provide fully specified assembly name of your type (that is MySolution.MyProjectA.Car) and assembly name (that is MySolution.MyProjectA) as comma separated as below:

Type myType = Type.GetType("MySolution.MyProjectA.Car,MySolution.MyProjectA");

UPDATE AND SOLUTION

Since I am calling this code in another project (which is same as assembly), I need to provide fully specified type name and assembly name as comma separated like so:

namespace MySolution.MyProjectB
{
    public class MyClass
    {
        ...
        ...
        public void MyMethod()
        {
             // this wont work, I get null back
             //Type myType = Type.GetType("MySolution.MyProjectA.Car");
             Type myType = Type.GetType("MySolution.MyProjectA.Car,MySolution.MyProjectA"); //this works
             ...
        }
    }
}
Salsify answered 14/2, 2019 at 0:11 Comment(4)
#1825647Misdemean
Please edit your existing question and add more details rather than asking a new one for the same question. Even this question does not show the pertinent details; you should show the namespace around the class (it may not be the same as "MySolution.MyProject").Grievance
@RufusL The namespace is correct and I added that info, too as you asked. The questions is as simple as it gets, not sure what else is not clear. Plus it is simplified version of previous one (so I am not asking it again) in a way that I focus only on one thing which is getting the actual type Car out of string "Car".Salsify
Where are you calling GetType from? Your code works fine for me.Grievance
A
-3

Yes. You need to serialize the object to a string first. You can use json or xml as the serializer type.

Then on the other side you use the same serializer object to deserialize the string into an instance of your class.

https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

Account account = JsonConvert.DeserializeObject<Account>(json); 

http://www.janholinka.net/Blog/Article/11

XmlSerializer serializer = new XmlSerializer(typeof(List), new XmlRootAttribute("Products"));

StringReader stringReader = new StringReader(xmlString);

List productList = (List)serializer.Deserialize(stringReader);

And then after you deserialize you can get the Type of the object

Assignor answered 14/2, 2019 at 0:18 Comment(5)
This question is about getting a Type from a string, not serialization.Grievance
The type of any string is string. I'm not followingAssignor
Take a look at the documentation for Type.GetType(String)Grievance
Got it. I misunderstood the question. But I don't seem to be able to delete the answerAssignor
Following your comment at the top, it looks like the same question and several answers are thereAssignor

© 2022 - 2024 — McMap. All rights reserved.