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

查看:28
本文介绍了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
    {
      ...
    }
}

我尝试获取这样的类型,但它返回空值:

I trying getting type like this but it returns null:

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

给定一个代表我的类型(即汽车")的字符串变量,我如何获得它的类型汽车?

更新和解决方案

感谢@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 serializer = 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);

列出产品列表 =(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天全站免登陆