Click to See Complete Forum and Search --> : Type.GetType doesn't work
shethashish_a
July 16th, 2003, 08:01 AM
Hi all,
I want to display property of the controls in the listbox.
But the following code which I got from an example in MSDN doesn't work properly.
On line 1 if I use "System.String" it is working but for
"System.Windows.Forms.TextBox" it doesn't return any type. So MyType is having an undefined value. On line 2 it gives error like "Object reference not set to Object" because MyType has undefined value.
1 > Type MyType = Type.GetType( "System.Windows.Forms.TextBox") ;
2 > MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
why this??
Thanks,
Al'Kimiya
July 16th, 2003, 08:11 AM
You know you can use GetType() on any object.
Like this:
TextBox tb = ...
Type type = tb.GetType();
MemberInfo[] memberArray = type.GetMembers();
shethashish_a
July 16th, 2003, 08:55 AM
No,
My user will give the type name and based on the type name I have to show the Members of that type. So until runtime I will not know the type. That's why this solution will not work.
Al'Kimiya
July 16th, 2003, 11:05 AM
Ok.. here is a workaround I think will work. The type you receive must be accessible in the application domain.
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in assemblies) {
object o = a.CreateInstance("System.Windows.Forms.TextBox");
if (o != null) {
Type type = o.GetType();
MemberInfo[] mIArray = type.GetMembers();
}
}
poochi
July 16th, 2003, 12:12 PM
Type.GetType() searches for a particular type in mscorlib assembly(CLR Library) and the current assembly only. Even if the System.Windows.Forms assembly is loaded in the memory, it will not look into it.
Al'Kimiya has posted one workaround to solve this problem. Another work around is, if you know the assembly name (not the namespace) then you can use the following code.
Type mytype = Assembly.LoadWithPartialName("System.Windows.Forms").GetType("System.Windows.Forms.TextBox");
shethashish_a
July 17th, 2003, 06:01 AM
thanks,
but how can I create the TextBox control and place it on the form with the Type object I am having?
Here I will not know the type of the control to be created until runtime.
regards,
Al'Kimiya
July 17th, 2003, 06:10 AM
Originally posted by shethashish_a
thanks,
but how can I create the TextBox control and place it on the form with the Type object I am having?
Here I will not know the type of the control to be created until runtime.
regards,
You have the object and since it is a TextBox (which you can check with GetType()) you can add it to form by setting it's parent to the Form. That's why you enumerate methods and properties with reflection. You could also convert your object to TextBox:
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in assemblies) {
object o = a.CreateInstance("System.Windows.Forms.TextBox");
if (o != null) {
if (o is TextBox) {
TextBox tb = (TextBox) o;
tb.Parent = Form1;
}
}
or rather as a Control (more general)
if (o is Control) {
Control o = (Control) o;
o.Parent = Form1;
}
shethashish_a
July 18th, 2003, 09:41 AM
that's fine
but what if you don't even know that u r creating a textbox control or any other control.
here is the solution which I found and it is working. Here I am sure that i will be creating any of the windows control that is derived from the System.Windows.Forms.Control class. So here is my solution.
private void CreateControl(string controlType)
{
System.Windows.Forms.Control ctrlDyn ;
Type t = myAssem.GetType(controlType) ;
System.Reflection.ConstructorInfo[] ctorInfo = t.GetConstructors() ;
pnlDynControl.Controls.Clear() ;
if(ctorInfo.Length >=0)
{
ctrlDyn = (System.Windows.Forms.Control)ctorInfo[0].Invoke(null) ;
ctrlDyn.Location = new Point(20,80) ;
ctrlDyn.Visible = true ;
ctrlDyn.Text = "Hi all" ;
ctrlDyn.Width = 100 ;
ctrlDyn.Height = 50 ;
pnlDynControl.Controls.Add(ctrlDyn) ;
}
}
here myAssem is and Assembly object which has System.Windows.Forms.Dll already loaded in it.
Now I have new problem:
How I will set the property of the control created this way?
If it is a simple string type property I can set it using PropertyInfo.SetValue property, but what if it is a user defined class or collection??
regards,
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.