Create Control from String Value
I am using DevExpress Components controls.I want to create controls from string Value
like "DevExpress.XtraEditors.TextEdit".I know I can make this with reflection like
var textBoxType = typeof(Control).Assembly.GetType("System.Windows.Forms.TextBox", true);
var textBox = Activator.CreateInstance(textBoxType);
I want to make this for write little code.but DevExpress have a lot of开发者_Python百科 namespace and class. Can I create control from string unless give A MainClass? (like my sample code typeof(Control)) if I can not make I have to use alot of if
You need to know which assembly the class is defined in.
For example, all of the editors are in DevExpress.XtraEditors.vX.Y.dll
, or typeof(BaseEdit).Assembly
.
If you don't know which assembly it's defined in, you can create a collection of DevExpress assemblies (typeof(GridControl).Assembly, typeof(TreeList).Assembly, ...
) and loop through them until asm.GetType(name)
doesn't return null
.
Note that it will be very slow.
精彩评论