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);
}
Tôi hy vọng câu hỏi của tôi là rõ ràng. Bất kỳ ai cũng có thể giúp tôi.THX Tôi chỉ được phép sử dụng dạng chuỗi của tên lớp này để có được loại của nó.Cách lấy loại lớp theo tên lớp?
thx mọi người. Tôi đã tự mình giải quyết câu hỏi này như thế này.
{
Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace"));
}
có lẽ đây sẽ giúp http://stackoverflow.com/questions/17045635/how-to-store-reference-to-class-property-and-access-an-instances- property-by-t – user1306322
Hãy thử cách này mô hình chuỗi = "Myspace.MyClass"; Sử dụng tên hội đủ điều kiện thay vì – Devesh
http://stackoverflow.com/questions/648160/how-do-i-create-an-instance- từ-a-string-in-c –