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

Spero che la mia domanda sia chiara. Qualcuno mi può aiutare.THX Mi è permesso solo di usare la forma di stringa di questo nome di classe per ottenere il suo tipo.Come ottenere il tipo di classe in base al nome della classe?

thx tutti. Ho risolto questa domanda da solo in questo modo.

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

forse questo ti aiuto http://stackoverflow.com/questions/17045635/how-to-store-reference-to-class-property-and-access-an-instances- property-by-t – user1306322

+1

Prova in questo modo string model = "Myspace.MyClass"; utilizza invece il nome qualificato dell'assembly – Devesh

+0

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

risposta

2

In generale, non è quasi mai necessario fare confronti di tipo a meno che non si stia facendo qualcosa con la riflessione o le interfacce. Tuttavia:

Se si conosce il tipo che si desidera confrontare con, usa il è o come operatori:

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

Il come operatore esegue un cast che restituisce null se viene a mancare, piuttosto che un'eccezione:

TypeIKnow typed = unknownObject as TypeIKnow; 

Se non si conosce il tipo e vogliono solo il runtime informazioni sul tipo, utilizzare il .GetType() metodo:

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

Ho cambiato la mia domanda in modo che sia più chiara. Puoi vederlo di nuovo? grazie –

0

La tua linea Type.GetType (modello) funzionerà se si utilizza il nome classe completo, incluso il suo spazio dei nomi.

Inoltre, se si trova in un assembly diverso dal codice che effettua la chiamata, è necessario utilizzare Assembly.GetType (typeName) quando l'oggetto di assieme a cui si fa riferimento è un'istanza dell'assembly che contiene il tipo.

13

basta usare la funzione typeof(). Il parametro è solo il nome di questa classe.

Type type = typeof(FIXProtoClientTest); 

MSDN on typeof()

+2

Solo se il tipo è noto in fase di compilazione. – Alejandro

Problemi correlati