dim a as textbox=new textbox()
dim b as type=gettype(textbox)  or a.gettype()

解决方案 »

  1.   

    属性是可以用反射。但是要怎么样才能得到类的类型啊?
    Type.GetType("AddressBookData");这样是得不到自己定义的类型啊。
    好象比如底下这样的控件也不可以啊。
    System.Windows.Forms.CheckBox
      

  2.   

    您可能正在納悶,為什麼取得特定型別的資訊如此重要。總之,那就是說明系統和文件的目的,對吧?下列範例能夠協助您建立使用者文件,或用來協助動態呼叫方法或設定屬性。 有一些步驟您需要去執行。首先,您需要取得使用者想要 (以字串的形式) 使用的型別。決定您將使用什麼型別之後,您需要指派物件來代表那型別。這會做兩件事:它為下列步驟建立您可使用的物件,並確認指定型別存在而且可被系統找到。下列範例指派物件至 System.String 型別。請注意,雖然範例透過其中的 Console 物件,反應訊息給使用者,而實際範例則是傳送反應至 ASP.NET 標記 (Label) 物件。然而,解譯是一樣的。 
    // don't forget your using statements at the top of your code...
    Using System;
    Using System.Reflection;// class declaration, and method declaration...// remember that this string is case-sensitive, so be careful
    Type t = Type.GetType("System.String");// check to see if we have a valid value. If our object is null, the type does not exist...
    if (t == null) {
    // Don't assume that it is a SYSTEM datatype...
    Console.WriteLine("Please ensure you specify only valid types in the type field.");
    Console.WriteLine("REMEMBER: The Case matters (Byte is not the same as byte)."); return; // don't continue processing
    }
      

  3.   

    如何... 取得組件中的型別?
    這個範例說明如何擷取指定的組件 (Assembly) 的所有型別。若要看透組件的型別,您首先需要識別您想要操作的組件。一旦您擁有相關組件的物件參考,您就可以在那組件上呼叫 GetTypes 方法,其將傳回該組件中所有型別的陣列。您可以在那陣列中使用控制邏輯來識別更確定的型別,並使用重複邏輯來剖析您的陣列,在需要時傳回型別資訊給使用者。擷取型別資訊的能力將有助於判斷替代型別 (您可能運用在指定的工作),或識別現有項目以提供您所需的功能。 
      
    Visual Basic GetTypes.aspx [執行範例] | [檢視原始檔]  
    從特定組件擷取型別時,要學會的第一件事是,如何識別組件。這個快速入門示範兩個擷取組件的方式。第一個是,識別特定物件 (您想從中尋找組件的),並向組件要求那物件的模組 (請記得模組是型別和程式碼的邏輯群組,例如 .dll 或 .exe)。第二個則是,使用 Assembly 類別的 LoadFrom 方法,為具名的模組 (如 myapp.exe) 載入特定組件。 
    // don't forget your using statements
    using System;
    using System.Reflection;
    // ...// Getting an Assembly, method 1. Get the mscorlib assembly
    // Note that other types such as String, or Int32 would have worked just as well,
    // since they reside in the same assembly
    Assembly a = typeof(Object).Module.Assembly;// Getting an Assembly, method 2. Load a particular assembly, using a reference to a
    // module that is within that assembly. Note that this requires a compiled module for
    // the reference, and when running in an aspx page, will require a fully qualifed path
    // to the file, to ensure it is correctly identified
    Assembly b = Assembly.LoadFrom ("GetTypes.exe");// note that either of the above methods is viable, depending on the information
    // you have. Since we know the name of the file which houses all of the base system
    // objects, we could do the following to replace the first example, just as effectively
    // (the absolute path may change on your machine)
    // Assembly a = Assembly.LoadFrom
    // ("c:/winserv/microsoft.net/framework/v1.0.2230/mscorlib.dll"); 
      

  4.   

    既已識別組件,您現在就可以指派 GetTypes 方法的傳回值給 Type 物件的陣列,進一步擷取型別。並進而可以操作型別。下列範例中,您取得核心 Runtime 程式庫的型別,並分別計算那組件中不同樣式型別的數目 (如需 foreach (For Each) 陳述式的詳細資訊,請參閱主題如何... 在集合中重複?)。這個範例中,我們僅示範如何計算介面的數目,雖我們可以計算其他成員數目,例如類別和列舉。 
    //Get all the types in the assembly identified in the previous example
    Type [] types = a.GetTypes ();int numInterfaces = 0;foreach (Type t in types) { //the following line uses a set of methods which identify what
    //kind of type we are currently querying
    if (t.IsInterface) {
    // only print out the names of the Interfaces
    Console.WriteLine (t.Name + "");
    numInterfaces++;
    }
    }// write out the totals
    Console.WriteLine("Out of {0} types in the {1} library:",
    types.Length, typeof(Object).Module.ToString());
    Console.WriteLine ("{0} are interfaces (listed)", types.Length, numInterfaces);