2013-08-19 65 views
5
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")); 
} 
+0

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

+1

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

+0

http://stackoverflow.com/questions/648160/how-do-i-create-an-instance- từ-a-string-in-c –

Trả lời

2

Nói chung, bạn sẽ hầu như không bao giờ cần phải làm so sánh loại trừ khi bạn đang làm một cái gì đó với sự phản ánh hoặc giao diện. Tuy nhiên:

Nếu bạn biết loại bạn muốn so sánh nó với, sử dụng là hay như các nhà khai thác:

if(unknownObject is TypeIKnow) { // run code here 

Toán tử as thực hiện một dàn diễn viên mà trả về null nếu nó không thành công chứ không phải là một ngoại lệ:

TypeIKnow typed = unknownObject as TypeIKnow; 

Nếu bạn không biết loại và chỉ muốn loại thông tin thời gian chạy, sử dụng .GetType() phương pháp:

Type typeInformation = unknownObject.GetType(); 



    // int is a value type 
    int i = 0; 
    // Prints True for any value of i 
    Console.WriteLine(i.GetType() == typeof(int)); 

    // string is a sealed reference type 
    string s = "Foo"; 
    // Prints True for any value of s 
    Console.WriteLine(s == null || s.GetType() == typeof(string)); 

    // object is an unsealed reference type 
    object o = new FileInfo("C:\\f.txt"); 
    // Prints False, but could be true for some values of o 
    Console.WriteLine(o == null || o.GetType() == typeof(object)); 

// Get the type of a specified class. 
       Type myType1 = Type.GetType("System.Int32"); 
       Console.WriteLine("The full name is {0}.", myType1.FullName); 
       // Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException. 
       Type myType2 = Type.GetType("NoneSuch", true); 
       Console.WriteLine("The full name is {0}.", myType2.FullName); 

    // FileSystemInfo is an abstract type 
    FileSystemInfo fsi = new DirectoryInfo("C:\\"); 
    // Prints False for all non-null values of fsi 
    Console.WriteLine(fsi == null || fsi.GetType() == typeof(FileSystemInfo)); 
+0

Tôi đã thay đổi câu hỏi của mình để nó rõ ràng hơn.Bạn có thấy nó một lần nữa không? thx –

0

Dòng của bạn Type.GetType (mô hình) sẽ hoạt động nếu bạn sử dụng tên lớp hoàn toàn đủ điều kiện, bao gồm cả không gian tên của nó.

Hơn nữa, nếu nó nằm trong một cụm khác từ mã thực hiện cuộc gọi, bạn nên sử dụng Assembly.GetType (typeName) khi đối tượng assembly được gọi là một thể hiện của assembly có chứa kiểu.

13

chỉ cần sử dụng hàm typeof(). Tham số chỉ là tên lớp.

Type type = typeof(FIXProtoClientTest); 

MSDN on typeof()

+2

Chỉ khi loại được biết tại thời gian biên dịch. – Alejandro