【发布时间】:2013-08-20 21:44:45
【问题描述】:
namespace Myspace
{
public class MyClass
{
}
} //This class is in another file.
using Myspace;
static void Main(string[] args)
{
Regex regexViewModelKey = new Regex(RegularExpr.ViewModelKeyPattern);
string viewModel = regexViewModelKey.Match(match.Value).Value;
//Now, vieModel is a string, and its value is "MyClass". So, I only know the class name, this is why I ask this question.
//Now, I'm only allowed to use the string form of this class name to get its type.
//I have tyied like this, but its no use.
Type t = Type.GetType(viewModel);
//it's return a null.
//Then I tyied another method like this, but there is an exception when calling Assembly.Load
Assembly assembly = Assembly.Load("Myspace");
Type ty = assembly.GetType("Myspace" + viewModel);
}
我希望我的问题很清楚。谁能帮帮我.THX 我只允许使用这个类名的字符串形式来获取它的类型。
谢谢大家。 我自己就这样解决了这个问题。
{
Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace"));
}
【问题讨论】:
-
试试这个方法 string model = "Myspace.MyClass";改用程序集限定名
标签: c# reflection gettype