2013-08-19 45 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); 
} 

我希望我的問題很清楚。任何人都可以幫助我.THX 我只允許使用這個類名的字符串形式來獲得它的類型。如何通過類名獲取類類型?

thx everyone。 我已經自己解決了這個問題。

{ 
     Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace")); 
} 
+0

也許這會幫助http://stackoverflow.com/questions/17045635/how-to-store-reference-to-class-property-and-access-an-instances- property-by-t – user1306322

+1

試試這種方式string model =「Myspace.MyClass」;使用匯編合格的名稱代替 – Devesh

+0

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

回答

2

一般來說,除非您使用反射或界面進行操作,否則幾乎不需要進行類型比較。儘管如此:

如果你知道你要進行對比的類型,使用的是或作爲運營商:

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

as運算符執行的是返回null鑄造如果失敗,而不是一個例外:

TypeIKnow typed = unknownObject as TypeIKnow; 

如果你不知道的類型,只是想運行時類型信息,使用.GetType()方法:

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

我已經改變了我的問題,以便它更清楚。你能再次看到它嗎? thx –

0

如果使用完全限定的類名稱(包括它的名稱空間),則Type.GetType(model)行可以工作。另外,如果它與調用的代碼位於不同的程序集中,則當引用的程序集對象是包含該類型的程序集的實例時,應該使用Assembly.GetType(typeName)。

13

只使用函數typeof()。該參數就是那個類的名字。

Type type = typeof(FIXProtoClientTest); 

MSDN on typeof()

+2

只有在編譯時已知類型。 – Alejandro