C#是否可以从表示该类型的字符串中获取实际类型? [英] C# Is it possible to get actual type out of a string representing that type?

查看:196
本文介绍了C#是否可以从表示该类型的字符串中获取实际类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串 Car,我想从中获取 Car 类型。我的班级汽车是:

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
    {
      ...
    }
}

我尝试输入像这样,但返回null:

I trying getting type like this but it returns null:

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

给出一个表示我的类型的字符串变量(即 Car),我如何获得其Type Car?

更新和解决方案

在此感谢@AsafPala的建议( Type.GetType( namespace.abClassName ;)返回null ),我开始工作了。事实证明,如果要从解决方案中的另一个项目(另一个程序集)调用GetType,则还必须提供程序集名称。

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.

由于MySolution有2个项目MyProjectA和MyProjectB,所以它们是同一解决方案中的2个独立项目(或程序集),因此您需要提供类型完全指定的程序集名称(是 MySolution.MyProjectA.Car )和程序集名称(即 MySolution.MyProjectA ),以逗号分隔,如下所示:

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");

更新和解决方案

由于我在另一个项目(与程序集相同)中调用此代码,因此需要提供完全指定的类型名称和程序集名称,以逗号分隔,如下所示:

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
             ...
        }
    }
}


推荐答案

是。您需要先将对象序列化为字符串。您可以使用json或xml作为序列化器类型。

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序列化器= new XmlSerializer(typeof(List),
new XmlRootAttribute( Products));

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

StringReader stringReader = new StringReader(xmlString);

StringReader stringReader = new StringReader(xmlString);

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

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

然后反序列化之后,您可以获取对象的类型

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

这篇关于C#是否可以从表示该类型的字符串中获取实际类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆