C#中 - 你如何返回基于掀起了字符串的类型 [英] C# - How do you return a Type based off of a string

查看:143
本文介绍了C#中 - 你如何返回基于掀起了字符串的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一类称为服装继承自一个抽象类产品。

 公共类服装:产品
{
    公共BOOL IsInSeason {获得;组;}
    公共字符串材料{获得;组; }
    公共小数IsFeatured {获得;组; }
}
 

编辑: 在一个函数,我怎么会能够通过字符串服装,并有一个typeof运算的类返回?

  ReturnTypeOfClassBasedOffOfString(string类)
{
    // ...
}
 

解决方案

使用类型.GetType(串)

注意,有时类名是不够的。有时你会需要提供 AssemblyQualifiedName

如果类型是在当前执行的程序集或在mscorlib.dll,它足以提供其命名空间限定的类型名称。

For example, I have a class called Clothing that inherits from a abstract class of Product.

public class Clothing : Product
{
    public bool IsInSeason {get; set;}
    public string Material {get; set; }
    public Decimal IsFeatured { get; set; }
}

Edit: In a function, how would I be able to pass the string "Clothing" and have a typeof the class returned?

ReturnTypeOfClassBasedOffOfString(string class)
{
    // ... 
}

解决方案

Use Type.GetType(string)

Note that sometime the class name won't be enough. Sometime you will need to provide the AssemblyQualifiedName

"If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace."

这篇关于C#中 - 你如何返回基于掀起了字符串的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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